我必須使用QTableWidget顯示項目列表。我在運行時動態插入這些項目。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();
我在做什麼錯?我怎樣才能解決這個問題?
以正確的方式實現小工具,手動更新,*從未*必要的。考慮這些變通辦法總是指向其他問題。 –
您的用戶界面中沒有'QTableWidget',只有'QTableView'。 –
對不起,但我無法重現您的問題。你的用戶界面文件是可以的。投票結束,因爲你仍然拒絕提供一個能夠複製你的問題的自我測試案例。 –