QTableWidget 不显示行

QTableWidget not displaying rows

我必须使用 QTableWidget 显示项目列表。我在运行时动态插入这些项目。

问题是 QTableWidget 控件没有显示任何行。我已经用调试器验证了这些项目确实被插入了,但由于某种原因,小部件没有得到刷新/重新绘制。

我在网上进行了广泛的搜索,并针对类似情况尝试了建议的解决方案,但没有成功。

请在下面找到相关代码:

QTableWidget.ui

    <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>QDialogClassName</class>
 <widget class="QDialog" name="QDialogClassName">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>587</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="windowTitle">
   <string>QDialogClassName</string>
  </property>
  <widget class="QWidget" name="verticalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>40</y>
     <width>781</width>
     <height>541</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QLabel" name="introductionLabel">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="autoFillBackground">
       <bool>false</bool>
      </property>
      <property name="text">
       <string>Label</string>
      </property>
     </widget>
    </item>
    <item>
     <spacer name="verticalSpacer">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeType">
       <enum>QSizePolicy::Fixed</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>20</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item>


   <widget class="QTableView" name="tableView">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>30</y>
      <width>551</width>
      <height>271</height>
     </rect>
    </property>

    <property name="sortingEnabled">
       <bool>true</bool>
    </property>
    <attribute name="horizontalHeaderVisible">
       <bool>true</bool>
    </attribute>
    <attribute name="horizontalHeaderStretchLastSection">
       <bool>true</bool>
    </attribute>    
   </widget>




    </item>
    <item>
     <widget class="QProgressBar" name="progressBar">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="value">
       <number>24</number>
      </property>
     </widget>
    </item>
    <item>
     <layout class="QHBoxLayout" name="buttonsHorizontalLayout">
      <item>
       <spacer name="horizontalSpacer">
        <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
        <property name="sizeHint" stdset="0">
         <size>
          <width>40</width>
          <height>20</height>
         </size>
        </property>
       </spacer>
      </item>
      <item>
       <widget class="QPushButton" name="previousBackButton">
        <property name="text">
         <string>Previous...</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="nextButton">
        <property name="text">
         <string>Next...</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

ui_QTableWidget.h

    tableWidget = new QTableWidget(verticalLayoutWidget);
    tableWidget->setObjectName(QStringLiteral("tableWidget"));
    tableWidget->setShowGrid(true);
    tableWidget->setColumnCount(N_COLUMNS);
    tableWidgetHeader << "Header1" << "Header2";
    tableWidget->setHorizontalHeaderLabels(tableWidgetHeader);
    tableWidget->setRowCount(0);
    tableWidget->verticalHeader()->setVisible(true);
    tableWidget->verticalHeader()->show();

    verticalLayout->addWidget(tableWidget);

QTableWidget.cpp

int row_count;
QTableWidgetItem *itab;
row_count = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row_count);
// as an alternative to the above line of code I have also tried ui->tableWidget->->setRowCount(row_count);


itab = new QTableWidgetItem;
itab->setText("Cell1");
ui->tableWidget->setItem(row_count, 0, itab);
ui->tableWidget->item(row_count, 0)->setBackground(Qt::red);

itab = new QTableWidgetItem;
itab->setText("Cell2");
ui->tableWidget->setItem(row_count, 1, itab);
ui->tableWidget->item(row_count,1)->setBackground(Qt::green);

/*Each of the following lines is an attempted solution. Other proposed solutions include emiting rowCountChanged and similar protected signals. */
    //ui->tableWidget->update();
    //ui->tableWidget->show();
    //ui->tableWidget->viewport()->update();
    //ui->tableWidget->viewport()->show();

我做错了什么?我该如何解决这个问题?

看来你几乎什么都做对了。有两个问题:

  1. 当您将项目插入可能排序的 table 小部件时,每个行项目的更改都可以重新排序视图并最终更改行索引。设置 item1 的文本后,它所在的行将不是您插入空行的行。您必须在每次文本更改后获取项目的行。

  2. 您的 UI 文件有点损坏,因为没有 top-level 布局,但这并不影响视图本身的功能。

以下测试用例证明一切正常。您未在此处显示的其他一些代码已损坏。

  1. 你的行插入代码曾经运行吗?在那里设置一个断点,看它是否触发。

  2. 您还有其他看法吗?也许重叠的?您确定要向正在查看的视图中添加行吗?

  3. 确保您看到 table 小部件的 headers - 如果您没有看到,那么您的整个 table 小部件都不可见,并且您的问题与您插入的任何行无关。

  4. 您是否在插入行后阻塞了事件循环,从不给小部件重绘的机会?添加行后应用程序是否响应?

这些确实是调试基础知识,但您从未在问题中表示您已经检查过这些。

// https://github.com/KubaO/Whosebugn/tree/master/questions/tablewidget-32403753
#include <QtWidgets>

int main(int argc, char ** argv) {
   QApplication app{argc, argv};

   QWidget w;
   QVBoxLayout layout { &w };

   QTableWidget tw;
   tw.setShowGrid(true);
   tw.setColumnCount(2);
   auto header = QStringList() << "Header1" << "Header2";
   tw.setHorizontalHeaderLabels(header);
   tw.setSortingEnabled(true);

   QPushButton button { "Add Row" };
   QObject::connect(&button, &QPushButton::clicked, &tw, [&tw]{
      auto row = tw.rowCount();
      tw.insertRow(row);

      auto item1 = new QTableWidgetItem { "Cell1" };
      item1->setBackground(Qt::red);
      tw.setItem(row, 0, item1);
      row = item1->row(); // Needed to support sorted tables.

      auto item2 = new QTableWidgetItem { "Cell2" };
      item2->setBackground(Qt::green);
      tw.setItem(row, 1, item2);
   });

   layout.addWidget(&tw);
   layout.addWidget(&button);
   w.show();
   return app.exec();
}