AlphaWolfXV
30th August 2010, 17:44
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
#include <QtGui/QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
model = new QStandardItemModel(5,2);
table = new QTableView(this);
table->setModel(model);
table->setSortingEnabled(true);
table->sortByColumn(0);
addData2Model();
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(table);
setMinimumSize(300,300);
setLayout(layout);
connect(model, SIGNAL(itemChanged(QStandardItem*)),this,SLOT(reor derTables(QStandardItem*)));
}
void Dialog::reorderTables(QStandardItem *item)
{
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++)
{
model->setItem(i,0,new QStandardItem(QString::number(rpm.at(i))));
model->setItem(i,1,new QStandardItem(QString::number(other.at(i))));
}
qDebug()<<rpm<<other;
}
Dialog::~Dialog()
{
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui>
#include <QDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void reorderTables(QStandardItem *item);
private:
QStandardItemModel *model;
QTableView *table;
void addData2Model(void);
};
#endif // DIALOG_H
testTableViewSort.pro
#-------------------------------------------------
#
# Project created by QtCreator 2010-08-30T07:46:08
#
#-------------------------------------------------
TARGET = testTableViewSort
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
Thanks again!
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
#include <QtGui/QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
dialog.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
model = new QStandardItemModel(5,2);
table = new QTableView(this);
table->setModel(model);
table->setSortingEnabled(true);
table->sortByColumn(0);
addData2Model();
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(table);
setMinimumSize(300,300);
setLayout(layout);
connect(model, SIGNAL(itemChanged(QStandardItem*)),this,SLOT(reor derTables(QStandardItem*)));
}
void Dialog::reorderTables(QStandardItem *item)
{
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++)
{
model->setItem(i,0,new QStandardItem(QString::number(rpm.at(i))));
model->setItem(i,1,new QStandardItem(QString::number(other.at(i))));
}
qDebug()<<rpm<<other;
}
Dialog::~Dialog()
{
}
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui>
#include <QDialog>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void reorderTables(QStandardItem *item);
private:
QStandardItemModel *model;
QTableView *table;
void addData2Model(void);
};
#endif // DIALOG_H
testTableViewSort.pro
#-------------------------------------------------
#
# Project created by QtCreator 2010-08-30T07:46:08
#
#-------------------------------------------------
TARGET = testTableViewSort
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
Thanks again!