Results 1 to 9 of 9

Thread: emit qt signal is very slow how it can be optimized?

  1. #1
    Join Date
    Aug 2008
    Posts
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default emit qt signal is very slow how it can be optimized?

    I have a such peace of code.

    thread that reeds the file

    Qt Code:
    1. #include <QThread>
    2. #include <QString>
    3. class openner : public QThread
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. openner(QString);
    9. ~openner();
    10. void run();
    11. private:
    12. QString file;
    13. signals:
    14. void onStart(int);
    15. void on(int);
    16. void onEnd(int);
    17. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "openner.h"
    2. #include <QFile>
    3. #include <QTextStream>
    4. openner::openner(QString f)
    5. : QThread(0)
    6. {
    7. file=f;
    8. }
    9. void openner::run()
    10. {
    11. QString str;
    12. QFile f(file);
    13. f.open(QIODevice::ReadOnly | QIODevice::Text);
    14. QTextStream in(&f);
    15. emit onStart(f.size());
    16. while(!in.atEnd())
    17. {
    18. str=in.readLine();
    19. emit on(in.pos());
    20. }
    21. emit onEnd(f.size());
    22. f.close();
    23. }
    24. openner::~openner()
    25. {
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    main form

    Qt Code:
    1. #include <QtGui/QMainWindow>
    2. #include <QProgressBar>
    3. #include "openner.h"
    4. class progressTest : public QMainWindow
    5. {
    6. Q_OBJECT
    7. private:
    8. openner *o;
    9. public:
    10. progressTest(QWidget *parent = 0, Qt::WFlags flags = 0);
    11. ~progressTest();
    12. protected slots:
    13. void onStart(int);
    14. void onProgres(int);
    15. void onEnd(int);
    16. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "progresstest.h"
    2.  
    3. progressTest::progressTest(QWidget *parent, Qt::WFlags flags)
    4. : QMainWindow(parent, flags)
    5. {
    6. pb=new QProgressBar(this);
    7. pb->resize(200,20);
    8. o=new openner("C:\\projects\\umc\\UMC_invoice_.28532.00.00.100000_id_0793918_part_1.csv");
    9. QObject::connect(o,SIGNAL(onStart(int)),this,SLOT(onStart(int)));
    10. QObject::connect(o,SIGNAL(on(int)),this,SLOT(onProgres(int)));
    11. QObject::connect(o,SIGNAL(onEnd(int)),this,SLOT(onEnd(int)));
    12. o->start();
    13. }
    14. void progressTest::onStart(int m)
    15. {
    16. pb->setMaximum(m);
    17.  
    18. }
    19. void progressTest::onProgres(int l)
    20. {
    21. pb->setValue(l);
    22. }
    23. void progressTest::onEnd(int l)
    24. {
    25. pb->setValue(0);
    26. }
    27. progressTest::~progressTest()
    28. {
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 


    When I use my program with emit on(in.pos()); then it takes for about three hours to read file. But then I use it without emit on(in.pos()); file is read in the minutes. the size of file is 3000 kb
    How the emit of signal can be optimised?
    OS - windows XP
    IDE - Microsoft Visual Studio 2008?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: emit qt signal is very slow how it can be optimized?

    What happens if u use Qt:irectConnection as last argument with connect ?

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: emit qt signal is very slow how it can be optimized?

    it's bad idea to use Qt:: DirectConnection between threads, Qt::QueuedConnection must be used.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Aug 2008
    Posts
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: emit qt signal is very slow how it can be optimized?

    I tried Qt::QueuedConnection and nothing changed.

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: emit qt signal is very slow how it can be optimized?

    yes, because by default last parameter in 'connect' is Qt::AutoConnection, which means
    If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt:: DirectConnection; otherwise the signal is queued, as with Qt::QueuedConnection.
    try to use customs events, but I think result will not change.
    ps. if you use thread just for loading information and don't transform it then you can you only main thread and use qApp->processEvents() in long time operations.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: emit qt signal is very slow how it can be optimized?

    a) Qt::QueuedConnection is the default for threads. So explicitly specifying it won't change anything.

    b) simple solutions might be
    1. emit the signal not every time but only every 100th time (or so)
    2. just open the file in a normal function call, update the gui and call QCoreApplication::processEvents() now and then
    3. do not use a thread
    4. use a plain callback (do not forget to synchronize access! and don't forget you must not call gui code from another thread!)


    HTH

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: emit qt signal is very slow how it can be optimized?

    What happen when You don't emit signal but You call in.pos() ?

  8. #8
    Join Date
    Oct 2006
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: emit qt signal is very slow how it can be optimized?

    Qt Code:
    1. #include "openner.h"
    2. #include <QFile>
    3. #include <QTextStream>
    4. openner::openner(QString f)
    5. : QThread(0)
    6. {
    7. file=f;
    8. }
    9. void openner::run()
    10. {
    11. QString str;
    12. QFile f(file);
    13. f.open(QIODevice::ReadOnly | QIODevice::Text);
    14. QTextStream in(&f);
    15. emit onStart(f.size());
    16. while(!in.atEnd())
    17. {
    18. str=in.readLine();
    19. emit on(in.pos());
    20.  
    21. QApplication::processEvents();
    22.  
    23. }
    24. emit onEnd(f.size());
    25. f.close();
    26. }
    27. openner::~openner()
    28. {
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

    Try the processEvents() function. I think that u emit too much signals, so the GUI its not able to process them during an intensive while loop.

  9. #9
    Join Date
    Oct 2006
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: emit qt signal is very slow how it can be optimized?

    OPPS! Sorry for the mistake

    use QCoreApplication:rocessEvents();

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 21:36
  2. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16
  3. Connection of custon signals/slots
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 07:04
  4. how to know which button emit the signal?
    By coder1985 in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2008, 14:26
  5. emit is slow in Thread?
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2007, 08:52

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.