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 = 0);
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 = 0);
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();
}
|
运行结果:
右键的时候出现菜单
|