Qt中MainWindow类实例

QMainWindow有菜单栏,工具栏,状态栏;

可以将QWidget作为QMainWindow的中间部位的一个部件;


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include "myview.h"//在mainwindow上再添加一个窗口
#include //托盘

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 
#include //菜单类
#include //菜单栏
#include //菜单项,是一个可以触发的东西
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include //鼠标当前的位置
#include 
#include 
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("Processing...");

    /*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 

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 

MyView::MyView(QWidget *parent) :
    QWidget(parent)
{
}


void MyView::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.fillRect(rect(), Qt::red);
}

Qt中MainWindow类实例_第1张图片

Qt中MainWindow类实例_第2张图片

你可能感兴趣的:(Qt)