Results 1 to 4 of 4

Thread: None-GUI thread call widget method cause error

  1. #1
    Join Date
    May 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default None-GUI thread call widget method cause error

    I use QListWidget for logging.
    If call this method within GUI thread, it runs OK.
    But if call this method from none-GUI thread, it throws error below:

    QObject::connect: Cannot queue arguments of type 'QItemSelection'
    (Make sure 'QItemSelection' is registered using qRegisterMetaType().)

    The problem is I had never connect signal/slot with QItemSelection parameter.
    I guess it happend after executing setCurrentItem() and setSelected().
    So, how to call this method from none-GUI thread succussfully?

    void MainWindow::updateLog(const QString& log)
    {
    QListWidgetItem* newItem = new QListWidgetItem;
    newItem->setText(log);
    ui->logList->addItem(newItem); // QListWidget
    ui->logList->setCurrentItem(newItem);
    ui->logList->scrollToItem(newItem);
    newItem->setSelected(true);
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: None-GUI thread call widget method cause error

    You can't access any widgets directly from worker threads. Use signals and slots that are thread-safe to communicate with the main thread so that widgets are called from within its context (in other words make your updateLog() a slot and invoke it using signals and slots or QMetaObject::invokeMethod() with Qt::QueuedConnection).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: None-GUI thread call widget method cause error

    Use signals/slots is a good idea, but it slower than direct call when response speed is my first consideration. A simple example is vedio data transfered via callback function, if use signals/slots, the real-time can not be guaranteed or flicker will appear too. So how to solve speed need?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: None-GUI thread call widget method cause error

    Signals and slots are not slower than directly calling a function, that's just in your head or you did something very inefficiently.
    But, you can also use events.

    By the way, updating a list in real time IS inefficiently. The users of your program will not be able to follow it anyway.

Similar Threads

  1. Replies: 6
    Last Post: 15th April 2010, 20:02
  2. Signal/slot or direct method call within a class
    By mike_the_tv in forum Newbie
    Replies: 6
    Last Post: 11th March 2010, 18:49
  3. Why can't I call base public method?
    By lni in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2009, 20:37
  4. Replies: 6
    Last Post: 3rd September 2008, 14:27
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

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.