3.关于QT中的MainWindow窗口,MenuBar,ToolBar,QuickTip等方面的知识点
生活随笔
收集整理的這篇文章主要介紹了
3.关于QT中的MainWindow窗口,MenuBar,ToolBar,QuickTip等方面的知识点
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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>? //通過這個頭文件可以讓程序在狀態(tài)欄顯示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>"); ? ??? /* 別的控件占用了之后,剩下的區(qū)域都是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(); } |
| 運行結(jié)果:
右鍵的時候出現(xiàn)菜單
|
?
總結(jié)
以上是生活随笔為你收集整理的3.关于QT中的MainWindow窗口,MenuBar,ToolBar,QuickTip等方面的知识点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2.关于QT中的Dialog(模态窗口)
- 下一篇: 军人配偶出轨军人算破坏军婚吗