Results 1 to 5 of 5

Thread: error in qt concurrent

  1. #1
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default error in qt concurrent

    hi,
    I am trying some example on Qt::Concurrent using this example http://codeprogress.com/cpp/librarie...hp#.VJgBKf8Fzw but application crashes for some reason.Can any one help on this.

    It says
    cannot send events to objects owned by different thread
    .What should i do to overcome this.

    thanks,
    Last edited by anbu01; 22nd December 2014 at 11:41. Reason: spelling corrections

  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: error in qt concurrent

    That example is broken in so many ways that it is hard to believe anyone actually posted it on the Internet.

    Luckily the Qt documentation has an example as well: http://qt-project.org/doc/qt-4.8/qtc...nfunction.html

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: error in qt concurrent

    hi,
    That example is good but if i use widgets(using the documentation example) i get the same error.Is the problem because i try to access widgets from different thread.so how we can make make a responsive Gui where data runs in background.I have attached the code that i tried.
    Qt Code:
    1. void colorizeButton1()
    2. {
    3. int index = 0;
    4. while(true)
    5. {
    6. if(index%2 == 0)
    7. {
    8. button1->setPalette(*(new QPalette(Qt::green)));
    9. }
    10. else
    11. {
    12. button1->setPalette(*(new QPalette(Qt::yellow)));
    13. }
    14. index++;
    15. QThread::sleep(1);
    16. }
    17. }
    18.  
    19.  
    20. int main(int argc, char **argv)
    21. {
    22. QApplication app(argc, argv);
    23.  
    24. QMainWindow *window = new QMainWindow();
    25.  
    26. QWidget *central = new QWidget(window);
    27. button1 = new QPushButton(central);
    28.  
    29. QHBoxLayout* layout = new QHBoxLayout(centralWidget);
    30.  
    31. layout->addWidget(button1);
    32.  
    33. window->setCentralWidget(central);
    34.  
    35. QFuture<void> f1 = QtConcurrent::run(colorizeButton1);
    36.  
    37. f1.waitForFinished();
    38. window->show();
    39.  
    40. app.exec();
    41. }
    To copy to clipboard, switch view to plain text mode 
    thanks,
    Last edited by anbu01; 22nd December 2014 at 12:41.

  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: error in qt concurrent

    Quote Originally Posted by anbu01 View Post
    That example is good
    I am sorry, but no, it is not.

    An example that does not work is not a good example.
    An example that leaks memory like crazy is not a good example.
    An example that ignores the requirements for access to UI resources in multithreading context is not a good example.

    The usage of global variables does not help either.

    Quote Originally Posted by anbu01 View Post
    I have attached the code that i tried.
    So you are taking the worst possible example and attempt it make it even worse by waiting for the end of a never ending function?

    I am afraid we have a very different definition of "good" when it comes to examples on programming.

    Cheers,
    _

  5. #5
    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: error in qt concurrent

    The example is really bad indeed... Using QTimer would be sufficient to obtain the same functionality without any threading.

    By the way, in some conditions the discussed example function might not even execute or it can break other functionality using Qt Concurrent by starving jobs.
    Last edited by wysota; 23rd December 2014 at 07:07.
    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.


Similar Threads

  1. Prevent concurrent access from different threads?
    By agarny in forum Qt Programming
    Replies: 5
    Last Post: 26th February 2013, 12:44
  2. Concurrent file downloading
    By Alir3z4 in forum Qt Programming
    Replies: 14
    Last Post: 21st February 2012, 15:56
  3. how to debugger Qt concurrent with GDB
    By learning_qt in forum Qt Programming
    Replies: 0
    Last Post: 8th November 2010, 12:13
  4. concurrent signals handling
    By s_stavrev in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2010, 15:16
  5. Concurrent progress reporting
    By chezifresh in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2008, 08:47

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.