Read tableWidget
-
Hello. My Qt version is 4.8
I want enter by keyboard data to tableWidget and then read it.
How i can do it ?I try to do the following.
@
iColumns=ui.tableWidget->columnCount();
iRows=ui.tableWidget->rowCount();
for(int i=0;i<iRows;++i)
{
for(int j=0;j<iColumns;++j)
{
//QTableWidgetItem *item=ui.tableWidget->item(i,j);
//QString temp=item->text();
//std::cout<<temp.toAscii().data()<<std::endl;QMessageBox msgBox; QTableWidgetItem* theItem = ui.tableWidget->item(i,j); QString theText = theItem->text(); msgBox.about(this,"Test",theText); }
}
@it's not work.
if i use the commented code lines show me "Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must reimplement QApplication::notify() and catch all exceptions there"
How to solve my problem ?
thanks
-
Hi and welcome to devnet,
Are you sure you have an item in each cell ?
-
Hi,
When a QTableWidget is created all the cells are "empty". You need to add a widget to every cell to hold text/icons etc. The QTableWidget will be the parent of the items, so no cleanup is needed after it.
In your code this line
@
QTableWidgetItem* theItem = ui.tableWidget->item(i,j);
@
will probably return '0' pointer. So the next line to read the text will crash your program!
After generation of the table, add the items:
@
ui->tableWidget.setItem(i,j) = new QTableWidgetItem;
@
Hope this helps!