QTableWidget::itemAt() returns seemingly random items

Posted by Jordan Milne on Stack Overflow See other posts from Stack Overflow or by Jordan Milne
Published on 2010-06-06T13:18:18Z Indexed on 2010/06/06 13:22 UTC
Read the original article Hit count: 417

Filed under:
|
|
|

I've just started using Qt, so please bear with me. When I use QTableWidget->getItemAt(), it returns a different item from if I used currentItemChanged and clicked the same item. I believe it's necessary to use itemAt() since I need to get the first column of whatever row was clicked.

Some example code is below:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QList<QString> rowContents;

    rowContents << "Foo" << "Bar" << "Baz" << "Qux" << "Quux" << "Corge" << "Grault" << "Garply" << "Waldo" << "Fred";

    for(int i =0; i < 10; ++i)
    {
        ui->tableTest->insertRow(i);
        ui->tableTest->setItem(i, 0, new QTableWidgetItem(rowContents[i]));
        ui->tableTest->setItem(i, 1, new QTableWidgetItem(QString::number(i)));
    }
}

//...

void MainWindow::on_tableTest_currentItemChanged(QTableWidgetItem* current, QTableWidgetItem* previous)
{
    ui->lblColumn->setText(QString::number(current->column()));
    ui->lblRow->setText(QString::number(current->row()));
    ui->lblCurrentItem->setText(current->text());
    ui->lblCurrentCell->setText(ui->tableTest->itemAt(current->column(), current->row())->text());
}

For the item at 1x9, lblCurrentItem displays "9" (as it should,) whereas lblCurrentCell displays "Quux". Am I doing something wrong?

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt