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 ...
随机推荐
- bzoj 1925: [Sdoi2010]地精部落
Description 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi, ...
- bzoj1597[Usaco2008 Mar]土地购买 斜率优化dp
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5524 Solved: 2074[Submit] ...
- c++ primer第15章这几个例子中的构造函数形式不太理解
//向基类构造函数传递实参p491 class Bulk_item : public Item_base{ public: Bulk_item(,double disc_rate = 0.0): It ...
- Weekly Contest 75题解
Q1. Rotate String(796) We are given two strings, A and B. A shift on A consists of taking string A a ...
- 批量录入快递地址-快宝地址服务(PHP代码示例)
快递地址写错了怎么办?快递地址写的不详细怎么办?怎么皮批量录入收件人地址?微商怎么批量录入发件人地址?快宝地址清洗,有效的解决了寄送快递时,批量录入收件人信息.发件人信息时,纠正地址数据,不完整地址识 ...
- WPF ViewModel与多个View绑定后如何解决的问题
当重复创建View并绑定同一个ViewModel后,ViewModel中的字段更新,在新的View中的没有反应或者在View中找不到相应的视觉树(如ListBox的ListBoxItem) 初始的解决 ...
- UDP网络编程
概念: UDP协议(用户数据报协议)是无连接,不可靠的,无序的.速度比较快, UDP协议以数据报作为数据传输的载体 进行数据传输时,首先将传输的数据定义成数据报(Datagram),在数据报中指明数据 ...
- Helm 架构 - 每天5分钟玩转 Docker 容器技术(161)
在实践之前,我们先来看看 Helm 的架构. Helm 有两个重要的概念:chart 和 release. chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板.参数定 ...
- Go 语言条件语句
条件语句需要开发者通过指定一个或多个条件,并通过测试条件是否为 true 来决定是否执行指定语句,并在条件为 false 的情况在执行另外的语句. 下图展示了程序语言中条件语句的结构: Go 语言提供 ...
- 用python爬了自己的微信,原来好友都是这样的!
偶然了解到Python里的itchat包,它已经完成了wechat的个人账号API接口,使爬取个人微信信息更加方便.鉴于自己很早之前就想知道诸如自己微信好友性别比例都来自哪个城市之类的问题,于是乎玩心 ...