PDA

View Full Version : QTableView sorting



realdarkman71
27th November 2010, 23:17
Hi,

I have a QTableView with some columns, one column is with numbers only. If I sorting this column, it's 1, 11, 12 ... 2, 21, 22 ... but I need 1, 2, 3 ... 10, 11, 12 ...

How can I do this?

Thx!
Chris

waynew
28th November 2010, 23:08
Hi Chris, you need to make sure your database column is a number type, then it would sort correctly. It looks to me like it's a character or varchar type and that's why it is sorting like that.

realdarkman71
30th November 2010, 15:45
Ok, but how can I do that?

ChrisW67
30th November 2010, 23:49
What is the model underlying the view? Is it a QStandardItemModel, QSqlTableModel or something else?

waynew
1st December 2010, 01:02
Are doing this from a database? If so, how are you defining the column spec when you create the table?

realdarkman71
1st December 2010, 09:22
QStandardItemModel *model = new QStandardItemModel(this);

...

QList<QStandardItem *> column1, column2, column3, column4, column5;

foreach (...) {
column1.append(new QStandardItem("data1..."));
column2.append(new QStandardItem("data2..."));
...
}

model->appendColumn(column1);
model->appendColumn(column2);
...

model->setHeaderData(0, Qt::Horizontal, tr("Column1"));
model->setHeaderData(1, Qt::Horizontal, tr("Column2"));
...

ui->tableView->setModel(model);


column5 data are numbers only! "79962", "35776", "140480", "120384" ... but all QStrings!

waynew
1st December 2010, 12:20
Well, that is the problem then. Even if strings contain only numbers, they will still sort order as strings, like you are seeing.
What happens if you load column5 like:

column5.append(new QStandardItem(1234)); ?

realdarkman71
1st December 2010, 13:05
...this doesn't work unfortunately, the constructor allows no numbers!


QStandardItem::QStandardItem ()
QStandardItem::QStandardItem ( const QString & text )
QStandardItem::QStandardItem ( const QIcon & icon, const QString & text )
QStandardItem::QStandardItem ( int rows, int columns = 1 )

realdarkman71
1st December 2010, 16:50
I try to subclass QStandardItem, I think, this is the solution!



class NumberItem : public QStandardItem {

public:
NumberItem(const int number);

private:
int value;

public:
int type() const;
QStandardItem *clone() const;
QVariant data(int role = Qt::UserRole + 1) const;
};

NumberItem::NumberItem(const int number) : QStandardItem() {
value = number;
}

int NumberItem::type() const {
return QStandardItem::UserType;
}

QStandardItem *NumberItem::clone() const {
return new NumberItem(*this);
}

QVariant NumberItem::data(int) const {
return QVariant(number);
}


than I append this to the model:


column5.append(new NumberItem(12345));

It work's, the numbers are in the column 5 of tableView, but all number cells are checkable and disabled!? I don't know, why! setCheckable(false) and setEnabled(true) doesn't work!

ChrisW67
1st December 2010, 21:52
...this doesn't work unfortunately, the constructor allows no numbers!


QStandardItem::QStandardItem ()
QStandardItem::QStandardItem ( const QString & text )
QStandardItem::QStandardItem ( const QIcon & icon, const QString & text )
QStandardItem::QStandardItem ( int rows, int columns = 1 )

So create the item with the default constructor and then use QStandardItem::setData() to set a QVariant of type QVariant::Int (or Uint, LongLong etc.) as the data for the Qt::DisplayRole. Alternatively, set the integer data on the EditRole and use setSortRole on the model to set the sort role to the EditRole.

realdarkman71
1st December 2010, 22:20
So create the item with the default constructor and then use QStandardItem::setData() to set a QVariant of type QVariant::Int (or Uint, LongLong etc.) as the data for the Qt::DisplayRole.

Ok, this works!



QStandardItem *item = new QStandardItem();
item->setData(QVariant(12345), Qt::DisplayRole);
column5.append(item);




Alternatively, set the integer data on the EditRole and use setSortRole on the model to set the sort role to the EditRole.

How can I do this? Have you a code snippet for me?

ChrisW67
1st December 2010, 22:36
If setting the Qt::DisplayRole is working then just use that. However:


QStandardItem *item = new QStandardItem();
item->setData(QVariant(12345), Qt::EditRole);
column5.append(item);

and, elsewhere:


model->setSortRole(Qt::EditRole);

This makes all the columns in the model sort on the edit role.

realdarkman71
1st December 2010, 22:45
Ok, big thanks for your help!