我有一个 QMainWindow,左侧包含一个 QTreeWidget,右侧包含另一个 QMainWindow,其中 windowFlags 设置为 Qt::Widget。
这是因为 QMainWindow 是向 QWidget 提供停靠功能的唯一方法。(如果需要,我希望停靠的组件完全从“真实”窗口中弹出。
我的问题是我希望用户能够保持弹出的停靠小部件,即使他们的项目未在左侧选择。
例如,这里是全局布局:
假设我选择了第 1 项。在右侧,我将有一些可停靠的小部件,我可以根据需要重新排序。如果我弹出一个来关注它,如果我选择第 2 项,我不希望它消失。
更进一步,如果我愿意,我可能想一次显示所有项目的可停靠小部件。
我最初的想法是让每个项目都将其专用的 QMainWindow 存储在其数据中,我只需切换正确的 QMainWindow 以反映当前活动的项目。
也许我想要的是一个坏主意,也许它甚至不可行。
有一些 Qt 知识的人可以告诉我我是否在做/想要做错什么吗?
编辑:
如果有办法手动触发“内部 QMainWindow”弹出,那对我来说非常好。例如,右上角的“弹出”按钮将完全弹出并使其成为一个全新的窗口(但仍链接到另一个窗口)
编辑2:
我想指出,我还没有尝试过关于这个问题的任何事情。我基本上想知道它是否符合 Qt 的做事方式。
这个问题让我很高兴能够只为程序的一部分提供对接功能,但我仍然不确定我想要什么。我真的能做到这一点吗?
编辑7,MVCE:
希望我没有忘记任何事情,因为这是通过修改我的文件来完成的。
主窗口2.cpp
#include "mainwindow2.hh"
#include "ui_mainwindow2.h"
#include <QTreeWidgetItem>
MainWindow2::MainWindow2(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow2)
{
ui->setupUi(this);
ui->mainPanel->setWindowFlags(Qt::Widget);
QTreeWidgetItem* item = new QTreeWidgetItem;
item->setData(0, 0, QVariant::fromValue(QString("Item 1")));
ui->accountsTreeWidget->addTopLevelItem(item);
QTreeWidgetItem* item2 = new QTreeWidgetItem;
item2->setData(0, 0, QVariant::fromValue(QString("Item 2")));
ui->accountsTreeWidget->addTopLevelItem(item2);
}
MainWindow2::~MainWindow2()
{
delete ui;
}
主窗口2.hh
#ifndef MAINWINDOW2_HH
#define MAINWINDOW2_HH
#include <QMainWindow>
namespace Ui {
class MainWindow2;
}
class MainWindow2 : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow2(QWidget *parent = 0);
~MainWindow2();
private:
Ui::MainWindow2 *ui;
};
#endif // MAINWINDOW2_HH
主窗口2.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow2</class>
<widget class="QMainWindow" name="MainWindow2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1200</width>
<height>700</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>1200</width>
<height>700</height>
</size>
</property>
<property name="windowTitle">
<string>Main Window</string>
</property>
<widget class="QWidget" name="centralWidget">
<property name="minimumSize">
<size>
<width>1200</width>
<height>658</height>
</size>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1" rowspan="2" colspan="2">
<widget class="QMainWindow" name="mainPanel"/>
</item>
<item row="0" column="0">
<widget class="QTreeWidget" name="accountsTreeWidget">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>300</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectItems</enum>
</property>
<property name="uniformRowHeights">
<bool>true</bool>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<connections/>
</ui>
主文件
#include "mainwindow2.hh"
#include <QApplication>
#include <QStyleFactory>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setStyle(QStyleFactory::create("Fusion"));
MainWindow2 w;
w.show();
return app.exec();
}
项目.pro
#-------------------------------------------------
#
# Project created by QtCreator 2017-11-12T23:07:49
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = project
TEMPLATE = app
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += $$PWD
SOURCES += \
main.cpp \
mainwindow2.cpp
HEADERS += \
mainwindow2.hh
FORMS += \
mainwindow2.ui