Results 1 to 20 of 23

Thread: Multithreading & GUI

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by wysota View Post
    It will not affect your GUI.


    You are probably not running an event loop in that thread.


    Sure, just state what the actual task is
    I am new to qt. I am not able to find any good resource in multithreading. I just want to do fuzzing on web applictions which i was assigned to do testing.

    Here is exactly what i wanted:
    I need to send more than 1000 or 10,000 requests to a web application without affecting the GUI(i tried to run 10,000 request in main thread, it's affecting the gui, not able to access any other items). so i wanted to run the QNAM &loop in a separate thread or any other way in such a way that it should not affect the other tasks. Can you give me a sample snippet or tutorial links to achieve this

    Thank you

  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: Multithreading & GUI

    One way to do it is to have a worker object and move it to a QThread

    Qt Code:
    1. Worker *worker = new Worker(); // class derived from QObject
    2.  
    3. QThread *thread = new QThread();
    4. worker->moveToThread(thread);
    5. connect(thread, SIGNAL(started()), worker, SLOT(start())); // Worker needs a slot that start the work
    6. thread->start();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    qthread (3rd August 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by anda_skoa View Post
    One way to do it is to have a worker object and move it to a QThread

    Qt Code:
    1. Worker *worker = new Worker(); // class derived from QObject
    2.  
    3. QThread *thread = new QThread();
    4. worker->moveToThread(thread);
    5. connect(thread, SIGNAL(started()), worker, SLOT(start())); // Worker needs a slot that start the work
    6. thread->start();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Thanks it worked for me

    How can i distinguish the responses?! I'm sending 1000 requests.
    I can use response->request().url() function to get the corresponding url. However i want to set a unique number for each request and display along with corresponding response. How to do that in QNAM?

  5. #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: Multithreading & GUI

    One option would be to have a mapping from the QNetworkReply pointer to your reference data, e.g. using QHash.

    In your case where you only need a single number, it is probably quicker to just set this value as a dynamic property on the QNetworkReply object.
    See QObject::setProperty().

    Cheers,
    _

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

    Default Re: Multithreading & GUI

    Just remember that QNetworkReply is a QIODevice which in turn is a QObject thus accessing it from more than one thread is not safe. Push the object to the main thread before you try accessing it. The original thread will probably not try accessing the object after finished() is emitted but it is better to be safe than sorry.

    Which of course doesn't negate the fact that there is probably no point in using any extra threads here..
    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.


  7. #6
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    I will be thankful if anyone give me a sample snippet to distinguish each QNAM response from others

  8. #7
    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: Multithreading & GUI

    Each request is handled by a QNetworkReply object. So each request has a different, distinguishable, pointer to a QNetworkReply instance.
    Each instance has the QNetworkRequest that it was created for.

    Qt Code:
    1. void MyClass::networkRequestFinished(QNetworkReply *reply)
    2. {
    3. qDebug() << "Request for" << reply->request().url() << "finished" << (reply->error() != QNetworkReply::NoError ? "with an error" : "without error");
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  9. #8
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Thanks is it possible to pass a unique value to QNAM and access them via QNetworkReply other than URL ?!

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

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    Thanks is it possible to pass a unique value to QNAM and access them via QNetworkReply other than URL ?!
    What kind of unique value?
    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.


  11. #10
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multithreading & GUI

    Quote Originally Posted by wysota View Post
    What kind of unique value?
    Strings. for example hello1, hello2

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

    Default Re: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    Strings. for example hello1, hello2
    I was rather asking for the reason and meaning of those values rather than their datatype.
    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.


  13. #12
    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: Multithreading & GUI

    Quote Originally Posted by qthread View Post
    Thanks is it possible to pass a unique value to QNAM and access them via QNetworkReply other than URL ?!
    QNetworkReply is a QObject derived class, you can set any value as a dynamic property.
    See QObject::setProperty.

    Cheers,
    _

Similar Threads

  1. Multithreading
    By havij000 in forum Newbie
    Replies: 22
    Last Post: 21st August 2013, 13:35
  2. When to use Multithreading?
    By "BumbleBee" in forum General Programming
    Replies: 5
    Last Post: 30th April 2011, 18:21
  3. regarding multithreading
    By mohanakrishnan in forum Qt Programming
    Replies: 19
    Last Post: 9th December 2009, 08:21
  4. multithreading
    By mickey in forum General Programming
    Replies: 2
    Last Post: 5th June 2008, 22:01
  5. MultiThreading in Qt
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 7th November 2006, 19:26

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