Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QAbstractListModel > call to endInsertRows() from NON Qt (GUI) thread results in crash

    General and Desktop
    2
    2
    3247
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      bkamps last edited by

      Hi,

      I have a derived class from QAbstractListModel and I want to add elements to this list. So I do the following:

      @
      void addElement()
      beginInsertRows(QModelIndex(), myList.count(), myList.count());
      myList.push_back(myElement);
      endInsertRows();
      @

      This works when I call addElement() from the Qt main thread. When I call this function from a NON Qt thread (normal pthread) the application crashes on endInsertRows() method.

      I have read that endInsertRows() emits a signal and this shouldn't be done from a NON Qt thread. For now I have "fixed" this by connecting addElement() to a signal that's used in the NON Qt thread and using Qt:QueuedConnection option.

      Is there another way to do this?

      1 Reply Last reply Reply Quote 0
      • Z
        ZapB last edited by

        That seems a reasonable solution to me. If you know that you will always call this function from another thread then you can hide the use of signals/slots/queued connections within the class:

        @
        MyModel::MyModel( QObject* parent )
        : QAbstractListModel( parent )
        {
        connet( this, SIGNAL( addElementRequested() ), this, SLOT( doAddElement() ) );
        ...
        }

        void MyModel::addElement()
        {
        emit addElementRequested();
        }

        void MyModel::doAddElement()
        {
        beginInsertRows(QModelIndex(), myList.count(), myList.count());
        myList.push_back(myElement);
        endInsertRows();
        }
        @

        where doAddElement() is a private slot. WIth the above if you call addElement() from the main GUI thread it will be synchronous as it will use a direct connection. If you call it from a worker thread then it will use a queued connection and happen asynchronously.

        HTH.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply Reply Quote 0
        • First post
          Last post