Results 1 to 2 of 2

Thread: QTableWidget - problems with setItem

  1. #1
    Join Date
    Mar 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QTableWidget - problems with setItem

    Hi, I'm new to programming in Qt and I'm having problems using the TableWidget. I first initialize the QTableWidget pointer in my MainWindow constructor to have a number of rows and columns, but once I try to initialize selected items in a loop, it doesn't work. I tried disabling the sorting option but it didn't help either. I tried setting a single item outside the loop and it worked fine. Here's the code:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. this->showMaximized();
    7. tab = new QTableWidget(10,10, this);
    8. this->setCentralWidget(tab);
    9. tab->setSortingEnabled(false);
    10. }
    11.  
    12. void MainWindow::on_actionShow_dialog_window_triggered()
    13. {
    14. foreach( QTableWidgetItem *i, tab->selectedItems() )
    15. {
    16. if(i)
    17. {
    18. i->setText("AAAA");
    19. }
    20. else
    21. {
    22. tab->setItem(i->row(), i->column(), new QTableWidgetItem( "BBB" ));
    23. }
    24.  
    25. }
    26. tab->setItem(3, 3, new QTableWidgetItem( "CCC" ) );
    27. }
    To copy to clipboard, switch view to plain text mode 

    Is it just one of those things that can't be done in a loop? How should I load some data out of a file and put it inside the table properly?

  2. #2
    Join Date
    Mar 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableWidget - problems with setItem

    I think I found the solution. Turns out that when I select some empty cells and write the number of them via the debugger (tab->selectedItems().count() ), it returns a 0, so no wonder the foreach loop does nothing. I guess the thread can be marked solved now.
    Last edited by tickhead; 2nd March 2015 at 17:31.

Similar Threads

  1. QTableWidget crashes on setItem
    By Raccoon29 in forum Qt Programming
    Replies: 5
    Last Post: 18th August 2016, 18:48
  2. Replies: 3
    Last Post: 22nd March 2010, 13:50
  3. QTableWidget -- SetItem
    By QbelcorT in forum Qt Programming
    Replies: 3
    Last Post: 29th July 2009, 06:00
  4. QTableWidget problems
    By eleanor in forum Qt Programming
    Replies: 6
    Last Post: 22nd May 2009, 13:18
  5. QTableWidget::setItem very slow the second time
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 26th September 2008, 22:18

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.