当前位置:网站首页>Main window in QT learning 27 application

Main window in QT learning 27 application

2022-07-07 07:59:00 A little black sauce

Qt Study 27 The main window in the application

The concept of the main window

  • The main window in the application

    • The main window is with the user Top level window for long-term interaction
    • programmatic Most functions Directly provided by the main window
    • The main window is usually The first window displayed after the application starts
    • The whole program consists of a main window and Multiple dialog boxes form
  • Qt The main window in

    • Qt The concept of main window is directly supported in the development platform
    • QMainWindow yes Qt The base class of the main window in
    • QMainWindow Inherited from QWidget It's a kind of Container type The components of

 Insert picture description here

  • QMainWindow The secret encapsulated in

 Insert picture description here

  • QMainWindow Component layout in

 Insert picture description here

  • stay Qt Provides menu related class components

 Insert picture description here

  • stay Qt Create a menu in the main window
QMenuBar* mb = menuBar();	// member function
QMenu* menu = new QMenu("File(&F)");
QAction* action = new QAction("New", NULL);
menu->addAction(action);
mb->addMenu(menu);

Programming experiment - Create a menu in the main window

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QAction>
#include <QMenu>

class MainWindow : public QMainWindow
{
    
    Q_OBJECT
private:
    MainWindow();

    bool construct();
    bool initMenuBar();
    bool initFileMenu(QMenuBar* mb);
    bool initEditMenu(QMenuBar* mb);
    bool initFormatMenu(QMenuBar* mb);
    bool initViewMenu(QMenuBar* mb);
    bool initHelpMenu(QMenuBar* mb);
    bool makeAction(QAction*& action, QString text, int key=0);
    bool myAddAction(QMenu* menu, QString text, int key=0);

public:

    static MainWindow* NewInstance();
    ~MainWindow();
};

#endif // MAINWINDOW_H
#include "MainWindow.h"
#include <QMenuBar>
#include <QMenu>

MainWindow::MainWindow()
{
    

}

bool MainWindow::construct()
{
    
    bool ret = true;

    ret = initMenuBar();

    return ret;
}

bool MainWindow::initMenuBar()
{
    
    bool ret = true;

    QMenuBar* mb = menuBar();
    ret = ret && initFileMenu(mb);
    ret = ret && initEditMenu(mb);
    ret = ret && initFormatMenu(mb);
    ret = ret && initViewMenu(mb);
    ret = ret && initHelpMenu(mb);

    return ret;
}

bool MainWindow::initFileMenu(QMenuBar* mb)
{
    
    bool ret = true;

    QMenu* menu = new QMenu("File(&F)");
    ret = (menu != NULL);
    if (ret) {
    
        ret = ret && myAddAction(menu, "New(N)", Qt::CTRL + Qt::Key_N);
        ret = ret && myAddAction(menu, "New Window(W)", Qt::CTRL + Qt::SHIFT + Qt::Key_N);
        ret = ret && myAddAction(menu, "Open(O)", Qt::CTRL + Qt::Key_O);
        ret = ret && myAddAction(menu, "Save(S)", Qt::CTRL + Qt::Key_S);
        ret = ret && myAddAction(menu, "Save as(S)", Qt::CTRL + Qt::SHIFT + Qt::Key_S);
        menu->addSeparator();
        ret = ret && myAddAction(menu, "Page setting(U)");
        ret = ret && myAddAction(menu, "Print(P)", Qt::CTRL + Qt::Key_P);
        menu->addSeparator();
        ret = ret && myAddAction(menu, "Exit(X)", Qt::CTRL + Qt::Key_X);
    }
    if (ret) {
    
        mb->addMenu(menu);
    }
    else {
    
        delete menu;
    }

    return ret;
}

