我是qt的新手所以我对sizepolicy和小部件的不了解。 我有一个垂直框布局,我首先添加了一个Qlabel,然后是一个QTreeWidget,然后再添加一个Qlabel。现在的问题是QTreeWidget在垂直方向占据了整个空间,然后在QTreeWidget之后留下了一个非常大的空间,虽然目前只有行存在,但是添加了QLabel。我希望第二个Qlabel在两行可见之后立即添加,现在当添加更多行时,它会向下移动。有人可以建议我怎么做吗?
答案 0 :(得分:1)
我认为这就是你的意思: UI具有布局:Label,QTreeWidget,Label,Spacer (Spacer很重要因为其他Qt可能只是扩展标签以填充窗口。) 重要提示:在Designer TreeWidget中,需要关闭优先级,垂直滚动条。
UI示例:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>517</width>
<height>568</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="my_tree" name="treeView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAsNeeded</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>517</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>my_tree</class>
<extends>QTreeView</extends>
<header>my_tree.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
重载的QTreeView(在你的情况下是QTreeWidget)用于获取想要的sizehints: 编辑 - 这是一个uggly头只定义我称为my_tree.h -
#ifndef MY_TREE_H
#define MY_TREE_H
#include <QHeaderView>
#include <QTreeView>
class my_tree: public QTreeView
{
Q_OBJECT
public:
my_tree(QWidget* parent):QTreeView(parent)
{
// Important: if something is shown/hidden we need a new size
connect(this,SIGNAL(expanded( const QModelIndex & )),SLOT(onExpandCollapsed()));
connect(this,SIGNAL(collapsed( const QModelIndex & )),SLOT(onExpandCollapsed()));
};
// QWidget interface
public:
QSize sizeHint() const {return my_size(); };
QSize minimumSizeHint() const { return my_size(); };
private:
QSize my_size() const
{ //QSize tst(sizeHintForColumn(0) + 2 * frameWidth(), sizeHintForRow(0) + 2 * frameWidth());
int neededHight= 2 * frameWidth()+ this->header()->height();
QModelIndex root = this->rootIndex();
QAbstractItemModel* m = this->model();
//if(this->rootIsDecorated())
{
neededHight += recursiveHeightHint(root,m);
}
QSize temp = QTreeView::sizeHint();
temp.setHeight(neededHight);
return QSize(1,neededHight);
}
// we need the size of all visible items -> isExpanded
// the root item is usually shown as a non-Valid index -> !i.isValid()
int recursiveHeightHint(QModelIndex i,QAbstractItemModel* m) const
{
int temp=sizeHintForIndex(i).height();
if(this->isExpanded(i) || !i.isValid())
{
if(m->hasChildren(i))
{
int numRows = m->rowCount(i);
for(int count =0;count<numRows;count++)
temp+=recursiveHeightHint(m->index(count,0,i),m);
}
}
return temp;
}
private slots:
void onExpandCollapsed(){updateGeometry();}
};
#endif // MY_TREE_H
PS:在Designer中首先放置基本Widget,然后将其定义为自行设计的占位符。
我使用此条目来获得此解决方案: