Results 1 to 9 of 9

Thread: segfault with QMessageBox in QAbstractItemModel::hasChildren

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default segfault with QMessageBox in QAbstractItemModel::hasChildren

    It appears if I display something inside the implementation of hasChildren on a QAbstractItemModel I get a segfault.
    This is easily reproducible with the simpletreemodel example included in the source tarball.
    Simply add the following method to treemodel.cpp (and corresponding method to the header as well).
    You can click through the first round of messages. After that when you mouse over one of the tree items it will give you another prompt. After clicking it you'll segfault.

    Qt Code:
    1. #include <string>
    2. #include <sstream>
    3.  
    4. // ...
    5.  
    6. bool TreeModel::hasChildren(const QModelIndex &parent) const
    7. {
    8. std::ostringstream oss;
    9. if (!parent.isValid()) {
    10. oss << "hasChildren root" << std::endl;
    11. } else {
    12. oss << "hasChildren (" << parent.row() << "," << parent.column() << ")" << std::endl;
    13. }
    14. oss << QAbstractItemModel::hasChildren(parent) << std::endl;
    15.  
    16. QMessageBox::information((QWidget*)QAbstractItemModel::parent(), "TITLE", QString::fromStdString(oss.str()));
    17. return QAbstractItemModel::hasChildren(parent);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    Since I'm sure it will be asked, let me explain why I'm doing this.
    I have an application which connects to a server but can also work off-line.
    While connected a user can drag an item from the server to their local cache.
    This does a shallow copy.
    If the user then closes and re-opens the application they will see their local copy of that top level item and 1 level of items under it.
    Expanding nodes any further than what the client has cached locally will cause a login dialog to appear to log into the server.

    What should I do?
    I'm open to suggestions.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: segfault with QMessageBox in QAbstractItemModel::hasChildren

    The model index might have become invalid.

    Basically model indexes are only considere valid as long as the model doesn't change or something else has happend, e.g. events have been processed.
    The latter happens inside the message box.

    You could try storing the index in a QPersitantModelIndex before you go into the nested event loop.

    Ideally of course the model wouldn't use a message box with a nested event loop at all.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: segfault with QMessageBox in QAbstractItemModel::hasChildren

    Thanks for the reply.

    I don't think a QPersistentModelIndex helps here.
    There is only a single line after the QMessageBox where I call...
    Qt Code:
    1. return QAbstractItemModel::hasChildren(parent);
    To copy to clipboard, switch view to plain text mode 
    If I were to store the result of this call as a boolean variable at the top of the method and just return it, then I'm not using the index at all after the QMessageBox and it still segfaults.

    Qt Code:
    1. bool TreeModel::hasChildren(const QModelIndex &parent) const
    2. {
    3. bool ret = QAbstractItemModel::hasChildren(parent);
    4. std::ostringstream oss;
    5. if (!parent.isValid()) {
    6. oss << "hasChildren root" << std::endl;
    7. } else {
    8. oss << "hasChildren (" << parent.row() << "," << parent.column() << ")" << std::endl;
    9. }
    10. oss << QAbstractItemModel::hasChildren(parent) << std::endl;
    11.  
    12. QMessageBox::information((QWidget*)QAbstractItemModel::parent(), "TITLE", QString::fromStdString(oss.str()));
    13. return ret;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by eric.frederich; 27th October 2015 at 18:10. Reason: added code

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: segfault with QMessageBox in QAbstractItemModel::hasChildren

    Hmm, what does the backtrace say where it crashes?

    Is the parent really a QWidget? A C style cast is always a chance for trouble.

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 23rd April 2015, 15:14
  2. QSqlQuery segfault
    By pdoria in forum Qt Programming
    Replies: 14
    Last Post: 16th March 2012, 21:01
  3. Replies: 4
    Last Post: 7th May 2010, 03:13
  4. Segfault
    By Dumbledore in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 07:31
  5. segfault
    By conexion2000 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2006, 12:34

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
  •  
Qt is a trademark of The Qt Company.