前面的文章已经封装了数个最基本的图元,但是视图的呈现是基于测试数据形成的。为了尽快完善软件交互的框架和能力,本文将增加工具栏。
资源文件
1.首先,创建按钮图标。使用绘图软件构建出工具栏按钮的图标,绘图软件多种多样,可基于自己的熟悉程度来创建图标。
2.创建qrc资源文件。
<RCC><qresource prefix="/"><file>images/draw_circle.png</file><file>images/draw_line.png</file><file>images/draw_point.png</file></qresource>
</RCC>
3.配置CMake文件,将资源文件加入。
qt5_add_big_resources(SOURCES resource/main.qrc)
SOURCE_GROUP("qrc" ${SOURCES})add_executable(JCad${MOC_FILES}${SOURCES}../src/main.cpp../src/mainwindow.cpp../src/mainwindow.h../src/mainwindow.ui../src/j-mdi-area.cpp../src/j-view-widget.cpp
)
创建工具栏
本文中创建三个QAction到QToolBar工具栏上,并添加相应ICON和槽函数。
// mainwindow.h
public slots:void SlotDrawPoint();void SlotDrawLine();void SlotDrawCircle();
private:void InitToolBar();private:Ui::MainWindow *ui;QMdiArea * mMdiArea = nullptr;// toolbarQToolBar* tool_bar_draw_ = nullptr;// mainwindow.cppvoid MainWindow::InitToolBar() {QSize icon_size(25, 25);int fixed_width = 30;tool_bar_draw_ = new QToolBar();addToolBar(Qt::LeftToolBarArea, tool_bar_draw_);tool_bar_draw_->setOrientation(Qt::Vertical);tool_bar_draw_->setMovable(false);tool_bar_draw_->setFixedWidth(fixed_width);tool_bar_draw_->setIconSize(icon_size);QAction *action_draw_point = new QAction(tr("Point"), tool_bar_draw_);tool_bar_draw_->addAction(action_draw_point);action_draw_point->setIcon(QIcon(":/images/draw_point.png"));connect(action_draw_point, SIGNAL(triggered(bool)), this, SLOT(SlotDrawPoint()));QAction *action_draw_line = new QAction(tr("Line"), tool_bar_draw_);tool_bar_draw_->addAction(action_draw_line);action_draw_line->setIcon(QIcon(":/images/draw_line.png"));connect(action_draw_line, SIGNAL(triggered(bool)), this, SLOT(SlotDrawLine()));QAction *action_draw_circle = new QAction(tr("Circle"), tool_bar_draw_);tool_bar_draw_->addAction(action_draw_circle);action_draw_circle->setIcon(QIcon(":/images/draw_circle.png"));connect(action_draw_circle, SIGNAL(triggered(bool)), this, SLOT(SlotDrawCircle()));
}void MainWindow::SlotDrawPoint() {
}void MainWindow::SlotDrawLine() {
}void MainWindow::SlotDrawCircle() {
}