Results 1 to 2 of 2

Thread: Getting / setting data on QTableWidgets

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2011
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Getting / setting data on QTableWidgets

    I have two QTableWidgets and I'm trying to synchronize them as an exercise to figure out how to get / set data.

    The current code I have is:
    Qt Code:
    1. void MainWindow::on_tableWidget_2_cellChanged(int row, int column)
    2. {
    3. double value = ui->tableWidget_2->itemAt(row,column)->text().toDouble();
    4. ui->tableWidget->itemAt(row,column)->setData(Qt::UserRole, value);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I have also tried:

    Qt Code:
    1. void MainWindow::on_tableWidget_2_cellChanged(int row, int column)
    2. {
    3. QString value = ui->tableWidget_2->itemAt(row,column)->text();
    4. ui->tableWidget->itemAt(row,column)->setText(value);
    5. }
    To copy to clipboard, switch view to plain text mode 

    No matter what, the QString returned from QTableWidgetItem::text() is an empty string. This happens regardless of whether or not there was any text before I attempted editing.

    Not sure if it matters, but this is how I initialize the table:

    Qt Code:
    1. QTableWidgetItem * tableItem;
    2. for(int i = 0; i < 5; i++)
    3. {
    4. ui->tableWidget->insertRow(i);
    5. ui->tableWidget_2->insertRow(i);
    6.  
    7. tableItem = new QTableWidgetItem();
    8. ui->tableWidget->setItem(i,0,tableItem);
    9. tableItem = new QTableWidgetItem();
    10. ui->tableWidget->setItem(i,1,tableItem);
    11.  
    12. tableItem = new QTableWidgetItem();
    13. ui->tableWidget_2->setItem(i,0,tableItem);
    14. tableItem = new QTableWidgetItem();
    15. ui->tableWidget_2->setItem(i,1,tableItem);
    16. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Getting / setting data on QTableWidgets

    QTableWidgetItem * QTableWidget::itemAt ( const QPoint & point ) const

    QTableWidgetItem * QTableWidget::itemAt ( int ax, int ay ) const

    for both the functions the parameter is point (in table widget's coordinate system), this is not row & column. This is the reason for empty in the text().

    Try using this
    QTableWidgetItem * QTableWidget::item ( int row, int column ) const

    Use the second method.
    First method should also work if Qt::UserRole is changed to Qt:: DataRole
    Last edited by Santosh Reddy; 30th October 2011 at 03:37.

Similar Threads

  1. Replies: 5
    Last Post: 10th June 2011, 11:58
  2. Using QSortFilterProxyModel with QTableWidgets
    By kremuwa in forum Qt Programming
    Replies: 5
    Last Post: 30th September 2010, 17:32
  3. setting UserRole data in QSqlTableModel
    By orgads in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 10:40
  4. QStandardItem: Setting Top Column Data?
    By AaronMK in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2008, 21:13
  5. QTableWidgets problems with columns
    By 3nc31 in forum Qt Programming
    Replies: 1
    Last Post: 24th October 2007, 09:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.