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[])
{
Dialog w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
dialog.cpp
#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()
{
}
#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(reorderTables(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()
{
}
To copy to clipboard, switch view to plain text mode
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui>
#include <QDialog>
{
Q_OBJECT
public:
~Dialog();
public slots:
private:
void addData2Model(void);
};
#endif // 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
To copy to clipboard, switch view to plain text mode
testTableViewSort.pro
#-------------------------------------------------
#
# Project created by QtCreator 2010-08-30T07:46:08
#
#-------------------------------------------------
TARGET = testTableViewSort
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
#-------------------------------------------------
#
# Project created by QtCreator 2010-08-30T07:46:08
#
#-------------------------------------------------
TARGET = testTableViewSort
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
To copy to clipboard, switch view to plain text mode
Thanks again!
Bookmarks