1
新建一个空Qt项目

编写12MainWindow.pro

HEADERS
+=
\

MyMainWindow.h
\

MyView.h

SOURCES
+=
\

MyMainWindow.cpp
\

MyView.cpp

QT
+=
gui widgets

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等方面的知识点的更多相关文章

  1. Qt中的主窗口之菜单栏

    1.Qt中的主窗口 主窗口为建立应用程序用户界面提供了一个框架 Qt开发平台中直接支持主窗口的概念 QMainWindow是Qt中主窗口的基类 QMainWindow继承于QWidget是一种容器类型 ...

  2. Qt 中如何捕获窗口停用和激活的消息

    最近一直在用Qt做一个简单的俄罗斯方块的游戏,由于要实现一个暂停游戏的功能,就是当鼠标移出正在运行的游戏,点击电脑桌面上的其他位置时,这个时候游戏暂停.在这里把实现过程简单的记录一下,作为一个学习笔记 ...

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

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

  4. Qt中如何固定窗口的大小?

    这个是从网上转载过来的,我第一次看到的在如下网页:http://blog.csdn.net/cgb0210/article/details/5712980  这里我记录一下,留以后查阅. 一种方法是设 ...

  5. Qt中重绘制窗口方法:

    void CircleWidget::paintEvent(QPaintEvent * event) { QPainter painter(this); int wight = this->wi ...

  6. Qt中常用知识点

    1:QRegExp 正则表达式 QRegExp regExp("[a-zA-Z][1-9][0-9]{0,2}"); xxx->setValidator(new QRegEx ...

  7. 【C++/Qt】Qt中的parent形参

    在 派生类的构造函数初始化列表中 调用 父类的带有参数的构造函数,是为了初始化从父类继承来的成员变量.因为这些变量无法直接初始化,只能采用这种方式初始化. 而在qt中,MainWindow中的某成员变 ...

  8. Qt中各个widget前后位置的设定(在Qt中,所有问题都要一分为二,QWidget体系和QGraphicsWidget体系)

    这两天在总结一些以往project中遇到的问题,正好别组有同事问我关于Qt中各个widget窗口的前后位置是如何定义的,这里就总结一下: 在Qt中,所有问题都要一分为二,讨论两种不同的情况:一个是最常 ...

  9. QT中关闭应用程序和窗口的函数(quit(),exit()以及close()的区别)

    使用QT编辑界面,其中带来很大方便的一点就是Qt中自带丰富的.种类齐全的类及其功能函数,程序员可以在编辑程序的过程中简单地直接调用.关于窗口关闭的操作,在这里指出常用的三个槽,即quit(),exit ...

随机推荐

  1. epoll源码分析(转)

    在create后会创建eventpoll对象保存在一个匿名fd的file struct的private指针中,然后进程睡在等待队列上面. 对于等待的fd,通过poll机制在准备好之后会调用相应的cal ...

  2. [模版]平衡树splay2

    题目描述 1. 加入:一个新的成员加入同好会,我会分配给他一个没有使用的id,并且询问他的兴趣值val. 2. 修改:id在区间[a,b]内的成员,兴趣值同时改变k,k有可能是负数,表示他们失去了对同 ...

  3. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  4. bzoj2006 NOI2010 数据结构+堆维护区间和最大

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 3431  Solved: 1686[Submit][Statu ...

  5. Windows、Unix、Linux是什么类型的操作系统?

    Windows:具有图形用户界面的视窗操作系统. Unix:多用户分时操作系统. Linux:类似Unix操作系统,用于个人计算机.

  6. python中的缩进问题

    python中没有{}来表示代码块,而是用缩进来表示,刚开始写python代码,没有注意缩进,结果各种报错(( ╯□╰ )). 在python中的原则就是同一层次的代码一定要有相同的缩进!!! 从上图 ...

  7. 在浏览器中运行Keras模型,并支持GPU

    Keras.js 推荐一下网页上的 demo https://transcranial.github.io/keras-js/#/ 加载的比较慢,但是识别的非常快. Run Keras models ...

  8. CNN中的经典结构之AlexNet

    AlexNet的基本结构 Alexnet是由5个卷积层和三个全连接层组成,一共8个权重层(池化层不是权重层因为其没有参数),其中ReLU激活函数作用在每个卷积层和全连接层上,在第一个卷积层和第二个卷积 ...

  9. python显示中文出错以及抛出UnicodeDecodeError的处理办法

    程序开头粘贴如下代码即可: # -*- coding:utf-8 -*- import sys default_encoding = 'utf-8' if sys.getdefaultencoding ...

  10. 通过ajax和spring 后台传输json数据

    在通过ajax从页面向后台传数据的时候,总是返回415(Unsupported media type)错误,后台无法获取数据.如下图所示: 在尝试解决这个问题的时候,我们首先要理解一下概念: @req ...