QT浮动窗口和菜单项绑定显示和隐藏

 

 

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDockWidget>
#include <QLabel>
#include <QMessageBox>
#include <QStackedWidget>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QMenuBar *mBar = this->menuBar();//菜单栏
    QMenu *window = mBar->addMenu("窗体");
    QAction *docktoggles_ = new QAction("浮动窗口显示") ;//浮动框显示菜单项
    window->addAction(docktoggles_);

    //创建一个QDockWidget浮动框
    QDockWidget * toolsettings_ = new QDockWidget(this);
    toolsettings_->setWindowTitle("显示浮动框");
    toolsettings_->setObjectName("toolSetting");
    toolsettings_->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    addDockWidget(Qt::RightDockWidgetArea, toolsettings_); //右侧笔刷设置窗口

    QStackedWidget * widgets_ = new QStackedWidget(this);
    toolsettings_->setWidget(widgets_);
     widgets_->addWidget(new QLabel("内容",widgets_));

    //创建一个子菜单 作为浮动框显示菜单项的子菜单
    QMenu *togglesMenu = new QMenu(this);
    togglesMenu->addAction(toolsettings_->toggleViewAction()); //加入这个 可以设置勾选显示

    docktoggles_->setMenu(togglesMenu);//把子菜单加入到docktoggles_ setMenu就是为QAction增加QMenu子菜单

    setCentralWidget(new QLabel("中间内容"));
}

 

posted @ 2023-03-09 14:53  麦田HH  阅读(536)  评论(0编辑  收藏  举报