窗口1 -> 应用程序的主窗口(27)

窗口1 -> 应用程序的主窗口(27)

主窗口是应用程序启动后显示的第一个窗口
QMainWindow中封装了菜单栏,工具栏,中心组件,停靠组件,状态栏等组件

菜单点击后会有很多选项。

整个程序由一个主窗口和多个对话框组成。

Menu Bar
Toolbars
Dock Widgets
Central Widget

菜单栏(QMenuBar)
下拉菜单组(QMenu)
菜单项(QAction)


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)
{
    QMenu* menu = new QMenu("File(&F)");
    bool ret = (menu != NULL);

    if (ret) {
        QAction* action = NULL;

        ret = ret && makeAction(action, "New(N)", Qt::CTRL + Qt::Key_N);

        if( ret )
        {
            menu->addAction(action);    // add Action item to Menu
        }

        menu->addSeparator();

        ret = ret && makeAction(action, "Exit(X)", Qt::CTRL + Qt::Key_X);

        if(ret) 
        {
            menu->addAction(action);    // add Action item to Menu
        }
    }

    if( ret )
    {
        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);

    if( action != NULL )
    {
        action->setShortcut(QKeySequence(key));
    }
    else
    {
        ret = false;
    }

    return ret;
}

MainWindow::~MainWindow()
{
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值