bool MainWindow::initEditMenu(QMenuBar *mb)
{
    
    bool ret = true;

    QMenu* menu = new QMenu("Edit(&E)");
    ret = (menu != NULL);
    if (ret) {
    
        ret = ret && myAddAction(menu, "Undo(U)", Qt::CTRL + Qt::Key_Z);
        ret = ret && myAddAction(menu, "Clip(T)", Qt::CTRL + Qt::Key_X);
        ret = ret && myAddAction(menu, "Copy(C)", Qt::CTRL + Qt::Key_C);
        ret = ret && myAddAction(menu, "Stick(C)", Qt::CTRL + Qt::Key_V);
        ret = ret && myAddAction(menu, "Delete(L)", Qt::Key_Delete);
        menu->addSeparator();
        ret = ret && myAddAction(menu, "Use Bing search", Qt::CTRL + Qt::Key_E);
        ret = ret && myAddAction(menu, "Find(F)", Qt::CTRL + Qt::Key_F);
        ret = ret && myAddAction(menu, "Find next(N)", Qt::Key_F3);
        ret = ret && myAddAction(menu, "Find pre(V)", Qt::SHIFT + Qt::Key_F3);
        ret = ret && myAddAction(menu, "Replace(R)", Qt::CTRL + Qt::Key_H);
        ret = ret && myAddAction(menu, "Replace(R)", Qt::CTRL + Qt::Key_H);
        ret = ret && myAddAction(menu, "Jump(G)", Qt::CTRL + Qt::Key_G);
        menu->addSeparator();
        ret = ret && myAddAction(menu, "Select all(A)", Qt::CTRL + Qt::Key_A);
        ret = ret && myAddAction(menu, "Data(D)", Qt::Key_F5);
    }
    if (ret) {
    
        mb->addMenu(menu);
    }
    else {
    
        delete menu;
    }

    return ret;
}

bool MainWindow::initFormatMenu(QMenuBar *mb)
{
    
    bool ret = true;

    QMenu* menu = new QMenu("Format(&O)");
    ret = (menu != NULL);
    if (ret) {
    
        ret = ret && myAddAction(menu, "Auto wrap(W)");
        ret = ret && myAddAction(menu, "Font(F)");
    }
    if (ret) {
    
        mb->addMenu(menu);
    }
    else {
    
        delete menu;
    }

    return ret;
}

bool MainWindow::initViewMenu(QMenuBar *mb)
{
    
    bool ret = true;

    QMenu* menu = new QMenu("View(&V)");
    ret = (menu != NULL);
    if (ret) {
    
        ret = ret && myAddAction(menu, "Zoom(Z)");
        ret = ret && myAddAction(menu, "Status bar(S)");
    }
    if (ret) {
    
        mb->addMenu(menu);
    }
    else {
    
        delete menu;
    }

    return ret;
}

bool MainWindow::initHelpMenu(QMenuBar *mb)
{
    
    bool ret = true;

    QMenu* menu = new QMenu("Help(&H)");
    ret = (menu != NULL);
    if (ret) {
    
        ret = ret && myAddAction(menu, "View Help(H)");
        ret = ret && myAddAction(menu, "Send feedback(F)");
        ret = ret && myAddAction(menu, "About App(A)");
    }
    if (ret) {
    
        mb->addMenu(menu);
    }
    else {
    
        delete menu;
    }

    return ret;
}

bool MainWindow::makeAction(QAction *&action, QString text, int key)
{
    
    bool ret = true;

    action = new QAction(text, NULL);
    if (action != NULL) {
    
        if (key != 0) {
    
            action->setShortcut(QKeySequence(key));
        }
    }
    else {
    
        ret = false;
    }

    return ret;
}

bool MainWindow::myAddAction(QMenu *menu, QString text, int key)
{
    
    bool ret = true;

    QAction* action = NULL;
    ret = makeAction(action, text, key);
    if (ret) {
    
        menu->addAction(action);
    }

    return ret;
}

MainWindow *MainWindow::NewInstance()
{
    
    MainWindow* ret = new MainWindow();

    if ((ret == NULL) || !ret->construct()) {
    
        delete ret;
        ret = NULL;
    }

    return ret;
}

MainWindow::~MainWindow()
{
    

}
#include "MainWindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    
    QApplication a(argc, argv);
    MainWindow* w = MainWindow::NewInstance();
    int ret = -1;
    if (w != NULL) {
    
        w->show();
        ret = a.exec();
    }

    return ret;
}

Tips

  • Shortcut key settings

    action->setShort(QKeySequence(KEY))

    QKeySequence - Qt China and Shortcut key Related to the class

    KEY - Qt Chinese stands for Key value The constant

Summary

  • The main window is with the user Top level window for long-term interaction
  • The main window is usually The first window displayed after the application starts
  • QMainWindow yes Qt The base class of the main window in
  • QMainWindow It's a kind of Container type Window components
  • QMainWindow The menu bar is encapsulated in , The toolbar , Status bar and other components
原网站

版权声明
本文为[A little black sauce]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070431162067.html