问题
I'm somewhat stuck on what feels like it should be a simple problem.
I've got QMainWindow with several QDockWidgets. The QDockWidget (_tree_dock in my code) starts in the LeftDockWidgetArea. It contains a QWidget (dockWidgetContents_2) which contains a QTreeWidget (_tree_view). I can resize/move around the QDockWidget at runtime without any problems, however, when the program starts up the QDockWidget is slightly too small.
I'm able to shrink the QDockWidget quite a bit so I know it isn't at its minimum (200). It can also be made larger so it isn't at the max either. When I start Qt Designer the width is always at 258.
I put in some debug output in a few methods and saw that the width starts off as 200. Then in a call to changeEvent(QEvent *) the width is suddenly 258!
changeEvent() Enter: width of the dock is 200 where event is 105
changeEvent() Exit: width of the dock is 200 where event is 105
changeEvent() Enter: width of the dock is 258 where event is 99
changeEvent() Exit: width of the dock is 258 where event is 99
I put the output as the first and last lines of changeEvent. According to the docs these event codes are:
QEvent::WindowStateChange 105 The window's state (minimized, maximized or full-screen) has changed (QWindowStateChangeEvent). QEvent::ActivationChange 99 A widget's top-level window activation state has changed.
I'm guessing that the size gets updated once its done creating it? I've tried calling resize on the QDockWidget and the QTreeView but those don't appear to do anything. Or they are simply being overwritten by that resize to 258.
Pretty much I need the width to be like 265. One of the columns in the tree view is being half-hidden by default. If there's no 'simple' way to do this an alternative will be to just make the first column slightly smaller.
Below is the segment of the ui file with the dock.
<widget class="QDockWidget" name="_tree_dock">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Ignored">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>216</height>
</size>
</property>
<property name="features">
<set>QDockWidget::DockWidgetFloatable|QDockWidget::DockWidgetMovable</set>
</property>
<property name="allowedAreas">
<set>Qt::AllDockWidgetAreas</set>
</property>
<property name="windowTitle">
<string>Objects</string>
</property>
<attribute name="dockWidgetArea">
<number>1</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Ignored">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<layout class="QVBoxLayout" name="horizontalLayout_5">
<property name="spacing">
<number>1</number>
</property>
<property name="leftMargin">
<number>1</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>1</number>
</property>
<property name="bottomMargin">
<number>1</number>
</property>
<item>
<widget class="QTreeView" name="_tree_view">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Ignored">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragDrop</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="indentation">
<number>16</number>
</property>
<property name="rootIsDecorated">
<bool>true</bool>
</property>
<property name="uniformRowHeights">
<bool>true</bool>
</property>
<property name="animated">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
来源:https://stackoverflow.com/questions/24431163/qdockwidget-starting-size