简体   繁体   English

如何在没有中央小部件(PyQt4)的应用程序中设置 QDockWidget 初始(默认)大小?

[英]How to set QDockWidget initial (default) size in the app with no central widget (PyQt4)?

I have an app with a lot of QDockWidgets and without central widget.我有一个包含很多QDockWidgets而没有中央小部件的应用程序。 I want to set some of those QDockWidgets initial size (size at application's start), but I don't want to limit their min/max sizes.我想设置一些QDockWidgets初始大小(应用程序启动时的大小),但我不想限制它们的最小/最大大小。

How to do it?怎么做? For example, I need to set initial size 150x75 to one of them.例如,我需要将初始大小 150x75 设置为其中之一。 I tried obvious methods (such as QWidget.Resize() relating to dock widget content), but it didn't work at all.我尝试了明显的方法(例如与停靠小部件内容相关的QWidget.Resize() ),但它根本不起作用。

Here is a simplified model of situation:这是一个简化的情况模型:

from PyQt4 import QtCore, QtGui

app = QtGui.QApplication([''])

mw = QtGui.QMainWindow() # mw = MainWindow
mw.setCentralWidget(None)
mw.showMaximized()

mw.dockWdg1 = QtGui.QDockWidget(mw)
mw.content1 = QtGui.QTreeWidget()
mw.dockWdg1.setWidget(mw.content1)
mw.addDockWidget(QtCore.Qt.DockWidgetArea(2), mw.dockWdg1)
mw.dockWdg1.setWindowTitle("1st dock widget")

mw.dockWdg2 = QtGui.QDockWidget(mw)
mw.content2 = QtGui.QTreeWidget()
mw.dockWdg2.setWidget(mw.content2)
mw.addDockWidget(QtCore.Qt.DockWidgetArea(1), mw.dockWdg2)
mw.dockWdg2.setWindowTitle("2nd dock widget")

mw.dockWdg3 = QtGui.QDockWidget(mw)
mw.content3 = QtGui.QTreeWidget()
mw.dockWdg3.setWidget(mw.content3)
mw.addDockWidget(QtCore.Qt.DockWidgetArea(1), mw.dockWdg3)
mw.dockWdg3.setWindowTitle("3rd dock widget")

mw.show()
app.exec_()

The dockwidgets will be incorporated into the layout of the main window, so any attempt to resize them will be ignored. dockwidgets将被合并到主窗口的布局中,因此任何调整它们的尝试都将被忽略。

The standard workaround for this is to create a subclass of the content widget and reimplement its sizeHint : 此标准解决方法是创建内容窗口小部件的子类并重新实现其sizeHint

class TreeWidget(QtGui.QTreeWidget):
    def sizeHint(self):
        return QtCore.QSize(150, 75)

mw.dockWdg2 = QtGui.QDockWidget(mw)
mw.content2 = TreeWidget()
mw.dockWdg2.setWidget(mw.content2)

However, this will only work to the extent that you also carefully manage the sizes of the other dockwidgets. 但是,这只会在您仔细管理其他dockwidgets的大小的情况下起作用。 And of course maximiizing the main window is also going to have a impact on the final outcome. 当然,最大化主窗口也会对最终结果产生影响。

You might also want to consider using QMainWindow.saveState and QMainWindow.restoreState to manage the initial state of your dockwidgets. 您可能还需要考虑使用QMainWindow.saveStateQMainWindow.restoreState来管理dockwidgets的初始状态。

使用fixedSize函数,如setFixedWidth / setFixedHeight。

I think reimplementing the sizeHint() function based on MainWindow's size is better than just returning a fixed size. 我认为基于MainWindow的大小重新实现sizeHint()函数比返回固定大小更好。

Source Code 源代码

// size hint
QSize RenderWindow::sizeHint() const
{
    float width = MainWindow::instance()->width() * 0.3f;
    float height = MainWindow::instance()->height() * 0.65f;
    return QSize( width, height);
}

在此输入图像描述

Anyone landing on this old question since Qt 5.6 should look at QMainWindow::resizeDocks()自 Qt 5.6 以来遇到这个老问题的任何人都应该查看QMainWindow::resizeDocks()

https://doc.qt.io/qt-5/qmainwindow.html#resizeDocks https://doc.qt.io/qt-5/qmainwindow.html#resizeDocks

eg:例如:

mw.resizeDocks({ mw.dockWdg1,  mw.dockWdg1 }, { 260, 540 }, Qt.Vertical);

(Not sure that's 100% valid Py, but you get the idea...) (不确定这是 100% 有效的 Py,但你明白了……)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM