Results 1 to 5 of 5

Thread: QModel set data to item very slow

  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QModel set data to item very slow

    I have implemented my own (very simple though) QStandardItemModel-qtableview.

    Qt Code:
    1. #ifndef PARAMETERMODEL_H
    2. #define PARAMETERMODEL_H
    3. #include <QtGui>
    4. #include <QtSql/QSqlTableModel>
    5. #include <QtSql/QSqlRelationalDelegate>
    6. #include <QtSql/QSqlQuery>
    7. #include <QMessageBox>
    8. #include <QComboBox>
    9. #include <QWidget>
    10.  
    11.  
    12. #include <comboboxDelegates.h>
    13. #include <spinboxdelegates.h>
    14.  
    15. class ParameterModel : public QWidget
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. ParameterModel( QWidget *parent = 0);
    21.  
    22. QTableView* tableView;
    23.  
    24.  
    25.  
    26. public slots:
    27. void addNewRow();
    28. void addNewRow2();
    29.  
    30. void deleteRow(int);
    31. private:
    32. QVBoxLayout* vboxlayout;
    33.  
    34. ActionComboBoxDelegate *actionComboBoxDelegate;
    35. TypeComboBoxDelegate *typeComboBoxDelegate;
    36. StatusComboBoxDelegate *statusComboBoxDelegate;
    37.  
    38. VrotSpinBoxDelegate *vrotSpinBoxDelegate;
    39. SigmaSpinBoxDelegate *sigmaSpinBoxDelegate;
    40. GammaSpinBoxDelegate *gammaSpinBoxDelegate;
    41. XiSpinBoxDelegate *xiSpinBoxDelegate;
    42. SemitSpinBoxDelegate *semitSpinBoxDelegate;
    43. DlradSpinBoxDelegate *dlradSpinBoxDelegate;
    44. LlabSpinBoxDelegate *llabSpinBoxDelegate;
    45. AWSpinBoxDelegate *aWSpinBoxDelegate;
    46. EiSpinBoxDelegate *eiSpinBoxDelegate;
    47.  
    48.  
    49. };
    50. #endif // PARAMETERMODEL_H
    To copy to clipboard, switch view to plain text mode 
    I usually need 3-5 of these, which I append them to a QVector

    Qt Code:
    1. QVector<ParameterModel*>* modelVector;
    To copy to clipboard, switch view to plain text mode 

    My problem is that when I am trying to set the data to the items, it gets slow

    Qt Code:
    1. QElapsedTimer t;
    2. t.start();
    3. if ( column_index < 4 )
    4. modelVector->at(model_index)->model->item(row_index,column_index)->setData(line);
    5. qDebug()<<t.elapsed();
    6. t.start();
    7. if ( column_index > 4 )
    8. modelVector->at(model_index)->model->item(row_index,column_index)->setData(line.toDouble());
    9. qDebug()<<t.elapsed();
    To copy to clipboard, switch view to plain text mode 

    The timers here give values 500-600. The strange thing is that the output is usually like this (I run it multiple times)
    0
    593
    ....
    577
    0
    ....
    562
    0
    etc,etc.
    What can I do to make it faster?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QModel set data to item very slow

    I have implemented my own (very simple though) QStandardItemModel-qtableview.
    Well, no you haven't. You've implemented a QWidget, which for some strange reason you call ParameterModel, and which contains public member variables of types pointer to QStandardItemModel and pointer to QTableView, along with a bunch of pointers to child widgets.

    Then, floating out in interplanetary space, you have a pointer to a QVector of pointers to ParameterModel instances, and in another piece of interplanetary space, you have some code that uses a pointer from that vector to access a model that you set some data into.

    You don't bother to show us how you create that vector, how you create the instances of the ParameterModel widgets inside it or the children of those widgets, how you arrive at the model_index, row_index, and column_index values for the item you are trying to change. Just code floating in space, drifting along, trying to stay out of the way of planets, asteroids, and comets as it spirals into the Sun...

    How are we supposed to be able to understand what might be wrong, much less help?

  3. #3
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QModel set data to item very slow


    well...ok you are totally right, my intention was to focus on the setData function. Otherwise I had to present perhaps too many lines of code that probably noone would get into the trouble to read.
    Anyway, the fact is that I am implementing a big-idea-math tool, which I had never realized its actual complexity (perhaps not even now). It is when you are a part of a team and every now and then it is revealed to you another very-very important part of the algorithm that had never been mentioned before

    When I started I didn't know the existence of QT, that's why all these come from outer space and seem loosely designed. Miraculously, after really hard time in front of the computer it works so far fine.

    You've implemented a QWidget, which for some strange reason you call ParameterModel, and which contains public member variables of types pointer to QStandardItemModel and pointer to QTableView, along with a bunch of pointers to child widgets.
    Regardless the fact that I wanted a custom QStandardItemModel and I implemented a QWidget (I really can't remember why I did this, I think I needed some functions of QWidget that were not available in QStandardItemModel, has this any performance effect?) the bunch of pointers are my delegates. All the examples I found were declaring the delegates in this way.

    Then, floating out in interplanetary space, you have a pointer to a QVector of pointers to ParameterModel instances, and in another piece of interplanetary space, you have some code that uses a pointer from that vector to access a model that you set some data into.
    Come on, this can be easily guessed , I have a Vector which stores ParameterModels dynamically. While on run the user can create or destroy these models.

    You don't bother to show us how you create that vector, how you create the instances of the ParameterModel widgets inside it or the children of those widgets,
    Qt Code:
    1. modelVector = new QVector<ParameterModel*>(0);
    2. .............
    3. ParameterModel* m = new ParameterModel;
    4. modelVector->append(m);
    5. ...............
    To copy to clipboard, switch view to plain text mode 

    how you arrive at the model_index, row_index, and column_index values for the item you are trying to change.
    this is a detail, while the program runs, these indexes work well,trust me!

    Just code floating in space, drifting along, trying to stay out of the way of planets, asteroids, and comets as it spirals into the Sun...
    well, I have to tell you that the last half an hour I 've abandoned the keyboard and I have caught my guitar to write a song with these lyrics

    Now...can I make this thing set the model data a bit faster?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QModel set data to item very slow

    How have you identified this is setData which is slow and not the view update which comes after it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QModel set data to item very slow

    How have you identified this is setData which is slow and not the view update which comes after it?
    Exactly why more detail is needed. If the QElapsedTimer instrumentation is in the code as originally posted, then I don't think the code re-enters the event loop to update the GUI until after whatever is happening in the code that calls setData() finishes.

    QStandardItemModel does override setData(), but would it internally force events to be processed?

Similar Threads

  1. Replies: 5
    Last Post: 30th October 2014, 12:21
  2. Replies: 5
    Last Post: 2nd July 2012, 21:49
  3. Slow loading of data into QTableWidget
    By KenJustKen in forum Qt Programming
    Replies: 5
    Last Post: 11th April 2012, 16:50
  4. Replies: 9
    Last Post: 11th April 2011, 11:05
  5. some of QModel to QModel
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2007, 08:54

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.