Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Strange behaviour of QTableWidgetItems
Qt 6.11 is out! See what's new in the release blog

Strange behaviour of QTableWidgetItems

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 765 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Diado
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • F Offline
      F Offline
      flandi
      wrote on last edited by
      #2

      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).

      /* no comment */

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Diado
        wrote on last edited by
        #3

        Thanks a lot!!!!

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved