Results 1 to 4 of 4

Thread: QTableView massive model data updates issue

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

    Default QTableView massive model data updates issue

    Hi,

    Just learning QT as an option for replacing existing WPF application which shows horizontally scrolled set of mid size grids (10 grids, 100 rows x 20 cols) with custom cell contents (some numbers, arrows, signs ...). Grid contents are refreshed about 2-3 times per second, almost all cells. Updates are done through model.

    The issue is the following, application crashes randomly and I think it is related to QVariant object manipulation, check/set/get variant from model. Here is the sample code that is crashing after running for some time (10 sec to 2-3 mins):

    Here is the code:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtCore>
    3. #include <QWidget>
    4. #include <QStandardItemModel>
    5. #include <QTableView>
    6. #include <QScrollArea>
    7. #include <QHBoxLayout>
    8. #include <QHeaderView>
    9. #include <QSizePolicy>
    10. #include <QMainWindow>
    11. #include <iostream>
    12. #include <QtGui>
    13. #include <stdlib.h>
    14.  
    15. using namespace std;
    16.  
    17. class MyObject {
    18.  
    19. };
    20.  
    21. Q_DECLARE_METATYPE(MyObject)
    22.  
    23.  
    24. class PaintDelegate : public QStyledItemDelegate {
    25.  
    26. public:
    27. PaintDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
    28.  
    29. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    30. const QModelIndex &index) const
    31. {
    32. if (qVariantCanConvert<MyObject>(index.data(Qt::UserRole + 1))) {
    33. MyObject myObj = qVariantValue<MyObject>(index.data(Qt::UserRole + 1));
    34.  
    35. painter->save();
    36. painter->translate(option.rect.x() + 5, option.rect.y() + 10);
    37. painter->drawText(0, 0, QString("test %1").arg(rand()%1000));
    38. painter->restore();
    39.  
    40. } else {
    41. QStyledItemDelegate::paint(painter, option, index);
    42. }
    43. }
    44.  
    45. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    46. {
    47. qVariantCanConvert<MyObject>(index.data(Qt::UserRole + 1));
    48. return QSize(50, 10);
    49. }
    50. };
    51.  
    52. int main(int argc, char** argv)
    53. {
    54. QApplication app( argc, argv );
    55.  
    56. QStandardItem * item = new QStandardItem("item 1");
    57. item->setData(qVariantFromValue(MyObject()));
    58. model->setItem(0, 0, item);
    59. QTableView * tableView2 = new QTableView();
    60. tableView2->setModel(model);
    61. tableView2->setItemDelegate(new PaintDelegate);
    62. tableView2->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    63. tableView2->show();
    64.  
    65. extern void updateModel(QStandardItemModel*);
    66. QtConcurrent::run(updateModel, model);
    67.  
    68. return app.exec();
    69. }
    70.  
    71. void updateModel(QStandardItemModel* model){
    72. for (;;){
    73. model->item(0, 0)->setData(qVariantFromValue(MyObject()));
    74. usleep(1000);
    75. }
    76. }
    To copy to clipboard, switch view to plain text mode 

    Here is the error:

    Qt Code:
    1. *** glibc detected *** /home/bob/Documents/TableViewTest-build-desktop/TableViewTest: free(): invalid pointer: 0x00007f57d000a190 ***
    2. ======= Backtrace: =========
    3. /lib/libc.so.6(+0x774b6)[0x7f57ea7cd4b6]
    4. /lib/libc.so.6(cfree+0x73)[0x7f57ea7d3c83]
    5. /home/bob/Documents/TableViewTest-build-desktop/TableViewTest[0x4031e8]
    6. /usr/lib/libQtCore.so.4(_ZN9QMetaType7destroyEiPv+0x62)[0x7f57eb6089a2]
    7. /usr/lib/libQtCore.so.4(+0x18bfe2)[0x7f57eb620fe2]
    8. /home/bob/Documents/TableViewTest-build-desktop/TableViewTest[0x402db7]
    9. /usr/lib/libQtGui.so.4(+0x73f04d)[0x7f57ec07004d]
    10. /usr/lib/libQtGui.so.4(_ZN10QTableView10paintEventEP11QPaintEvent+0x913)[0x7f57ec071753]
    11. /usr/lib/libQtGui.so.4(_ZN7QWidget5eventEP6QEvent+0x59e)[0x7f57ebb3ec2e]
    12. /usr/lib/libQtGui.so.4(_ZN6QFrame5eventEP6QEvent+0x26)[0x7f57ebf0a5e6]
    13. /usr/lib/libQtGui.so.4(_ZN17QAbstractItemView13viewportEventEP6QEvent+0x3eb)[0x7f57ec043a8b]
    14. /usr/lib/libQtCore.so.4(_ZN23QCoreApplicationPrivate29sendThroughObjectEventFiltersEP7QObjectP6QEvent+0x87)[0x7f57eb5fd507]
    15. /usr/lib/libQtGui.so.4(_ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent+0x7c)[0x7f57ebae8fac]
    16. /usr/lib/libQtGui.so.4(_ZN12QApplication6notifyEP7QObjectP6QEvent+0x14d)[0x7f57ebaeeaed]
    17. /usr/lib/libQtCore.so.4(_ZN16QCoreApplication14notifyInternalEP7QObjectP6QEvent+0x8c)[0x7f57eb5fdcdc]
    18. /usr/lib/libQtGui.so.4(_ZN14QWidgetPrivate10drawWidgetEP12QPaintDeviceRK7QRegionRK6QPointiP8QPainterP19QWidgetBackingStore+0x3bd)[0x7f57ebb4567d]
    19. /usr/lib/libQtGui.so.4(+0x3e83a6)[0x7f57ebd193a6]
    20. /usr/lib/libQtGui.so.4(_ZN14QWidgetPrivate16syncBackingStoreEv+0x80)[0x7f57ebb38730]
    21. /usr/lib/libQtGui.so.4(_ZN7QWidget5eventEP6QEvent+0xc65)[0x7f57ebb3f2f5]
    22. /usr/lib/libQtGui.so.4(_ZN6QFrame5eventEP6QEvent+0x26)[0x7f57ebf0a5e6]
    23. /usr/lib/libQtGui.so.4(_ZN19QAbstractScrollArea5eventEP6QEvent+0x8b)[0x7f57ebf9871b]
    24. /usr/lib/libQtGui.so.4(_ZN17QAbstractItemView5eventEP6QEvent+0x9b)[0x7f57ec03c51b]
    25. /usr/lib/libQtGui.so.4(_ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent+0xac)[0x7f57ebae8fdc]
    26. /usr/lib/libQtGui.so.4(_ZN12QApplication6notifyEP7QObjectP6QEvent+0x14d)[0x7f57ebaeeaed]
    27. /usr/lib/libQtCore.so.4(_ZN16QCoreApplication14notifyInternalEP7QObjectP6QEvent+0x8c)[0x7f57eb5fdcdc]
    28. /usr/lib/libQtCore.so.4(_ZN23QCoreApplicationPrivate16sendPostedEventsEP7QObjectiP11QThreadData+0x2d2)[0x7f57eb600c22]
    29. /usr/lib/libQtCore.so.4(+0x195653)[0x7f57eb62a653]
    30. /lib/libglib-2.0.so.0(g_main_context_dispatch+0x1f2)[0x7f57e9deb342]
    31. /lib/libglib-2.0.so.0(+0x442a8)[0x7f57e9def2a8]
    32. /lib/libglib-2.0.so.0(g_main_context_iteration+0x6c)[0x7f57e9def45c]
    33. /usr/lib/libQtCore.so.4(_ZN20QEventDispatcherGlib13processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE+0x73)[0x7f57eb62a193]
    34. /usr/lib/libQtGui.so.4(+0x26aa4e)[0x7f57ebb9ba4e]
    35. /usr/lib/libQtCore.so.4(_ZN10QEventLoop13processEventsE6QFlagsINS_17ProcessEventsFlagEE+0x32)[0x7f57eb5fca02]
    36. /usr/lib/libQtCore.so.4(_ZN10QEventLoop4execE6QFlagsINS_17ProcessEventsFlagEE+0xdc)[0x7f57eb5fcdec]
    37. /usr/lib/libQtCore.so.4(_ZN16QCoreApplication4execEv+0xbb)[0x7f57eb600ebb]
    38. /home/bob/Documents/TableViewTest-build-desktop/TableViewTest[0x4025e6]
    39. /lib/libc.so.6(__libc_start_main+0xfe)[0x7f57ea774d8e]
    40. /home/bob/Documents/TableViewTest-build-desktop/TableViewTest[0x4022c9]
    To copy to clipboard, switch view to plain text mode 

    Please advice or correct me if I am using View/Model approach incorrectly.

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

    Default Re: QTableView massive model data updates issue

    Simplified version of the code which gives the same error:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtCore>
    3. #include <QStandardItemModel>
    4. #include <QTableView>
    5. #include <QtGui>
    6.  
    7. using namespace std;
    8.  
    9. class MyObject {
    10.  
    11. };
    12.  
    13. Q_DECLARE_METATYPE(MyObject)
    14.  
    15.  
    16. class PaintDelegate : public QStyledItemDelegate {
    17.  
    18. public:
    19. PaintDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
    20.  
    21. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    22. const QModelIndex &index) const
    23. {
    24. if (qVariantCanConvert<MyObject>(index.data(Qt::UserRole + 1))) {
    25. MyObject myObj = qVariantValue<MyObject>(index.data(Qt::UserRole + 1));
    26. } else {
    27. QStyledItemDelegate::paint(painter, option, index);
    28. }
    29. }
    30.  
    31. };
    32.  
    33. int main(int argc, char** argv)
    34. {
    35. QApplication app( argc, argv );
    36.  
    37. QStandardItem * item = new QStandardItem("item 1");
    38. item->setData(qVariantFromValue(MyObject()));
    39. model->setItem(0, 0, item);
    40. QTableView * tableView2 = new QTableView();
    41. tableView2->setModel(model);
    42. tableView2->setItemDelegate(new PaintDelegate);
    43. tableView2->show();
    44.  
    45. extern void updateModel(QStandardItemModel*);
    46. QtConcurrent::run(updateModel, model);
    47.  
    48. return app.exec();
    49. }
    50.  
    51. void updateModel(QStandardItemModel* model){
    52. for (;;){
    53. model->item(0, 0)->setData(qVariantFromValue(MyObject()));
    54. }
    55. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QTableView massive model data updates issue

    The original code didn't fail here on 32-bit Linux after a few minutes of running, the new code crashes immediately probably because the delay is no longer in your update loop. It could be a thread synchronisation issue. Have you considered locking? Have you tried using a QTimer and dispensing with the separate thread?

    Are you intending to paint anything if the item contains a MyObject?

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

    Default Re: QTableView massive model data updates issue

    Hi, Thanks for the try.

    This is just an example to show the error. If this is a defect inside qt maybe it should be logged as such so someone could fix it.

    In my real app I used two QTimers and a synchronized temporary store (QSet) to accumulate changes. First QTimer puts data into the store (high frequency) another updates UI (less frequently). This approach has not crashed yet, maybe because I am not touching model at such a high rate.

    I have 63bit Linux. The example above crashed once on 32bit windows virtual box.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 19:39
  2. Replies: 1
    Last Post: 15th October 2010, 22:30
  3. Replies: 5
    Last Post: 21st March 2009, 09:10
  4. QTableWidget massive select -> massive signals
    By zarkzervo in forum Qt Programming
    Replies: 0
    Last Post: 16th March 2009, 14:01
  5. Model/View framework: streaming data in a QTableView
    By yannickt in forum Qt Programming
    Replies: 6
    Last Post: 24th October 2008, 00:06

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.