Strange behaviour of QTableWidgetItems



  • Hey,

    I have trouble for some time now to access the data in a QTableWidget.
    To show the problem I created a new project (Qt 5.3) added a QTableWidget with some columns. Then I added following code:

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

    ui->tableWidget->insertRow(0);
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("0"));
    QString test0=ui->tableWidget->itemAt(0,0)->text();
    
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("1"));
    QString test1=ui->tableWidget->itemAt(0,1)->text();
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("2"));
    QString test2=ui->tableWidget->itemAt(0,2)->text();
    
    ui->tableWidget->setItem(0,3,new QTableWidgetItem("3"));
    QString test3=ui->tableWidget->itemAt(0,3)->text();
    
    ui->tableWidget->setItem(0,4,new QTableWidgetItem("None"));
    QString test4=ui->tableWidget->itemAt(0,4)->text();
    

    }@

    Now in the table everything is displayed as expected, but all my test Strings above display "0".
    Should I get with text() the same that is displayed in the table?
    What am I doing wrong?

    Thanks in advance for your help,

    kind regards,

    Diado



  • You want to get the item of a specific cell but you request the item of a specified pixel position! All your pixel positions refer the the first item with the string "0", so the observed behaviour is correct.

    You should use the method QTableWidget::item(int row, int column) instead of itemAt(int ax, int ay).



  • Thanks a lot!!!!

    Sometimes I can read the documentation several times, but only see what I want to see...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.