June 2, 2012

adnan adnan
Hobby Entomologist
269 posts

How to access the text of Qtablewidget when selection behavior is select rows

 

I want to access the text of first column of the currently selected row, but I can’t find a way similar to that of QtreeWidget.

  1.  if(ui->treewidget ->currentItem()->text(0)=="Something")
  2. // then do something

but Qtablewidget doesn’t support such a feature. So what to do.

3 replies

June 3, 2012

ChrisW67 ChrisW67
Robot Herder
371 posts

The table widget equivalent of your tree widget code would be something like:

  1. int row = ui->tableWidget->currentRow();
  2. QString text = ui->tableWidget->item(row, 0)->text();

The current item and current selection are not necessarily related though. If you allow only single selection and have QAbstractItemView::SelectRows set then QTableWidget::selectedItems() should contain only one item with row == 0.

June 3, 2012

adnan adnan
Hobby Entomologist
269 posts

thanks a lot for such a nice reply!

June 3, 2012

adnan adnan
Hobby Entomologist
269 posts

There is a bit problem with

  1.  ui-
  2. >tableWidget->item( row, 0)-
  3. >text();

the program crashes at this statement if the row is empty, if there is no text.

 
  ‹‹ Using QT widget from cocoa application      QTableView::scrollToBottom and performance ››

You must log in to post a reply. Not a member yet? Register here!