Qt中MainWindow類例項
阿新 • • 發佈:2018-11-12
轉載 : https://blog.csdn.net/linux_ever/article/details/50120617
QMainWindow有選單欄,工具欄,狀態列;
可以將QWidget作為QMainWindow的中間部位的一個部件;
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #include "myview.h"//在mainwindow上再新增一個視窗 #include <QSystemTrayIcon>//托盤 class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); void paintEvent(QPaintEvent *); void mousePressEvent(QMouseEvent *); bool event(QEvent *); QLabel * _label; MyView * _view;//把要顯示的東西都放在這個視窗部件裡面;mainWindow起到封裝的作用 QMenu * _menu;//用於右鍵彈出選單 QSystemTrayIcon * _icon;//托盤圖示 void slotActivated(QSystemTrayIcon::ActivationReason); signals: public slots: void slotOpen(); void slotSave(); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include <QApplication> #include <QMenu>//選單類 #include <QMenuBar>//選單欄 #include <QAction>//選單項,是一個可以觸發的東西 #include <QFileDialog> #include <QDebug> #include <QToolBar> #include <QStatusBar> #include <QPainter> #include <QPixmap> #include <QMouseEvent> #include <QCursor>//滑鼠當前的位置 #include <QIcon> #include <QEvent> MainWindow::MainWindow(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(slotSave()), QKeySequence::Save); menu->addSeparator(); QAction *closeAction = menu->addAction("&Exit", this, SLOT(close()), QKeySequence::Close); /*新增工具欄*/ QToolBar *toolBar = this->addToolBar("MyToolBar");//新增一個工具欄 toolBar->addAction(openAction); toolBar->addAction(saveAction); toolBar->addAction(closeAction); /*新增狀態列*/ QStatusBar *pstatusBar = this->statusBar(); pstatusBar->addWidget(_label = new QLabel("OK")); _label->setText("<font color = red>Processing...</font>"); /*mainwindow中別的控制元件佔用了之後,剩下的區域都是CentralWidget的*/ _view = new MyView; this->setCentralWidget(_view); /*托盤*/ _icon = new QSystemTrayIcon; _icon->setIcon(QIcon("../1.png")); _icon->setToolTip("This is my tray test"); _icon->show();//顯示該托盤圖示 _icon->setContextMenu(menu); connect(_icon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); } //這個函式的功能也沒有實現 void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::Trigger){ if (this->isHidden()) this->show(); else this->isHidden(); } } //這個函式的功能沒有實現 bool MainWindow::event(QEvent *event) { if(event->type() == QEvent::Close){ return false; } return QMainWindow::event(event); } void MainWindow::paintEvent(QPaintEvent *) { QPainter p(this); p.drawPixmap(QPointF(0, 0),QPixmap("../1.png")); } void MainWindow::mousePressEvent(QMouseEvent *ev) { if(ev->button() == Qt::RightButton) _menu->exec(QCursor::pos()); } void MainWindow::slotSave() { QFileDialog::getSaveFileName(); } void MainWindow::slotOpen() { QString strFile = QFileDialog::getOpenFileName(); qDebug() << strFile; } int main(int argc, char **argv) { QApplication app(argc, argv); MainWindow w; w.setGeometry(300, 130, 700, 500); w.show(); app.exec(); }
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); }