Results 1 to 6 of 6

Thread: Application crashed when QSortFilterProxyModel is used for QTableview sorting

  1. #1
    Join Date
    Oct 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question Application crashed when QSortFilterProxyModel is used for QTableview sorting

    I am using QTableview + QAbastractTableModel subclass to display order trading data. It works great. But when I use QSortFilterProxyModel for table sorting/filtering, the application crashes if I sort the rows and then add new data row. Sometimes, even a click in the table would result in a crash. I have not changed any codes in the Model . The following are the codes related. Please kindly help.

    VIEW:
    Qt Code:
    1. pTableView = new QTableView();
    2. pModel = new TradeManagerModel(0);
    3. proxyModel = new QSortFilterProxyModel(this);
    4. proxyModel->setSourceModel(pModel);
    5. proxyModel->setDynamicSortFilter(true);
    6. pTableView->setModel(proxyModel);
    7. pTableView->setSortingEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    MODEL: The data is stored in QMap<QString, QStringList> mGridData.
    Qt Code:
    1. int TradeManagerModel::rowCount(const QModelIndex & /*parent*/) const
    2. {
    3. return mGridData.size();
    4. }
    5.  
    6. int TradeManagerModel::columnCount(const QModelIndex & /*parent*/) const
    7. {
    8. return 10;
    9. }
    10.  
    11. QVariant TradeManagerModel::data(const QModelIndex &index, int role) const
    12. {
    13. if (role == Qt::DisplayRole)
    14. {
    15. return mGridData[mGridData.keys().at(index.row())].at(index.column());
    16. }
    17. return QVariant();
    18. }
    19.  
    20. QVariant TradeManagerModel::headerData(int section, Qt::Orientation orientation, int role) const
    21. {
    22.  
    23. if (role == Qt::DisplayRole)
    24. {
    25. if (orientation == Qt::Horizontal) {
    26. switch (section)
    27. {
    28. case 0:
    29. return QString("TIME");
    30. case 1:
    31. return QString("SYMBOL");
    32. case 2:
    33. return QString("B/S");
    34. case 3:
    35. return QString("QTY");
    36. case 4:
    37. return QString("PRICE");
    38. case 5:
    39. return QString("COMM");
    40. case 6:
    41. return QString("STRATEGYID");
    42. case 7:
    43. return QString("EXECID");
    44. case 8:
    45. return QString("ORDERID");
    46. case 9:
    47. return QString("");
    48. }
    49. }
    50. }
    51. return QVariant();
    52. }
    53.  
    54. void TradeManagerModel::onTradeItemUpdated(const TradeItem &tradeItem)
    55. {
    56. QString execId = tradeItem.execId;
    57.  
    58. QStringList tmpStrList;
    59. tmpStrList << tradeItem.tradeTimeStamp
    60. << tradeItem.symbol
    61. << tradeItem.action
    62. << QString::number(tradeItem.fillQty)
    63. << QString::number(tradeItem.fillPrice)
    64. << QString::number(tradeItem.commission)
    65. << tradeItem.orderRef
    66. << execId
    67. << tradeItem.orderId
    68. <<"";
    69.  
    70. if(!mGridData.contains(execId))
    71. {
    72. mGridData[execId] = tmpStrList;
    73. emit layoutChanged();
    74. }
    75.  
    76. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,936
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Application crashed when QSortFilterProxyModel is used for QTableview sorting

    Where do you check that the QModelIndex that is passed into any of your methods is valid? In particular, an invalid QModelIndex will have row = -1, col = -1, and I doubt if mGridData.keys().at( -1 ) will make the QMap very happy.

  3. #3
    Join Date
    Oct 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Application crashed when QSortFilterProxyModel is used for QTableview sorting

    Quote Originally Posted by d_stranz View Post
    Where do you check that the QModelIndex that is passed into any of your methods is valid? In particular, an invalid QModelIndex will have row = -1, col = -1, and I doubt if mGridData.keys().at( -1 ) will make the QMap very happy.
    d_stranz, thanks for help.
    I add debug as follow. But it turns out the app crashes without print the debug info. So it should be some other reason.

    Qt Code:
    1. QVariant TradeManagerModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (role == Qt::DisplayRole)
    4. {
    5. if(index.row() < 0 || index.column() < 0) qDebug() << "QModelIndex index invalid";
    6. return mGridData[mGridData.keys().at(index.row())].at(index.column());
    7. }
    8. return QVariant();
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Application crashed when QSortFilterProxyModel is used for QTableview sorting

    Run using the debugger, look at stack trace after crash.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,936
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Application crashed when QSortFilterProxyModel is used for QTableview sorting

    But it turns out the app crashes
    Of course it crashes. You are still executing the code that probably causes the crash. Writing out a qDebug() message doesn't change the fact that you are probably using an invalid QModelIndex to access something from a map.

    I do not know why the qDebug() message doesn't print - are you actually compiling and running the program in debug mode using the debugger? qDebug() does nothing in release mode. If you are running a debug program under the debugger, then possibly the crash is somewhere else.

    As jefftee says, the only way to really know what causes the crash is to run your program in the debugger and either set a breakpoint in the data() method and examine the index manually to see what it contains, or let the program crash and examine the stack to see where the crash occurs.

  6. #6
    Join Date
    Oct 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Application crashed when QSortFilterProxyModel is used for QTableview sorting

    Thanks all for the help. This is the stack after crash:

    0 ?? 0x7ffff661eae8
    1 QSortFilterProxyModel::flags(QModelIndex const&) const 0x7ffff661ee26
    2 QAbstractItemView::focusInEvent(QFocusEvent *) 0x7ffff790a601
    3 QWidget::event(QEvent *) 0x7ffff76f2809
    4 QFrame::event(QEvent *) 0x7ffff77ed39e
    5 QAbstractScrollArea::event(QEvent *) 0x7ffff786fb03
    6 QAbstractItemView::event(QEvent *) 0x7ffff7909c8b
    7 QApplicationPrivate::notify_helper(QObject *, QEvent *) 0x7ffff76b14dc
    8 QApplication::notify(QObject *, QEvent *) 0x7ffff76b6640
    9 QCoreApplication::notifyInternal(QObject *, QEvent *) 0x7ffff66556a3
    10 QApplicationPrivate::setFocusWidget(QWidget *, Qt::FocusReason) 0x7ffff76b3e7a
    11 QWidget::setFocus(Qt::FocusReason) 0x7ffff76eca50
    12 QApplication::setActiveWindow(QWidget *) 0x7ffff76b4392
    13 QApplicationPrivate::notifyActiveWindowChange(QWin dow *) 0x7ffff76b44a3
    14 QGuiApplicationPrivate:rocessActivatedEvent(QWindowSystemInterfacePrivate ::ActivatedWindowEvent *) 0x7ffff6e398e5
    15 QGuiApplicationPrivate:rocessWindowSystemEvent(QWindowSystemInterfacePriv ate::WindowSystemEvent *) 0x7ffff6e39b6d
    16 QWindowSystemInterface::sendWindowSystemEvents(QFl ags<QEventLoop::ProcessEventsFlag>) 0x7ffff6e1ec4f
    17 ?? 0x7fffef470170
    18 g_main_context_dispatch 0x7ffff4face04
    19 ?? 0x7ffff4fad048
    ... <More>

Similar Threads

  1. Replies: 10
    Last Post: 22nd March 2019, 15:03
  2. Qt application output says crashed
    By rawfool in forum Newbie
    Replies: 5
    Last Post: 3rd August 2015, 23:05
  3. Replies: 1
    Last Post: 18th July 2013, 10:26
  4. Replies: 0
    Last Post: 8th March 2011, 12:58
  5. Replies: 4
    Last Post: 2nd March 2011, 09:03

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.