I have the following code for formatting a QTableWidget. The sortItems method sorts alphabetically and not numerically. Is there an easy fix to this code to be able to do so? Please be explicit as I a quite new at Qt.
void myClass
::formatTable(QTableWidget *table,
double minimum,
double maximum
) {
double val;
int i;
int nrow = table->rowCount();
table->setSortingEnabled(false);
for (i = 0; i < nrow; i++)
{
// obtain tableWidgetItem
item = table->item(i, 0);
// ensure item is not NULL, otherwise the application will crash!
if (!item)
{
continue;
}
// obtain cell string and convert to double value
val = item->text().toDouble();
// make sure entries like ".7" are converted to "0.7". otherwise, sorting will be incorrect
item
->setText
(QString::number(val
));
// TRIED THIS BUT DID NOT WORK
// item->setData(Qt::DisplayRole, val);
// check the low bound
if (val <= minimum)
{
item
->setText
(QString::number(minimum
));
}
// check the high bound
if (val >= maximum)
{
item
->setText
(QString::number(maximum
));
}
// justification
item->setTextAlignment(Qt::AlignRight);
}
table->sortItems(0, Qt::AscendingOrder);
}
void myClass::formatTable(QTableWidget *table, double minimum, double maximum)
{
double val;
int i;
int nrow = table->rowCount();
QTableWidgetItem *item;
table->setSortingEnabled(false);
for (i = 0; i < nrow; i++)
{
// obtain tableWidgetItem
item = table->item(i, 0);
// ensure item is not NULL, otherwise the application will crash!
if (!item)
{
continue;
}
// obtain cell string and convert to double value
val = item->text().toDouble();
// make sure entries like ".7" are converted to "0.7". otherwise, sorting will be incorrect
item->setText(QString::number(val));
// TRIED THIS BUT DID NOT WORK
// item->setData(Qt::DisplayRole, val);
// check the low bound
if (val <= minimum)
{
item->setText(QString::number(minimum));
}
// check the high bound
if (val >= maximum)
{
item->setText(QString::number(maximum));
}
// justification
item->setTextAlignment(Qt::AlignRight);
}
table->sortItems(0, Qt::AscendingOrder);
}
To copy to clipboard, switch view to plain text mode
Bookmarks