• QTQMainWindow的菜单、工具栏、状态栏、浮动窗口、中心部件


     main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

      mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    };
    
    #endif // MAINWINDOW_H
    

        mainwindow.cpp

    #include "mainwindow.h"
    #include <QMenuBar>
    #include <QToolBar>
    #include <QDebug>
    #include <QPushButton>
    #include <QStatusBar>
    #include <QLabel>
    #include <QDockWidget>
    #include <QTextEdit>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        //重置窗口大小
        resize(600,400);
    
        //菜单栏  只能最多有一个
        //菜单栏创建
        QMenuBar * bar = menuBar();
        //将菜单栏放入到窗口中
        setMenuBar(bar);
    
        //创建菜单
        QMenu * fileMenu = bar->addMenu("文件");
        QMenu * editMenu = bar->addMenu("编辑");
    
        //创建菜单项
        QAction * newAction = fileMenu->addAction("新建");
        //添加分割线
        fileMenu->addSeparator();
        QAction * openAction = fileMenu->addAction("打开");
    
    
    
    
        //工具栏  可以有多个
        QToolBar * toolBar = new QToolBar(this);
        addToolBar(Qt::LeftToolBarArea,toolBar);
    
        //后期设置 只允许 左右停靠
        toolBar->setAllowedAreas( Qt::LeftToolBarArea | Qt::RightToolBarArea );
    
        //设置浮动
        toolBar->setFloatable(false);
    
        //设置移动 (总开关)
        toolBar->setMovable(false);
    
        //工具栏中可以设置内容
        toolBar->addAction(newAction);
        //添加分割线
        toolBar->addSeparator();
        toolBar->addAction(openAction);
        //工具栏中添加控件
        QPushButton * btn = new QPushButton("aa" , this);
        toolBar->addWidget(btn);
    
    
        //状态栏 最多有一个
        QStatusBar * stBar = statusBar();
        //设置到窗口中
        setStatusBar(stBar);
        //放标签控件
        QLabel * label = new QLabel("提示信息",this);
        stBar->addWidget(label);
    
        QLabel * label2 = new QLabel("右侧提示信息",this);
        stBar->addPermanentWidget(label2);
    
        //铆接部件 (浮动窗口) 可以有多个
        QDockWidget * dockWidget = new QDockWidget("浮动",this);
        addDockWidget(Qt::BottomDockWidgetArea,dockWidget);
        //设置后期停靠区域,只允许上下
        dockWidget->setAllowedAreas( Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea );
    
    
        //设置中心部件 只能一个
        QTextEdit * edit = new QTextEdit(this);
        setCentralWidget(edit);
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    

         mainwindow

  • 相关阅读:
    字符串,format格式化及列表的相关进阶操作---day07
    利用wiile双层循环打印各种星星---day06
    双层循环练习,pass_break_continue,和for循环---day06
    类型判断,代码块,流程控制及循环---day05
    频繁项集算法
    Unity 物体移动的理解
    Game1---游戏设计
    精读Hadamard Response论文
    java 创建线程
    Unity游戏开发面试基础知识
  • 原文地址:https://www.cnblogs.com/wuyuan2011woaini/p/16106675.html
Copyright © 2020-2023  润新知