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 = );
~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

Qt中实现菜单和工具栏功能的更多相关文章

  1. Qt5:Qt中屏幕或窗口截图功能的实现

    要想在Qt中实现屏幕或窗口截图功能 ,通常有两种方法: 1  -- 使用 QPixmap 类 2  -- 使用 QScreen类 然而虽然俩两种方法用到的类不相同,但是调用到的类成员函数的函数名称和参 ...

  2. qt中的菜单QMenu QAction

    Qt中要建立菜单,有三个类很重要: QMenuBar(QWidget * parent = 0) QMenu(QWidget * parent = 0) QMenu(const QString &am ...

  3. PyQt5教程——菜单和工具栏(3)

    PyQt5中的菜单和工具栏 在这部分的PyQt5教程中,我们将创建菜单和工具栏.菜单式位于菜单栏的一组命令操作.工具栏是应用窗体中由按钮和一些常规命令操作组成的组件. 主窗口 QMainWindow类 ...

  4. 在PyQt中构建 Python 菜单栏、菜单和工具栏

    摘要:菜单.工具栏和状态栏是大多数GUI 应用程序的常见且重要的图形组件.您可以使用它们为您的用户提供一种快速访问应用程序选项和功能的方法. 本文分享自华为云社区<Python 和 PyQt:创 ...

  5. Qt中截图功能的实现

    提要 需求:载入一张图片并显示,能够放大缩小,能够截取图片的某个矩形并保存. 原以为蛮简单的一个功能,事实上还是有点小复杂. 最简单Qt图片浏览器能够參考Qt自带的Demo:Image Viewer ...

  6. 在 jupyter 中添加菜单和自动完成功能

    在 jupyter 中添加菜单和自动完成功能 参考文档http://www.360doc.com/content/17/1103/14/1489589_700569828.shtmlhttp://to ...

  7. Qt中QMenu的菜单关闭处理方法

    Qt中qmenu的实现三四千行... 当初有个特殊的需求, 要求菜单的周边带几个像素的阴影, 琢磨了半天, 用QMenu做不来, 就干脆自己用窗口写一个 然而怎么让菜单消失却非常麻烦 1. 点击菜单项 ...

  8. qt中qlineedit和qtextedit右键菜单翻译成中文

    没有linguist和lupdate等命令需要安装Linguist: 在Terminal中输入: sudo apt-get install qt4-dev-tools qt4-doc qt4-qtco ...

  9. 第47课 Qt中的调色板

    1. QPalette类 (1)QPalette类提供了绘制QWidget组件的不同状态所使用的颜色. (2)QPalette对象包含了3个状态的颜色描述 ①激活颜色组(Active):组件获得焦点使 ...

随机推荐

  1. iframe 重新加载闪过白块问题

    在使用iframe时,iframe背景为白块,刷新时也会闪过白块.如果刷新时间长,就会一直出现白块,让人很烦恼,通过网上搜资料,测试最终解决方法如下所示,注意主要针对IE浏览器测试. 一.iframe ...

  2. js 进阶笔记

    JS中substr和substring的用法和区别 substr和substring都是JS截取字符串函数,两者用法很相近, substr方法 返回一个从指定位置开始的指定长度的子字符串. strin ...

  3. MVC 授权过滤器 AuthorizeAttribute

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. USACO OPEN 12 BOOKSELF(转)

    原文出处:http://txhwind.blog.163.com/blog/static/2035241792012112285146817/(版权为原作者所有,本博文无营利目的,不构成侵权) 题目大 ...

  5. DECLARE CONTINUE HANDLER FOR NOT FOUND

    1.解释: 在mysql的存储过程中经常会看到这句话:DECLARE CONTINUE HANDLER FOR NOT FOUND. 它的含义是:若没有数据返回,程序继续,并将变量IS_FOUND设为 ...

  6. nginx+webpy 出现 upstream timed out

    关于nginx配置webpy应用出现的错误 upstream timed out (: Connection timed out) while reading response header from ...

  7. 生成shadow中hash字串

    [root@master base]# openssl passwd -1 -salt 123Password: $1$123$2rm.J6pr3p.rmj6YoKSQ8.[root@master b ...

  8. Composer的使用

    安装 curl -sS https://getcomposer.org/installer | php 你可以使用--install-dir选项将Composer安装到指定的目录,例如: curl - ...

  9. 【python】bytearray和string之间转换,用在需要处理二进制文件和数据流上

    最近在用python搞串口工具,串口的数据流基本读写都要靠bytearray,而我们从pyqt的串口得到的数据都是string格式,那么我们就必须考虑到如何对这两种数据进行转换了,才能正确的对数据收发 ...

  10. CSS3 设置 Table 隔行变色

    table tr:nth-child(odd){background:#F4F4F4;} table td:nth-child(even){color:#C00;}