簡體   English   中英

QTreeWidget 折疊/展開項目

[英]QTreeWidget Collaps/Expand items

在 QtCreator 中,我在 canvas 中添加了一個包含兩列的 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。

調用 addTopLevelItem 可能也沒有必要,因為您已經將樹本身作為父級,這應該足夠了。

ui->postgresTree->addTopLevelItem(schema_branch);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM