• 第二十九课、主窗口中的状态栏------------------狄泰软件学院


    一、主窗口中的状态栏

    1、状态栏的概念和意义

    (1)、状态栏是应用程序中输出简要信息的区域

    (2)、状态栏一般位于主窗口的最底部

    (3)、状态栏中的消息类型

    A、实时消息:如当前程序状态

    B、永久消息:如程序版本号,机构名称

    C、进度消息:如进度条提示,百分百提示

    2、Qt提供了与状态栏相关的类组件

    3、在主窗口中创建状态栏

    4、Qt状态栏的设计原则

    (1)、左边区域用于输出实时消息

    (2)、右边区域用于设置永久消息

    (3)、addWidget在状态栏的左半部分添加组件

    (4)、addPermanentWidget在状态栏右半部分添加组件

    1、初探状态栏

    #include "MainWindow.h"
    #include <QStatusBar>
    #include <QLabel>
    #include <QPushButton>
    #include <QLineEdit>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
    /*******************************1、创建状态栏*****************************************/
        QStatusBar* sb = statusBar();
    /*******************************2、创建组件*****************************************/
        QLabel* label = new QLabel("Label");
        QLineEdit* edit = new QLineEdit();
        QPushButton* Btn = new QPushButton();
    
    /*******************************3、将组件添加到状态栏*****************************************/    
        sb->addPermanentWidget(label);
        sb->addPermanentWidget(edit);
        sb->addPermanentWidget(Btn);//显示在右边
    
        sb->showMessage("Hello Qt");//在左边
    }
    
    MainWindow::~MainWindow()
    {
        
    }
    MainWindow.cpp

    2、NotePad

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMenuBar>
    #include <QMenu>
    #include <QAction>
    #include <QString>
    #include <QtGui/QMainWindow>
    #include <QToolBar>
    #include <QIcon>
    #include <QSize>
    #include <QStatusBar>
    #include <QLabel>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    private:
            MainWindow(QWidget *parent = 0);
            MainWindow(const MainWindow& obj);
            MainWindow* operator = (const MainWindow& obj);
            bool construct();
    
            bool initMenuBar();//菜单栏
            bool initToolBar();//工具栏
            bool initStatusBar();//状态栏
    
            bool initFileMenu(QMenuBar* mb);//文件菜单
            bool initEditMenu(QMenuBar* mb);//编辑菜单
            bool initFormatMenu(QMenuBar* mb);//格式菜单
            bool initViewMenu(QMenuBar* mb);//视图菜单
            bool initHelpMenu(QMenuBar* mb);//帮助菜单
            bool initFileToolItem(QToolBar* tb);
    
            bool makeAction(QAction*& action, QString text, int ket);//菜单项
            bool makeAction(QAction*& action, QString tip, QString icon);
    public:
         static MainWindow* NewInstance();
        ~MainWindow();
    };
    
    #endif // MAINWINDOW_H
    MainWindow.h
    #include "MainWindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
    }
    
    bool MainWindow::construct()
    {
        bool ret = true;
        ret = ret && initMenuBar();
        ret = ret && initToolBar();
        ret = ret && initStatusBar();
        return ret;
    }
    MainWindow* MainWindow::NewInstance()
    {
        MainWindow* ret = new MainWindow();
    
        if((ret==NULL) || (!ret->construct()))
        {
            delete ret;
            ret = NULL;
        }
    
        return ret;
    }
    bool MainWindow::initMenuBar()//菜单栏
    {
        bool ret = true;
    
        QMenuBar* mb = menuBar();//一定要注意是menuBar(),这是普通成员函数,不是构造函数
    
        ret = ret && initFileMenu(mb);//传一个参数是为了在initFileMenu()函数将menu加入菜单栏
        ret = ret && initEditMenu(mb);
        ret = ret && initFormatMenu(mb);
        ret = ret && initViewMenu(mb);
        ret = ret && initHelpMenu(mb);
    
        return ret;
    
    }
    
    bool MainWindow::initToolBar()//工具栏
    {
        bool ret = true;
    
        QToolBar* tb = addToolBar("Tool Bar");
        tb->setMovable(false);
        tb->setFloatable(false);
        tb->setIconSize(QSize(16,16));
    
        ret = ret && initFileToolItem(tb);
    
        return ret;
    }
    
    bool MainWindow::initStatusBar()//状态栏
    {
        bool ret = true;
    
        QStatusBar* sb = statusBar();
    
        QLabel* label = new QLabel("Made By LGC");
    
        if(label != NULL)
        {
            label->setMinimumWidth(200);
            label->setAlignment(Qt::AlignHCenter);
            sb->addPermanentWidget(label);
        }
        else
        {
            ret = false;
        }
        return ret;
    }
    
    /************************************************文件菜单********************************************************/
    bool MainWindow::initFileMenu(QMenuBar* mb)
    {
        bool ret = true;
    
        QMenu* menu = new QMenu("File(&F)");//创建文件菜单,(&F)是为了可以Alt+F打开
        ret = (menu != NULL);
        if(ret)
        {
            QAction* action = NULL;
    
            //New
            ret = ret &&  makeAction(action, "New(&N)",Qt::CTRL + Qt::Key_N);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Open
            ret = ret &&  makeAction(action, "Open(&O)...",Qt::CTRL + Qt::Key_O);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Save
            ret = ret &&  makeAction(action, "Save(&S)",Qt::CTRL + Qt::Key_S);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Save As
            ret = ret &&  makeAction(action, "Save As(&A)...",0);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //print
            ret = ret &&  makeAction(action, "Print(&P)...",Qt::CTRL + Qt::Key_P);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Exit
            ret = ret &&  makeAction(action, "Exit(&X)",0);
            if(ret)
            {
               menu->addAction(action);//将菜单项加入到菜单
            }
    
        }
        if(ret)
        {
            mb->addMenu(menu);//将菜单加入到菜单栏
        }
        else
        {
            delete mb;
        }
        return ret;
    }
    
    /************************************************编辑菜单********************************************************/
    bool MainWindow::initEditMenu(QMenuBar* mb)
    {
        bool ret = true;
    
        QMenu* menu = new QMenu("Edit(&E)");
        ret = (menu != NULL);
        if(ret)
        {
            QAction* action = NULL;
    
            //Undo
            ret = ret &&  makeAction(action, "Undo(&U)",Qt::CTRL + Qt::Key_Z);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Redo
            ret = ret &&  makeAction(action, "Redo(&R)...",Qt::CTRL + Qt::Key_Y);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Cut
            ret = ret &&  makeAction(action, "Cut(&T)",Qt::CTRL + Qt::Key_X);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Copy
            ret = ret &&  makeAction(action, "Copy(&C)...",Qt::CTRL + Qt::Key_C);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Pase
            ret = ret &&  makeAction(action, "Pase(&P)...",Qt::CTRL + Qt::Key_V);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Delete
            ret = ret &&  makeAction(action, "Delete(&L)",Qt::Key_Delete);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Find
            ret = ret &&  makeAction(action, "Find(&F)...",Qt::CTRL + Qt::Key_F);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Replace
            ret = ret &&  makeAction(action, "Replace(&R)...",Qt::CTRL + Qt::Key_H);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Goto
            ret = ret &&  makeAction(action, "Goto(&G)",Qt::CTRL + Qt::Key_G);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Select All
            ret = ret &&  makeAction(action, "Select All(&A)",Qt::CTRL + Qt::Key_A);
            if(ret)
            {
               menu->addAction(action);
            }
    
        }
        if(ret)
        {
            mb->addMenu(menu);
        }
        else
        {
            delete mb;
        }
        return ret;
    }
    
    /************************************************格式菜单********************************************************/
    bool MainWindow::initFormatMenu(QMenuBar* mb)
    {
        bool ret = true;
    
        QMenu* menu = new QMenu("Format(&O)");
        ret = (menu != NULL);
        if(ret)
        {
            QAction* action = NULL;
    
            //Auto Wrap
            ret = ret &&  makeAction(action, "Auto Wrap(&W)",0);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Font
            ret = ret &&  makeAction(action, "Font(&F)...",0);
            if(ret)
            {
               menu->addAction(action);
            }
    
        }
        if(ret)
        {
            mb->addMenu(menu);
        }
        else
        {
            delete mb;
        }
        return ret;
    }
    
    /************************************************视图菜单********************************************************/
    bool MainWindow::initViewMenu(QMenuBar* mb)
    {
        bool ret = true;
    
        QMenu* menu = new QMenu("View(&V)");
        ret = (menu != NULL);
        if(ret)
        {
            QAction* action = NULL;
    
            //Tool Bar
            ret = ret &&  makeAction(action, "Tool Bar(&T)",0);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //Status Bar
            ret = ret &&  makeAction(action, "Status Bar(&S)",0);
            if(ret)
            {
               menu->addAction(action);
            }
    
        }
        if(ret)
        {
            mb->addMenu(menu);
        }
        else
        {
            delete mb;
        }
        return ret;
    }
    
    /************************************************帮助菜单********************************************************/
    bool MainWindow::initHelpMenu(QMenuBar* mb)
    {
        bool ret = true;
    
        QMenu* menu = new QMenu("Help(&H)");
        ret = (menu != NULL);
        if(ret)
        {
            QAction* action = NULL;
    
            //User Manual
            ret = ret &&  makeAction(action, "User Manual",0);
            if(ret)
            {
               menu->addAction(action);
            }
    
            menu->addSeparator();
    
            //About NotePad
            ret = ret &&  makeAction(action, "About NotePad...",0);
            if(ret)
            {
               menu->addAction(action);
            }
    
        }
        if(ret)
        {
            mb->addMenu(menu);
        }
        else
        {
            delete mb;
        }
        return ret;
    }
    
    bool MainWindow::initFileToolItem(QToolBar* tb)
    {
        bool ret = true;
        QAction* action = NULL;
    
        ret = ret && makeAction(action, "New", ":/Res/pic/new.png");
        if(ret)
        {
            tb->addAction(action);
        }
    
        ret = ret && makeAction(action, "Open", ":/Res/pic/open.png");
        if(ret)
        {
            tb->addAction(action);
        }
    
        ret = ret && makeAction(action, "Save", ":/Res/pic/save.png");
        if(ret)
        {
            tb->addAction(action);
        }
    
        ret = ret && makeAction(action, "Save As", ":/Res/pic/saveas.png");
        if(ret)
        {
            tb->addAction(action);
        }
        ret = ret && makeAction(action, "Print", ":/Res/pic/print.png");
        if(ret)
        {
            tb->addAction(action);
        }
        ret = ret && makeAction(action, "Undo", ":/Res/pic/undo.png");
        if(ret)
        {
            tb->addAction(action);
        }
        ret = ret && makeAction(action, "Redo", ":/Res/pic/redo.png");
        if(ret)
        {
            tb->addAction(action);
        }
    
        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;
    }
    bool MainWindow::makeAction(QAction*& action, QString tip, QString icon)
    {
        bool ret = true;
        action = new QAction("", NULL);
        if(action != NULL)
        {
            action->setToolTip(tip);
            action->setIcon(QIcon(icon));
        }
        else
        {
            ret = false;
        }
        return ret;
    }
    MainWindow::~MainWindow()
    {
        
    }
    MainWindow.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();
        }
    
        delete w;
        return ret;
    }
    main.cpp

    二、小结

    (1)、状态栏是应用程序中输出简要信息的区域

    (2)、QStatusBar是Qt中创建状态栏组件的类

    (3)、QStatusBar中可以任意添加QWidget

    (4)、QStatusBar有自己内置的设计原则

    (5)、QStatusBar可以定制出各种形式的状态栏

     

  • 相关阅读:
    jQuery Ajax 方法调用 Asp.Net WebService 的详细例子(原创)
    jQuery 访问WebService 返回复合类型列表
    Vista Media Center 开发之深入浅出 (一) Vista Media Center开发环境的搭建
    安装一个媒体解码器让 Windows Media Player 支持更多媒体格式
    静静期待 Windows 7 的到来
    集成 RealTek 声卡 在 Windows 7 有杂音、爆音的解决方法
    使用jQuery for Asp.Net 我的开发环境配置
    Windows 7 VHD 启动
    建立一个 C#.Net Windows Service 程序
    Windows server 2008 r2 简体中文180天评估版微软官方下载地址
  • 原文地址:https://www.cnblogs.com/gui-lin/p/6407850.html
Copyright © 2020-2023  润新知