Qt27アプリケーションのメインウィンドウ

ここに画像の説明を挿入

ここに画像の説明を挿入ここに画像の説明を挿入
ここに画像の説明を挿入
ここに画像の説明を挿入
ここに画像の説明を挿入ここに画像の説明を挿入
ここに画像の説明を挿入

MainWindow.h

#ifndef _MAINWINDOW_H_
#define _MAINWINDOW_H_
#include <QtGui/QMainWindow>
#include <QKeySequence>
#include <QMenuBar>
#include <QAction>
class MainWindow : public QMainWindow
{
    Q_OBJECT
private:
    MainWindow();//第一阶构造
  //  MainWindow(const MainWindow&);
  //  MainWindow& operator= (const MainWindow&);
    bool construct();//第二阶构造
    bool initMenuBar();
    bool initFileMenu(QMenuBar* mb);
    bool makeAction(QAction*& action, QString text, int key);
public:
    static MainWindow* NewInstance();//二阶构造入口
    ~MainWindow();
};
#endif // _MAINWINDOW_H_

MainWindow.cpp

#include "MainWindow.h"
#include <QMenu>
MainWindow::MainWindow()
{
}
//二阶构造入口
MainWindow* MainWindow::NewInstance()
{
    //第一阶构造函数
    MainWindow* ret = new MainWindow();
    //第二阶构造函数
    if( (ret == NULL) || !ret->construct() )
    {
        delete ret;
        ret = NULL;
    }
    return ret;
}
//第二阶构造函数
bool MainWindow::construct()
{
    bool ret = true;
    ret = ret && initMenuBar();
    return ret;
}
//创建菜单栏
bool MainWindow::initMenuBar()
{
    bool ret = true;
    //创建菜单栏
    QMenuBar* mb = menuBar();
    //创建下拉菜单
    ret = ret && initFileMenu(mb);
    return ret;
}
//创建下拉菜单
bool MainWindow::initFileMenu(QMenuBar* mb)
{
    //创建下拉菜单组对象, (&F):ALT+F键 可打开
    QMenu* menu = new QMenu("File(&F)");
    bool ret = (menu != NULL);
    if( ret )
    {
        //创建菜单项对象
        QAction* action = NULL;
        //创建 菜单项对象:New  快捷键:Qt::CTRL + Qt::Key_N
        ret = ret && makeAction(action, "New(N)", Qt::CTRL + Qt::Key_N);
        if( ret )
        {
            //将 菜单对象New 添加到 下拉菜单组QMenu中
            menu->addAction(action);    // add Action item to Menu
        }
        //创建分隔符
        menu->addSeparator();
        //创建 菜单项对象: Exit   快捷键:Qt::CTRL + Qt::Key_X
        ret = ret && makeAction(action, "Exit(X)", Qt::CTRL + Qt::Key_X);
        if( ret )
        {
            //将 菜单对象Exit 添加到 下拉菜单组QMenu中
            menu->addAction(action);    // add Action item to Menu
        }
    }
    if( ret )
    {
        //将 下拉菜单 添加到 QMenuBar当中去
        mb->addMenu(menu);    // add Menu add to application Menu Bar
    }
    else
    {
        delete menu;
    }
    return ret;
}
//创建 菜单项对象
bool MainWindow::makeAction(QAction*& action, QString text, int key)
{
    bool ret = true;
    //创建菜单项 对象
    action = new QAction(text, NULL);//暂时不指定父对象NULL
    if( action != NULL )
    {
        //设置快捷键
        action->setShortcut(QKeySequence(key));
    }
    else
    {
        ret = false;
    }
    return ret;
}
MainWindow::~MainWindow()
{
    
}

main.cpp

#include <QtGui/QApplication>
#include "MainWindow.h"
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;
}

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/LinuxArmbiggod/article/details/115269996