QT编程:菜单及工具条的建立

两者的建立过程是一致的。

1、头文件应包含 QAction、QMenu、QMenuBar、QToolBar等相关文件。如下:

#include <QMenu> #include <QMenuBar> #include <QAction> #include <QToolBar>

2、应在MainWindow中增加1步骤中提到的相关变量,如:action、menubar、toolbar等。如下:

QMenu *fileMenu, *editMenu, *helpMenu; QToolBar *fileToolBar, *editToolBar; QAction * newAct; QAction *openAct; QAction *saveAct; QAction *saveAsAct; QAction *exitAct; QAction *cutAct; QAction *copyAct; QAction *pasteAct; QAction *aboutAct; QAction *aboutQtAct;

3、初始化各种变量:

fileMenu = new QMenu(this); editMenu = new QMenu(this); helpMenu = new QMenu(this); /* 以下两个不用初化,因为MainWindow本身已经有一个函数addToolBar建立相关的指针。 fileToolBar = new QToolBar(NULL, this); editToolBar = new QToolBar(NULL, this);*/ /* menuBar不用初化,因为MainWindow本身已经有一个函数menuBar()建立相关的指针。 menuBar = new QMenuBar(this);*/

 

4、设置各种功能对应的动作:

newAct = new QAction(QIcon( ":/images/new.png" ), tr( "&New" ), this ); newAct->setShortcut(tr("Ctrl+N" )); newAct->setStatusTip(tr("Create a new file" )); connect(newAct, SIGNAL(triggered()), this , SLOT(newFile())); openAct = new QAction(QIcon( ":/images/open.png" ), tr( "&Open..." ), this ); openAct->setShortcut(tr("Ctrl+O" )); openAct->setStatusTip(tr("Open an existing file" )); connect(openAct, SIGNAL(triggered()), this , SLOT(open())); saveAct = new QAction(QIcon( ":/images/save.png" ), tr( "&Save" ), this ); saveAct->setShortcut(tr("Ctrl+S" )); saveAct->setStatusTip(tr("Save the document to disk" )); connect(saveAct, SIGNAL(triggered()), this , SLOT(save())); saveAsAct = new QAction(tr( "Save &As..." ), this ); saveAsAct->setStatusTip(tr("Save the document under a new name" )); connect(saveAsAct, SIGNAL(triggered()), this , SLOT(saveAs())); exitAct = new QAction(tr( "E&xit" ), this ); exitAct->setShortcut(tr("Ctrl+Q" )); exitAct->setStatusTip(tr("Exit the application" )); connect(exitAct, SIGNAL(triggered()), this , SLOT(close())); cutAct = new QAction(QIcon( ":/images/cut.png" ), tr( "Cu&t" ), this ); cutAct->setShortcut(tr("Ctrl+X" )); cutAct->setStatusTip(tr("Cut the current selection's contents to the clipboard" )); connect(cutAct, SIGNAL(triggered()), this, SLOT(cut())); copyAct = new QAction(QIcon( ":/images/copy.png" ), tr( "&Copy" ), this ); copyAct->setShortcut(tr("Ctrl+C" )); copyAct->setStatusTip(tr("Copy the current selection's contents to the clipboard" )); connect(copyAct, SIGNAL(triggered()), this, SLOT(copy())); pasteAct = new QAction(QIcon( ":/images/paste.png" ), tr( "&Paste" ), this ); pasteAct->setShortcut(tr("Ctrl+V" )); pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current selection" )); connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste())); aboutAct = new QAction(tr( "&About" ), this ); aboutAct->setStatusTip(tr("Show the application's About box" )); connect(aboutAct, SIGNAL(triggered()), this , SLOT(about())); aboutQtAct = new QAction(tr( "About &Qt" ), this ); aboutQtAct->setStatusTip(tr("Show the Qt library's About box" )); connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

 

5、增加菜单及菜单条:

fileMenu = menuBar()->addMenu(tr( "&File" )); fileMenu->addAction(newAct); fileMenu->addAction(openAct); fileMenu->addAction(saveAct); fileMenu->addAction(saveAsAct); fileMenu->addSeparator(); fileMenu->addAction(exitAct); editMenu = menuBar()->addMenu(tr("&Edit" )); editMenu->addAction(cutAct); editMenu->addAction(copyAct); editMenu->addAction(pasteAct); menuBar()->addSeparator(); helpMenu = menuBar()->addMenu(tr("&Help" )); helpMenu->addAction(aboutAct); helpMenu->addAction(aboutQtAct);

6、增加工具条:

fileToolBar = addToolBar(tr("File")); fileToolBar->addAction(newAct); fileToolBar->addAction(openAct); fileToolBar->addAction(saveAct); editToolBar = addToolBar(tr("Edit")); editToolBar->addAction(cutAct); editToolBar->addAction(copyAct); editToolBar->addAction(pasteAct);

7、添加图标:

     1)为了添加图标,我们首先要使用Qt的资源文件。在QtCreator的项目上右击,选择New File...,然后选择resource file。

     2)然后点击next,输入资源文件名并选择好文件放置的位置,再点击Finish即可。建议应该在仔细规划好文件之后,建在专门的 rsources文件夹下。完成之后,生成的是一个.qrc文件,qrc其实是Qt Recource Collection的缩写。它只是一个普通的XML文件,可以用记事本等打开。这里完全利用QtCreator操作这个文 件。

     3)双击项目管理中的这个文件名,进入对资源文件的编辑状态。

     4)点击Add按钮,首先选择Add prefix,然后把生成的/new/prefix改成/。这时prefix就是以后使用图标时需要提供的前缀,以/开头。添加过prefix之后,然后在工程文件中添加相应的图标(可以添加多个),再选择Add file,选择图标。完成之后保存qrc文件即可。

     5)重新编译项目即可。

8、添加对应的功能槽(slots):

     1)在相应的头文件中增加槽的预说明:

private slots: void createNewDocument(); /*void open(); void save(); void saveAs(); void close(); void cut(); void copy(); void paste(); void about(); void aboutQt();*/

    2)在对应的实现代码CPP文件中增加相应的实现代码:

void MainWindow::createNewDocument(){ QMessageBox::warning(this,tr("Warning"), tr("Hello, let's create a new document."), QMessageBox::Yes | QMessageBox::Default, QMessageBox::No); } /* void MainWindow::open(){ QMessageBox::warning(this,tr("Warning"), tr("Hello, let's create a new document."), QMessageBox::Yes | QMessageBox::Default, QMessageBox::No); } void MainWindow::save(){ } void MainWindow::saveAs(){ } void MainWindow::close(){ } void MainWindow::cut(){ } void MainWindow::copy(){ } void MainWindow::paste(){ } void MainWindow::about(){ } void MainWindow::aboutQt(){ }*/

 

9、至此,菜单及工具条的设计及实现完成。

你可能感兴趣的:(编程,File,application,qt,工具,Signal)