首先,新建一个QT Widget项目工程,删除MainWindow.ui中的菜单栏,并添加一个GraphicsView控件。
一,为工具栏添加openImage动作:
将openImage.png放在工程目录下新建的res文件夹中。
在工程目录下创建qrc文件, vi res.qrc如下:
<RCC>
<qresource prefix="/">
<file>res/openImage.png</file>
</qresource>
</RCC>
接着在项目中导入该qrc文件
在MainWindow类的构造函数中ui->setupUi(this);下面添加代码创建newAction动作并加到主工具栏,用以打开图像:
ui->setupUi(this);
QAction *newAction = new QAction(tr("&New"),this);
newAction->setIcon(QIcon(":/res/openImage.png"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
ui->mainToolBar->addAction(newAction);
声明及定义槽函数newFile,打开图像并在QGraphicsView控件中显示。
void MainWindow::newFile()
{
QMessageBox::warning(this,"open file","