Show integer value in qtable widget
-
wrote on 30 Aug 2017, 11:58 last edited by raven-worx
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
-
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
wrote on 30 Aug 2017, 12:43 last edited by Taz742ui->tableWidget_3->setItem(0, 1, new QTableWidgetItem(QString::number(rgnt.R_id) ) );
-
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
wrote on 30 Aug 2017, 12:55 last edited byHi,
QTableWidgetItem wont take integer as a input so try as @Taz742 has posted. -
wrote on 30 Aug 2017, 13:20 last edited by
Guys, there are other methods in a class beyond the constructor!
auto widItem = new QTableWidgetItem; widItem->setData(Qt::EditRole,rgnt.R_id); ui->tableWidget_3->setItem(0,1,widItem);
-
Guys, there are other methods in a class beyond the constructor!
auto widItem = new QTableWidgetItem; widItem->setData(Qt::EditRole,rgnt.R_id); ui->tableWidget_3->setItem(0,1,widItem);
-
wrote on 30 Aug 2017, 13:26 last edited by
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 -
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 towrote on 30 Aug 2017, 13:29 last edited by@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?
-
wrote on 30 Aug 2017, 13:50 last edited by
While we think arab numbers are universal, they are not. Chinese locales, for example, have special unicode chars for numbers (0 = 零, 1 = 一) . Even arabic has different chars for arab numbers
-
While we think arab numbers are universal, they are not. Chinese locales, for example, have special unicode chars for numbers (0 = 零, 1 = 一) . Even arabic has different chars for arab numbers
-
ui->tableWidget_3->setItem(0, 1, new QTableWidgetItem(QString::number(rgnt.R_id) ) );
wrote on 31 Aug 2017, 07:03 last edited by@Taz742 thank you.. it works..
-
@Taz742 thank you.. it works..
wrote on 31 Aug 2017, 07:21 last edited by@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);
-
While we think arab numbers are universal, they are not. Chinese locales, for example, have special unicode chars for numbers (0 = 零, 1 = 一) . Even arabic has different chars for arab numbers
-
wrote on 31 Aug 2017, 07:57 last edited by
@Taz742 said in Show integer value in qtable widget:
@VRonin
Why does not it take Qt::Checked or Qt::Unchecked?You need to adjust the flag too for that role to matter
widItem->setFlags(widItem->flags() | Qt::ItemIsUserCheckable);
-
@Taz742 said in Show integer value in qtable widget:
@VRonin
Why does not it take Qt::Checked or Qt::Unchecked?You need to adjust the flag too for that role to matter
widItem->setFlags(widItem->flags() | Qt::ItemIsUserCheckable);
wrote on 31 Aug 2017, 08:22 last edited by Taz742@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));
-
@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));
wrote on 31 Aug 2017, 08:29 last edited by VRonin- 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));
-
- 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));
5/16