2012-11-13 21 views
0

我有一个带有标签和QGraphicsView的垂直布局。我已经设置了QGraphicsView的最大尺寸,并将水平对齐设置为AlignHCenter,但它似乎仍然出现在左侧而不是已知的?这里是一个演示UI文件:最大尺寸的小工具没有遵循AlignHCenter

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>Form</class> 
<widget class="QWidget" name="Form"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>400</width> 
    <height>300</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>Form</string> 
    </property> 
    <layout class="QVBoxLayout" name="verticalLayout"> 
    <item> 
    <widget class="QLabel" name="label"> 
    <property name="text"> 
     <string>TextLabel</string> 
    </property> 
    <property name="alignment"> 
     <set>Qt::AlignCenter</set> 
    </property> 
    </widget> 
    </item> 
    <item> 
    <widget class="QGraphicsView" name="graphicsView"> 
    <property name="sizePolicy"> 
     <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> 
     <horstretch>0</horstretch> 
     <verstretch>0</verstretch> 
     </sizepolicy> 
    </property> 
    <property name="maximumSize"> 
     <size> 
     <width>100</width> 
     <height>100</height> 
     </size> 
    </property> 
    </widget> 
    </item> 
    </layout> 
</widget> 
<resources/> 
<connections/> 
</ui> 

如果我改变标签的水平对齐方式,它的行为,我期望(左移动到左边,AlignHCenter它移动到中心),但的QGraphicsView不遵循这种行为 - 它总是在左边。

我该如何居中这个固定大小的QGraphicsView?

回答

1

如果您缠绕的QGraphicsView在QHBoxLayout,负责如然后它会出现在中心像你期望的那样。如果您想将其对齐到任一侧,您可以在左侧或右侧插入水平间隔器。

alignment property控制其行为时的QGraphicsView具有滚动条,在不QLayout其相对位置。

<item> 
    <layout class="QHBoxLayout" name="horizontalLayout"> 
    <item> 
     <widget class="QGraphicsView" name="graphicsView"> 
     <property name="sizePolicy"> 
     <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> 
     <horstretch>0</horstretch> 
     <verstretch>0</verstretch> 
     </sizepolicy> 
     </property> 
     <property name="maximumSize"> 
     <size> 
     <width>100</width> 
     <height>100</height> 
     </size> 
     </property> 
     </widget> 
    </item> 
    </layout> 
    </item>