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.