PDA

View Full Version : QTableWidget - problems with setItem



tickhead
1st March 2015, 22:11
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:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->showMaximized();
tab = new QTableWidget(10,10, this);
this->setCentralWidget(tab);
tab->setSortingEnabled(false);
}

void MainWindow::on_actionShow_dialog_window_triggered( )
{
foreach( QTableWidgetItem *i, tab->selectedItems() )
{
if(i)
{
i->setText("AAAA");
}
else
{
tab->setItem(i->row(), i->column(), new QTableWidgetItem( "BBB" ));
}

}
tab->setItem(3, 3, new QTableWidgetItem( "CCC" ) );
}


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?

tickhead
2nd March 2015, 18:26
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.