Qt_设置菜单栏、状态栏、状态栏、核心控件、浮动窗口

头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H

源文件
main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp:

#include "mainwindow.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //菜单栏、工具栏、状态栏、核心控件、浮动窗口
    setWindowTitle(QStringLiteral("制作一个菜单窗口"));
    resize(500,500);

    QMenuBar *mBar = menuBar();

    QMenu *pFile = mBar->addMenu(QStringLiteral("文件"));

    QAction *pNew = pFile->addAction(QStringLiteral("新建"));
    connect(pNew, &QAction::triggered,
            [=]()
            {
                qDebug() << QStringLiteral("新建被按下");
            }
            );
    pFile->addSeparator();
    QAction *pOpen = pFile->addAction(QStringLiteral("打开"));
    connect(pOpen, &QAction::triggered,
            [=]()
            {
                qDebug() << QStringLiteral("打开被按下");
            }
            );
    //工具栏
    QToolBar *toolBar = addToolBar("toolBar");

    toolBar->addAction(pNew);
    toolBar->addAction(pOpen);

//    QPushButton *b = new QPushButton(toolBar);
//    b->move(100,0);
//    b->resize(40,20);
    QPushButton *b = new QPushButton(this);
    b->setText(QStringLiteral("新建"));
    toolBar->addWidget(b);
    connect(b, &QPushButton::clicked,
            [=]()
            {
                b->setText(QStringLiteral("打开"));
            }
            );
    //状态栏
    QStatusBar *sBar = statusBar();
    QLabel *label = new QLabel(this);
    label->setText("Welconme to here!");
    sBar->addWidget(label);
    sBar->addWidget(new QLabel("MM",this));

    sBar->addPermanentWidget(new QLabel("Author:K&J",this));

    //核心控件
    QTextEdit *textEdit = new QTextEdit(this);
    setCentralWidget(textEdit);

    //浮动窗口
    QDockWidget *dockWidget = new QDockWidget(this);
    addDockWidget(Qt::RightDockWidgetArea,dockWidget);

    QTextEdit *textEdit1 = new QTextEdit(this);
    dockWidget->setWidget(textEdit1);

}

MainWindow::~MainWindow()
{

}

Qt_设置菜单栏、状态栏、状态栏、核心控件、浮动窗口_第1张图片

你可能感兴趣的:(Qt自学,qt5)