Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractListModel > call to endInsertRows() from NON Qt (GUI) thread results in crash
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.4k Views
  • 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 Offline
    B Offline
    bkamps
    wrote on last edited by
    #1

    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
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      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
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved