Qt中实现菜单和工具栏功能

Qt创建菜单和工具栏:

一、  temp.h文件的内容

         1.定义类temp继承QMainWindow

         2.添加Q_OBJECT , 构造函数 、 析构函数等内容

         3.$重点内容

                   3.1定义QAction *newAction;  (相当于菜单和工具条里面的基本功能如:新建、打开文件)

                   3.2定义QMenu *fileMenu;

                   3.3定义  QToolBar *fileToolBar;

         4.定义QAction的响应函数即slot函数:

                   private slots:

                            void msg();

二、 temp.cpp 文件内容

         1.添加头文件

                   #include <QtGui>

                   #include "temp.h"

                   #include<windows.h>    //MessageBox()函数

         2.填写构造函数(重点内容)

                   2.1 初始化和设置QAction

    newAction = new QAction(tr("&New"), this);
    newAction->setIcon(QIcon("D:/Documents/Visual Studio 2010/Projects/qt-book/chap03/temp/images/new.png"));
    newAction->setShortcut(QKeySequence::New);
    newAction->setStatusTip(tr("Create a new spreadsheet file"));
    connect(newAction, SIGNAL(triggered()), this, SLOT(msg()));

                   2.2 添加File菜单并返回指针给QMenu *fileMenu , 将QAction添加到菜单File中。

                            fileMenu = menuBar()->addMenu(tr("&File"));       //menuBar()需要头文件#include <QtGui>

                            fileMenu->addAction(newAction);

                  2.3 添加File工具栏并返回指针给QToolBar * fileToolBar ,将QAction添加到File工具栏中

                            fileToolBar = addToolBar(tr("&File"));

                            fileToolBar->addAction(newAction);

         3.实现void msg()函数(用于测验使用)

                            void temp::msg()

                                     {        MessageBox(NULL,TEXT("这是对话框"),TEXT("你好"),MB_ICONINFORMATION|MB_YESNO); }

三、 main.cpp文件的内容

#include "temp.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    temp w;
    //w.setFixedSize(800,22);   //此函数可以实现ENVI菜单样式
    w.show();
    return a.exec();
}

main.cpp
main.cpp

四、补充temp.h和temp.cpp文件

#ifndef TEMP_H
#define TEMP_H

#include <QtGui/QMainWindow>
class temp : public QMainWindow
{
    Q_OBJECT

public:
    temp(QWidget *parent = 0);
    ~temp();
private:
    QAction *newAction;
    QMenu *fileMenu;
    QToolBar *fileToolBar;

private slots:
    void msg();

};

#endif // TEMP_H
temp.h
#include <QtGui>
#include "temp.h"
#include<windows.h>
temp::temp(QWidget *parent)
    : QMainWindow(parent)
{
    newAction = new QAction(tr("&New"), this);
    newAction->setIcon(QIcon("D:/Documents/Visual Studio 2010/Projects/qt-book/chap03/temp/images/new.png"));
    newAction->setShortcut(QKeySequence::New);
    newAction->setStatusTip(tr("Create a new spreadsheet file"));
    connect(newAction, SIGNAL(triggered()), this, SLOT(msg()));

     fileMenu = menuBar()->addMenu(tr("&File"));
     fileMenu->addAction(newAction);

      fileToolBar = addToolBar(tr("&File"));
      fileToolBar->addAction(newAction);
      
      //setCentralWidget(fileToolBar);  //此函数的作用是将QWidget控件放在中间位置
}

temp::~temp()
{

}
void temp::msg()
{
    MessageBox(NULL,TEXT("这是对话框"),TEXT("你好"),MB_ICONINFORMATION|MB_YESNO);
}
temp.cpp

五、结果预览

  
  

 六、工程文件下载地址

  http://pan.baidu.com/s/1mgkOkrQ

原文地址:https://www.cnblogs.com/lwngreat/p/4201268.html