在QtCreator中,我在画布上添加了一个带有两列的Tree Widget。这样创建了XML,如下所示:-
<widget class="QTreeWidget" name="postgresTree">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>851</width>
<height>471</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="lineWidth">
<number>1</number>
</property>
<property name="allColumnsShowFocus">
<bool>false</bool>
</property>
<property name="columnCount">
<number>2</number>
</property>
<column>
<property name="text">
<string notr="true">Schema</string>
</property>
</column>
<column>
<property name="text">
<string notr="true">Table</string>
</property>
</column>
然后我在其中填充:-
QSqlQuery query;
query.exec("SELECT schema_name FROM information_schema.schemata");
while(query.next()) {
QString schema = query.value(0).toString();
QTreeWidgetItem *schema_branch = new QTreeWidgetItem(ui->postgresTree);
schema_branch->setText(0,schema);
schema_branch->setText(1,QString("Table"));
//Get table list for schema
QSqlQuery table_query;
table_query.exec(QString("SELECT tablename FROM pg_tables where schemaname = '%1'").arg(schema));
while(table_query.next()) {
QString table = table_query.value(0).toString();
QTreeWidgetItem *table_branch = new QTreeWidgetItem(ui->postgresTree);
table_branch->setText(1,table);
schema_branch->addChild(table_branch);
}
ui->postgresTree->addTopLevelItem(schema_branch);
}
这可以工作,但是在我的树中没有展开/折叠手柄的固定布局。我希望能够在表列折叠的情况下查看此树,并显示展开/折叠控件。我该怎么做?
尝试更改table_branch以将schema_branch用作父级。
QTreeWidgetItem *table_branch = new QTreeWidgetItem(schema_branch);
然后您也不需要调用addChild。