I have an app with a lot of QDockWidgets and without central widget. 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,没有中心小部件。我想设置一些QDockWidgets的初始大小(应用程序开始时的大小),但是我不想限制它们的最小/最大大小。
How to do it? For example, I need to set initial size 150x75 to one of them. I tried obvious methods (such as QWidget.Resize() relating to dock widget content), but it didn't work at all.
如何去做?例如,我需要将初始大小设置为150x75。我尝试了一些明显的方法(例如QWidget.Resize()与dock widget内容相关),但根本不起作用。
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_()
2 个解决方案
#1
7
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
:
对此的标准解决方案是创建content widget的子类并重新实现其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. And of course maximiizing the main window is also going to have a impact on the final outcome.
但是,这只会在您还仔细管理其他dockwidgets大小的范围内有效。当然最大化主窗口也会对最终结果产生影响。
You might also want to consider using QMainWindow.saveState and QMainWindow.restoreState to manage the initial state of your dockwidgets.
您可能还想考虑使用QMainWindow。saveState QMainWindow。恢复您的dockwidget的初始状态。
#2
0
Use the fixedSize function, as setFixedWidth/setFixedHeight.
使用fixedSize函数,如setFixedWidth/setFixedHeight。
#1
7
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
:
对此的标准解决方案是创建content widget的子类并重新实现其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. And of course maximiizing the main window is also going to have a impact on the final outcome.
但是,这只会在您还仔细管理其他dockwidgets大小的范围内有效。当然最大化主窗口也会对最终结果产生影响。
You might also want to consider using QMainWindow.saveState and QMainWindow.restoreState to manage the initial state of your dockwidgets.
您可能还想考虑使用QMainWindow。saveState QMainWindow。恢复您的dockwidget的初始状态。
#2
0
Use the fixedSize function, as setFixedWidth/setFixedHeight.
使用fixedSize函数,如setFixedWidth/setFixedHeight。