QStandardItemModel / QTableView sorting issue
Hello all,
I have attached a simple program to demonstrate an issue I am seeing with the sorting of model data in a table view. The code is below and should compile/run. I am using QT Creator 1.2.1 on Win Vista 32 Business.
THe error I am seeing is: for example:
if the data in row 4 column 0 is changed from 7000->3300, the order of the displayed data changes in the table view, however it only seems to sort based on the first character in the sequence... Can someone please explain how one would go about sorting by the entire integer in column 0 on data changed?
Thanks,
AlphaWolfXV
main.cpp
Code:
#include <QtGui/QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
Dialog w;
w.show();
return a.exec();
}
dialog.cpp
Code:
#include "dialog.h"
{
table->setModel(model);
table->setSortingEnabled(true);
table->sortByColumn(0);
addData2Model();
layout->addWidget(table);
setMinimumSize(300,300);
setLayout(layout);
}
{
table->sortByColumn(0, Qt::AscendingOrder);
}
void Dialog::addData2Model()
{
QList<int> rpm;
QList<int> other;
rpm<<600<<3000<<5000<<7000<<10000<<14000;
other<<10<<10<<25<<25<<40<<40;
for(unsigned char i = 0; i < 6; i++)
{
}
qDebug()<<rpm<<other;
}
Dialog::~Dialog()
{
}
dialog.h
Code:
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui>
#include <QDialog>
{
Q_OBJECT
public:
~Dialog();
public slots:
private:
void addData2Model(void);
};
#endif // DIALOG_H
testTableViewSort.pro
Code:
#-------------------------------------------------
#
# Project created by QtCreator 2010-08-30T07:46:08
#
#-------------------------------------------------
TARGET = testTableViewSort
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
Thanks again!
Re: QStandardItemModel / QTableView sorting issue
You have to subclass QStandardItem. See here.
Re: QStandardItemModel / QTableView sorting issue
Or you can try this if you are not too fussy about display format:
Code:
for(unsigned char i = 0; i < 6; i++)
{
// put a QVariant(int) into the DisplayRole rather than a string
item->setData(rpm.at(i), Qt::DisplayRole);
model->setItem(i,0,item);
}