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?