博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt中实现菜单和工具栏功能
阅读量:6953 次
发布时间:2019-06-27

本文共 2553 字,大约阅读时间需要 8 分钟。

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 
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 
class temp : public QMainWindow{ Q_OBJECTpublic: temp(QWidget *parent = 0); ~temp();private: QAction *newAction; QMenu *fileMenu; QToolBar *fileToolBar;private slots: void msg();};#endif // TEMP_H
temp.h
#include 
#include "temp.h"#include
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

五、结果预览

  

  

 六、工程文件下载地址

  

转载于:https://www.cnblogs.com/lwngreat/p/4201268.html

你可能感兴趣的文章
eclipse打断点,进行弹窗提示后点击是才进入debug视图,这个要怎么恢复
查看>>
CentOS7 安装 mysql8
查看>>
[TypeScript] Define Custom Type Guard Functions in TypeScript
查看>>
人人都要培养AIQ
查看>>
【Java】使用Apache POI生成和解析Excel文件
查看>>
supervisor来自动化部署,集成git
查看>>
jeffy-vim-v3.1.tar.gz
查看>>
Android wpa_supplicant 启动过程
查看>>
290.单词模式。给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。(c++方法)...
查看>>
Task.Run()任务执行
查看>>
Nodejs+Express 搭建 web应用
查看>>
前后端分手大师——MVVM 模式 从输入cnblogs.com到博客园首页完全展示发生了什么...
查看>>
Racket Cheat Sheet
查看>>
TeamPlain for VSTS - Web Access for Team System-TFS 跨平台的客户端
查看>>
(原創) 如何用程序的方式载入indexd过的图形文件? (.NET) (ASP.NET) (C#) (GDI+) (Image Processing)...
查看>>
cocos2d的常用动作及效果总结之四:Special Actions
查看>>
ASP.NET MVC
查看>>
[ lucene扩展 ] MoreLikeThis 相似检索
查看>>
如果返回结构体类型变量(named return value optimisation,NRVO)
查看>>
C# 多线程详解 Part.02(UI 线程和子线程的互动、ProgressBar 的异步调用)
查看>>