Solved How to get QTableWidgetItem's text when it is editing?
-
Just as the upper pic shows, a simple qtablewidget with just one qtablewidgetitem.
When I type
1
,and then, I clicked theOk
, the triggered slot will try to get this qtablewidgetitem's text,void ok_btn_clicked_slot() { ... qDebug() << m_tableWidget->item(i, 0)->data(Qt::DisplayRole).toString().isEmpty(); qDebug() << m_tableWidget->item(i, 0)->data(Qt::EditRole).toString().isEmpty(); qDebug() << m_tableWidget->item(i, 0)->data(Qt::AccessibleTextRole).toString().isEmpty(); qDebug() << m_tableWidget->item(i, 0)->data(Qt::AccessibleDescriptionRole).toString().isEmpty(); qDebug() << m_tableWidget->item(i, 0)->data(Qt::UserRole).toString().isEmpty(); ... }
I found they all return
true
, that is to say, I can't get the text.But, actually, I typed
1
in it, why is it like that? Could someone give me any advice? -
Hi
While the cell is in editmode, the text is not yet saved to the (internal) model
So you can make it save by setting focus to some other widget. ( it saves on loosing focus )
or try to call
http://doc.qt.io/qt-5/qtablewidget.html#closePersistentEditor
to it commit the text.Since the click on button should take focus ( i assume ) there could be something else
wrong. You are sure the i variable has correct value ? -
I tried
closePersistentEditor
, but it failed.You are sure the i variable has correct value ? Sorry, what's meaning of thei variable
?Yes, I'm sure. I print the
m_tableWidget->item(i, 0)
, and it's surely existed. -
Hi
Just made small test. just clicking on the button makes editor
save so something else is incorrect with your code.void MainWindow::on_pushButton_released() { QMessageBox::warning(this,"info", ui->tableWidget->item(0,0)->data(Qt::DisplayRole).toString()); }
-
@mrjj Why? My codes are still failed.
Could you paste your codes here, thanks a lot...
-
-
@mrjj Make it!
You are right. But why my codes are wrong? Because the below codes,
m_addTableBtn->setFocusPolicy(Qt::NoFocus); m_removeTableBtn->setFocusPolicy(Qt::NoFocus); m_okBtn->setFocusPolicy(Qt::NoFocus); m_cancelBtn->setFocusPolicy(Qt::NoFocus);
I remove these codes, and then, problem solved.
Thanks a lot, mrjj.
-
@Limer
Hi
For my test code, the value is saved when button clicked as button takes focus and editor then saves losing focus.
However when using m_okBtn->setFocusPolicy(Qt::NoFocus); it means it will not take focus then
and editor won't save then as editor won't lose focus then.If really need noFocus on the buttons, i think we can find another way to have it save.