Results 1 to 3 of 3

Thread: QStandardItemModel / QTableView sorting issue

  1. #1
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation 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
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "dialog.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Dialog w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2.  
    3. Dialog::Dialog(QWidget *parent)
    4. : QDialog(parent)
    5. {
    6. model = new QStandardItemModel(5,2);
    7. table = new QTableView(this);
    8. table->setModel(model);
    9. table->setSortingEnabled(true);
    10. table->sortByColumn(0);
    11.  
    12. addData2Model();
    13. QVBoxLayout *layout = new QVBoxLayout(this);
    14. layout->addWidget(table);
    15. setMinimumSize(300,300);
    16. setLayout(layout);
    17.  
    18. connect(model, SIGNAL(itemChanged(QStandardItem*)),this,SLOT(reorderTables(QStandardItem*)));
    19.  
    20. }
    21. void Dialog::reorderTables(QStandardItem *item)
    22. {
    23. table->sortByColumn(0, Qt::AscendingOrder);
    24. }
    25. void Dialog::addData2Model()
    26. {
    27. QList<int> rpm;
    28. QList<int> other;
    29. rpm<<600<<3000<<5000<<7000<<10000<<14000;
    30. other<<10<<10<<25<<25<<40<<40;
    31. for(unsigned char i = 0; i < 6; i++)
    32. {
    33. model->setItem(i,0,new QStandardItem(QString::number(rpm.at(i))));
    34. model->setItem(i,1,new QStandardItem(QString::number(other.at(i))));
    35. }
    36. qDebug()<<rpm<<other;
    37. }
    38. Dialog::~Dialog()
    39. {
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 
    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QtGui>
    5. #include <QDialog>
    6.  
    7. class Dialog : public QDialog
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Dialog(QWidget *parent = 0);
    13. ~Dialog();
    14. public slots:
    15. void reorderTables(QStandardItem *item);
    16. private:
    17. QTableView *table;
    18. void addData2Model(void);
    19. };
    20.  
    21. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    testTableViewSort.pro
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2010-08-30T07:46:08
    4. #
    5. #-------------------------------------------------
    6.  
    7. TARGET = testTableViewSort
    8. TEMPLATE = app
    9.  
    10.  
    11. SOURCES += main.cpp\
    12. dialog.cpp
    13.  
    14. HEADERS += dialog.h
    To copy to clipboard, switch view to plain text mode 

    Thanks again!

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QStandardItemModel / QTableView sorting issue

    You have to subclass QStandardItem. See here.

  3. The following user says thank you to norobro for this useful post:

    AlphaWolfXV (30th August 2010)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStandardItemModel / QTableView sorting issue

    Or you can try this if you are not too fussy about display format:
    Qt Code:
    1. for(unsigned char i = 0; i < 6; i++)
    2. {
    3. item = new QStandardItem;
    4. // put a QVariant(int) into the DisplayRole rather than a string
    5. item->setData(rpm.at(i), Qt::DisplayRole);
    6. model->setItem(i,0,item);
    7. model->setItem(i,1,new QStandardItem(QString::number(other.at(i))));
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 31st August 2010 at 06:57.

  5. The following user says thank you to ChrisW67 for this useful post:

    AlphaWolfXV (31st August 2010)

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. Sorting a QStringlist issue
    By George Neil in forum Qt Programming
    Replies: 4
    Last Post: 30th September 2009, 05:23
  3. qtableview QStandardItemModel read only
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2008, 16:42
  4. Extract QStandardItemModel from QTableView
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2008, 08:34
  5. Sorting QTableView
    By Jimmy2775 in forum Qt Programming
    Replies: 7
    Last Post: 9th February 2006, 16:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.