Bill Brown
Bill Brown

Reputation: 41

QTableWidget not displaying text

I am building a simple widget that will take an input from a lineEdit, and add it as a new row (assuming that the entry is not in there already) to a QTableWidget. The problem I have been encountering is that the text will not display in the tablewidget, though the rows appear--empty.

Relevant code:

In constructor:

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

In 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));

}

^ that's in the code.

Upvotes: 0

Views: 2221

Answers (1)

Bill Brown
Bill Brown

Reputation: 41

Figured it out!

Initialized rowCounter with a value of zero, so it was always setting the item to a row that hadn't been initialized yet!

Here's the working version if anyone encounters the same issue:

Constructor:

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++;
}

Upvotes: 1

Related Questions