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. [JLOI 2011]飞行路线&[USACO 09FEB]Revamping Trails

    Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...

  2. LOJ #6119. 「2017 山东二轮集训 Day7」国王

    Description 在某个神奇的大陆上,有一个国家,这片大陆的所有城市间的道路网可以看做是一棵树,每个城市要么是工业城市,要么是农业城市,这个国家的人认为一条路径是 exciting 的,当且仅当 ...

  3. POJ2135:Farm Tour

    题意:给定一个无向图,从1走到n再从n走回1,每个边只能走一遍,求最短路 题解:可以定义一个源点s,和一个汇点t s和1相连容量为2,费用为0, t和n相连容量为2,费用为0 然后所用的边的容量都定为 ...

  4. hdu 5439(找规律)

    The sequence is generated by the following scheme. 1. First, write down 1, 2 on a paper. 2. The 2nd ...

  5. bzoj2655calc 容斥+dp

    2655: calc Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 322  Solved: 197[Submit][Status][Discuss] ...

  6. 笔记11 在XML中声明切面(2)

    为通知传递参数 1.声明一个CompactDiscs接口.内部包含两个方法: show() 用于显示唱片的名字和艺术风格 playTrack(int number) 根据传入的磁道数播放相应磁道的音乐 ...

  7. Union和Union All 的区别

    Union和Union All 的区别: Union 是对结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All,对两个结果集进行并集操作,包括重复行,不进行排序: Inter ...

  8. TRIM ,LTRIM ,RTRIM ,空格过滤

  9. sssp-springmvc+spring+spring-data-jpa增删改查

    环境:IDE:eclipse.jdk1.7.mysql5.7.maven 项目结构图 上面目录结构你可以自己创建 搭建框架 首先加入maven依赖包以及相关插件 <dependencies> ...

  10. java如何获得数据库表中各字段的字段名

    public class TestDemo { public static Connection getConnection() { Connection conn = null; try { Cla ...