Show integer value in qtable widget
-
HI..,
I am new to QT. i have to display an int value in qtable widget.I tried following code. but it doesnot showing the value in table widget.rgnt.R_id=1;
ui->tableWidget_3->setItem(0,1,new QTableWidgetItem((rgnt.R_id)));here rgnt is a structure object.
can anyone tell how can i show this value in tablewidget??Edit raven-worx: moved topic
-
ui->tableWidget_3->setItem(0, 1, new QTableWidgetItem(QString::number(rgnt.R_id) ) );
-
Hi,
QTableWidgetItem wont take integer as a input so try as @Taz742 has posted. -
Huge!
Just for a laugh, try filling a column with the numbers from 1 to 100 using your method and then sort the column.
on top of that,
QString::number
andQString::toInt
should never be used for things that are shown on the ui. Always useQLocale::toString
andQLocale::toInt
with the locale of the widget you are presenting the value to -
@VRonin said in Show integer value in qtable widget:
QString::number and QString::toInt should never be used for things that are shown on the ui.
I apologize for hijacking the thread but what if you have just plain integers and you are sure that values are locale independent?
-
@QTlearner90
Yes it works. But review the comments and then make a decision.
I think it's better if you use another method what @VRonin said.auto widItem = new QTableWidgetItem; widItem->setData(Qt::EditRole,rgnt.R_id); ui->tableWidget_3->setItem(0,1,widItem);
-
@VRonin Can not I give it my function?
QTableWidgetItem *dlgGroups::GetNewItem(const QVariant value){ auto c_item = new QTableWidgetItem; c_item->setData(Qt::UserRole, value); return (QTableWidgetItem*) c_item; } Contact *cont = globalall->GetConact(CUID); m_table->insertRow(0); m_table->setItem(0, 0, GetNewItem(InsertQry.lastInsertId().toString())); m_table->setItem(0, 1, GetNewItem(cont->UID)); m_table->setItem(0, 2, GetNewItem(Qt::Unchecked)); // ? m_table->setItem(0, 3, GetNewItem(cont->Name + " " + cont->FamilyName)); m_table->setItem(0, 4, GetNewItem(cont->Mobile)); m_table->setItem(0, 5, GetNewItem(cont->Type ? cont->Identification : cont->PersonID));
-
- you still did not set the flag
- you are saving everything in
Qt::UserRole
which is never used by Qt's standard views
QTableWidgetItem *dlgGroups::GetNewItem(const QVariant& value, int role = Qt::EditRole){ auto c_item = new QTableWidgetItem; c_item->setData(role , value); if(role==Qt::CheckStateRole) c_item->setFlags(c_item->flags() | Qt::ItemIsUserCheckable); return c_item; }
then you can use
m_table->setItem(0, 2, GetNewItem(Qt::Unchecked,Qt::CheckStateRole));