我有一些代码,我尝试在其中填充QTableWidget。但是,当我运行此应用程序时,卖空。我检查了QTableWidget是否包含任何元素(项目)。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidget>
#include <QDebug>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QTableWidget *pWidget = new QTableWidget( 5, 6 );
for( int row = 0; row < 5; row++ )
{
for( int column = 0; column < 6; column++ )
{
QString sItem = QString::number( row + column );
QVariant oVariant(sItem);
QTableWidgetItem oItem;
oItem.setData( Qt::DisplayRole, oVariant );
pWidget->setItem( row, column, &oItem );
QVariant oData = pWidget->item( row, column )->data( Qt::DisplayRole );
qDebug() << "OData: " << oData.toString();
}
}
QTimer::singleShot( 0, pWidget, SLOT( show() ) );
}
答案 0 :(得分:0)
您需要分配QTableWidgetItem
工作示例:
QTableWidget *pWidget = new QTableWidget( 5, 6 );
for( int row = 0; row < 5; row++ )
{
for( int column = 0; column < 6; column++ )
{
QString sItem = QString::number( row + column );
QVariant oVariant(sItem);
// allocate the widget item
QTableWidgetItem * poItem = new QTableWidgetItem();
poItem->setData( Qt::DisplayRole, oVariant );
pWidget->setItem( row, column, poItem );
}
}
答案 1 :(得分:0)
在PyQt5中,它看起来像这样:
from PyQt5 import QtWidgets
# Creates an Qt item
qt_item = QtWidgets.QTableWidgetItem(CELL_DATA)
# Set the qt_item at position (row, column) of your table_widget
table_widget.setItem(row, column, qt_item)