QTableWidget不显示文本

时间:2016-06-15 14:04:58

标签: qt

我正在构建一个简单的小部件,它将从lineEdit获取输入,并将其作为新行添加(假设该条目不在那里)到QTableWidget。我遇到的问题是文本不会显示在tablewidget中,尽管行显示为空。

相关代码:

在构造函数中:

rowCounter = 0;
ui->flagTable->setColumnCount(1);
ui->flagTable->horizontalHeader()->hide();
ui->flagTable->verticalHeader()->hide();
ui->flagTable->setColumnWidth(0,148);
QString commandInput = ui->flagInput->text();

在on_flagInput_returnPressed():

QString commandInput = ui->flagInput->text();
if (ui->flagTable->findItems(commandInput, Qt::MatchFixedString).isEmpty())
{
    rowCounter++;
    ui->flagTable->setRowCount(rowCounter);
    ui->flagTable->setItem(rowCounter, 0, new QTableWidgetItem(commandInput));

}

^代码中的内容。

1 个答案:

答案 0 :(得分:0)

想出来了!

初始化的rowCounter,其值为零,因此始终将项目设置为尚未初始化的行!

如果有人遇到同样的问题,这是工作版本:

构造

rowCounter = 1;
ui->flagTable->setColumnCount(1);
ui->flagTable->horizontalHeader()->hide();
ui->flagTable->verticalHeader()->hide();
ui->flagTable->setColumnWidth(0,148);

on_flagInput_returnPressed():

QString commandInput = ui->flagInput->text();
if (ui->flagTable->findItems(commandInput, Qt::MatchFixedString).isEmpty())
{
    QTableWidgetItem *commandItem = new QTableWidgetItem(commandInput, 1);
    ui->flagTable->setRowCount(rowCounter);
    ui->flagTable->setItem(rowCounter-1, 0, commandItem);
    rowCounter++;
}