3.关于QT中的MainWindow窗口,MenuBar,ToolBar,QuickTip等方面的知识点
1
新建一个空Qt项目
|
编写12MainWindow.pro |
|
HEADERS MyMainWindow.h MyView.h SOURCES MyMainWindow.cpp MyView.cpp QT |
|
MyView.h |
#ifndef MYVIEW_H #define MYVIEW_H #include <QWidget> class MyView:public QWidget{
Q_OBJECT public: explicit MyView(QWidget *parent); void paintEvent(QPaintEvent *); signals: public slots: }; #endif // MYVIEW_H |
|
MyView.cpp |
#include "MyView.h" #include <QPainter> MyView::MyView(QWidget *parent): QWidget(parent) {
} void MyView::paintEvent(QPaintEvent *) {
QPainter p(this); p.fillRect(rect(),Qt::red); } |
|
MyMainWindow.h |
#ifndef MYMAINWINDOW_H #define MYMAINWINDOW_H #include <QMainWindow> #include <QLabel> #include "MyView.h" #include <QSystemTrayIcon> //通过这个头文件可以让程序在状态栏显示icon class MyMainWindow:public QMainWindow {
Q_OBJECT public: explicit MyMainWindow(QWidget *parent); QLabel* _label; MyView* _view; QSystemTrayIcon* _icon; void paintEvent(QPaintEvent *); void mousePressEvent(QMouseEvent *); QMenu* _menu; bool event(QEvent *event); bool eventFilter(QObject *, QEvent *); signals: public slots: void slotOpen(); void slotActivated(QSystemTrayIcon::ActivationReason); }; #endif // MYMAINWINDOW_H |
|
MyMainWindow.cpp |
#include "MyMainWindow.h" #include <QApplication> #include <QMenu> #include <QMenuBar> #include <QAction> #include <QDebug> #include <QFileDialog> #include <QToolBar> #include <QStatusBar> #include <QLabel> #include <QPixmap> #include <QPainter> #include <QMouseEvent> #include <QCursor> #include <QIcon> MyMainWindow::MyMainWindow(QWidget *parent): QMainWindow(parent) {
/*加菜单*/ QMenuBar* pMenuBar = menuBar(); QMenu* menu = pMenuBar->addMenu("&File");
_menu = menu; QAction* openAction = menu->addAction("&Open", this, SLOT(slotOpen()), QKeySequence::Open);
QAction* saveAction = menu->addAction("&Save", this, SLOT(slotOpen()), QKeySequence::Save);
menu->addSeparator(); QAction* closeAction = menu->addAction("&Exit", this, SLOT(close()), QKeySequence::Close);
closeAction->setToolTip("close window");
/*toolbar 添加工具栏*/ QToolBar* toolBar = this->addToolBar("MyToolBar");
toolBar->addAction(openAction); toolBar->addAction(saveAction); toolBar->addAction(closeAction); /* status bar*/ QStatusBar* pStatusBar = this->statusBar(); pStatusBar->addWidget(_label = new QLabel("OK"));
_label->setText("<font color=red>Processing...</font>");
/* 别的控件占用了之后,剩下的区域都是CentralWidget */ _view = new MyView; this->setCentralWidget(_view); //system tray icon _icon = new QSystemTrayIcon; _icon->setIcon(QIcon("../bing.ico"));
_icon->setToolTip("This is tray icon test");
_icon->show(); _icon->setContextMenu(_menu); connect(_icon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); this->installEventFilter(this); } void MyMainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) {
if(reason == QSystemTrayIcon::Trigger) {
if(this->isHidden()) this->show(); else this->hide(); } } /** * @brief MyMainWindow::eventFilter * @param o * @param e * @return 消息过滤器 */ bool MyMainWindow::eventFilter(QObject *o, QEvent *e) {
if(o == (QObject *)this && e->type() == QEvent::Close) {
return true; } return QMainWindow::eventFilter(o, e); } bool MyMainWindow::event(QEvent *ev) {
qDebug() << ev; if(ev->type() == QEvent::Close) {
return false; } return QMainWindow::event(ev); } void MyMainWindow::mousePressEvent(QMouseEvent *ev) {
if(ev->button() == Qt::RightButton) _menu->exec(QCursor::pos()); } void MyMainWindow::paintEvent(QPaintEvent *) {
QPainter p(this); p.drawPixmap(QPoint(0,0),QPixmap(".../aaa.png"));
} void MyMainWindow::slotOpen() {
QString strFile = QFileDialog::getOpenFileName(); qDebug() << "Open file is:" << strFile; } int main(int argc,char* argv[]) {
QApplication app(argc,argv); MyMainWindow w; w.show(); return app.exec(); } |
|
运行结果: 右键的时候出现菜单 |
3.关于QT中的MainWindow窗口,MenuBar,ToolBar,QuickTip等方面的知识点的更多相关文章
- Qt中的主窗口之菜单栏
1.Qt中的主窗口 主窗口为建立应用程序用户界面提供了一个框架 Qt开发平台中直接支持主窗口的概念 QMainWindow是Qt中主窗口的基类 QMainWindow继承于QWidget是一种容器类型 ...
- Qt 中如何捕获窗口停用和激活的消息
最近一直在用Qt做一个简单的俄罗斯方块的游戏,由于要实现一个暂停游戏的功能,就是当鼠标移出正在运行的游戏,点击电脑桌面上的其他位置时,这个时候游戏暂停.在这里把实现过程简单的记录一下,作为一个学习笔记 ...
- Qt5:Qt中屏幕或窗口截图功能的实现
要想在Qt中实现屏幕或窗口截图功能 ,通常有两种方法: 1 -- 使用 QPixmap 类 2 -- 使用 QScreen类 然而虽然俩两种方法用到的类不相同,但是调用到的类成员函数的函数名称和参 ...
- Qt中如何固定窗口的大小?
这个是从网上转载过来的,我第一次看到的在如下网页:http://blog.csdn.net/cgb0210/article/details/5712980 这里我记录一下,留以后查阅. 一种方法是设 ...
- Qt中重绘制窗口方法:
void CircleWidget::paintEvent(QPaintEvent * event) { QPainter painter(this); int wight = this->wi ...
- Qt中常用知识点
1:QRegExp 正则表达式 QRegExp regExp("[a-zA-Z][1-9][0-9]{0,2}"); xxx->setValidator(new QRegEx ...
- 【C++/Qt】Qt中的parent形参
在 派生类的构造函数初始化列表中 调用 父类的带有参数的构造函数,是为了初始化从父类继承来的成员变量.因为这些变量无法直接初始化,只能采用这种方式初始化. 而在qt中,MainWindow中的某成员变 ...
- Qt中各个widget前后位置的设定(在Qt中,所有问题都要一分为二,QWidget体系和QGraphicsWidget体系)
这两天在总结一些以往project中遇到的问题,正好别组有同事问我关于Qt中各个widget窗口的前后位置是如何定义的,这里就总结一下: 在Qt中,所有问题都要一分为二,讨论两种不同的情况:一个是最常 ...
- QT中关闭应用程序和窗口的函数(quit(),exit()以及close()的区别)
使用QT编辑界面,其中带来很大方便的一点就是Qt中自带丰富的.种类齐全的类及其功能函数,程序员可以在编辑程序的过程中简单地直接调用.关于窗口关闭的操作,在这里指出常用的三个槽,即quit(),exit ...
随机推荐
- [HNOI 2001]求正整数
Description 对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m.例如:n=4,则m=6,因为6有4个不同整数因子1,2,3,6:而且是最小的有4个因子的整数. Input n ...
- hihoCoder 1595 : Numbers
Description You are given n constant integers c[1], c[2], ..., c[n] and an integer k. You are to ass ...
- ●POJ 1741 Tree
题链: http://poj.org/problem?id=1741题解: 树上点分治. 入门题,不多说了. 代码: #include<cstdio> #include<cstrin ...
- 洛谷mNOIP模拟赛Day1-分组
传送门 首先是贪心的思路 从后向前选,能多选就多选, 理由:数字越少肯定越优,同时间隔尽量向前推,字典序尽量小 对于K==1,枚举1~512直接判断 对于K==2,需要用镜像并查集,来刻画" ...
- hdu5631 BestCoder Round #73 (div.2)
Rikka with Graph Accepts: 123 Submissions: 525 Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- 使用JAXB解析xml文件(一)
1.java中解析xml的几种方式 1.1 JDK原生dom形式 原理:一次性把xml读入内存,在内存中构建成树形结构.优点:对节点操作方便,缺点:需要大量的内存空间,浪费资源 1.2 SAX形式 ...
- 使用word
同样这也是生活中常用到的办公软件,word本质是一个排版软件,它与一般的编辑器不同的是,它将整个文本分成了一页一页的,当然这也是方便于打印文档. 使用word还是很容易的,一般来说需要注意以下几方面的 ...
- C语言程序设计第三次作业——选择结构(一)
(一)改错题 错误信息: 错误原因:y=1/x后没加分号 改正方法:在其后加上分号 错误信息: 错误原因:if语句后接了:,使else语句找不到对应的if 改正方法:删掉if后的分号 错误信息: 错误 ...
- C语言程序设计第六次作业--循环结构(2)
(一)改错题 序列求和:输入一个正实数eps,计算序列部分和 1 - 1/4 + 1/7 - 1/10 + ... ,精确到最后一项的绝对值小于eps(保留6位小数). 输入输出样例: Input e ...
- Just for mysql
mysql的下载与安装 由于学校开设了数据库专业,并且最近准备在做一个web端的设计,虽然本人是负责前端(当然,前端技术也很LOW),但因种种原因,准备开始学习数据库相关的知识,以mysql为例. 昨 ...