Results 1 to 3 of 3

Thread: Emit signal from QThread to main thread!!!

  1. #1
    Join Date
    Jan 2013
    Posts
    22
    Thanked 3 Times in 2 Posts
    Qt products
    Qt/Embedded

    Default Emit signal from QThread to main thread!!!

    Hi
    I have tried to create thread.
    my doubts are as below:
    1.the way it is created is correct or not?
    2.After adding signal Getting error in .h file Signal_FromQThread(QString) is protected and in .cpp giving error "within context" where signal is emitted.
    3.the purpose of creating QThread is to do calculations in this thread and after passing control to main thread slot update() is called for paint.so that time it is taking now can be reduced.

    pls refer below code

    pls suggest on it


    Thanks,
    sanujas


    //in thread.h

    #include <qthread.h>


    class Cthread : public QWidget, private Ui::CthreadBase
    {
    Q_OBJECT


    //Qthread newly added
    class CRefresher : public QThread
    {
    public:
    static Cthread *m_fun;

    signals:
    void Signal_FromQThread(QString);

    protected:
    void run()
    {
    m_fun->updateThefun();
    }
    };
    friend class CRefresher;

    private slots:
    void updatefun();
    private:
    void updateThefun();
    }


    //in thread.cpp


    //in constructor

    Cthread* Cthread::CRefresher::m_scopeGraph = NULL;
    Cthread::Cthread( QWidget *l_Parent ) : QWidget( l_Parent )
    {
    updateThefun();
    Cthread::CRefresher::m_fun = this;

    m_TimerThread = new QTimer( this );
    connect( m_TimerThread, SIGNAL( timeout() ), this,
    SLOT( updatefun()) );
    connect(Cthread::CRefresher::m_fun, SIGNAL(Signal_FromQThread(QString)), this, SLOT(update()), Qt::QueuedConnection);

    m_TimerThread->start( 1000 );
    }

    //newly added function
    void Cthread::updatefun()
    {
    Cthread::CRefresher l_refersher;

    l_refersher.start();
    l_refersher.setPriority(QThread::LowestPriority);
    l_refersher.wait();
    emit l_refersher.Signal_FromQThread("emitting thread signal");
    }

    //in function
    void Cthread::updateThefun()
    {
    //some calculations
    //....
    //....

    update();
    }

    void Cthread:aintEvent( QPaintEvent *f_ptrEvent)
    {
    //draw
    //....
    //....
    }

  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: Emit signal from QThread to main thread!!!

    You cannot access widgets from within threads.
    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
    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: Emit signal from QThread to main thread!!!

    The code snippet is seriously broken.

    There is a widget subclass declaration that is unfinished and weirdly called Cthread, there is a QThread subclass declaration with a signals section but no Q_OBJECT macro, the thread's calls into the widget instead of doing the calculation as the introduction text claims it would, and so on.

    I suggest you repost the code, using code tags and separate section for the two classes.
    The code tags are like the quote tags, just with the keyword code instead of quote.

    Cheers,
    _

Similar Threads

  1. QThread class does emit signal
    By saman_artorious in forum Qt Programming
    Replies: 2
    Last Post: 14th June 2012, 15:10
  2. Emit signal outside run() in QThread
    By naturalpsychic in forum Qt Programming
    Replies: 4
    Last Post: 26th March 2012, 16:31
  3. Replies: 5
    Last Post: 22nd February 2011, 21:21
  4. QThread sends signal to main thread immediatly
    By BIllNo123 in forum Newbie
    Replies: 7
    Last Post: 27th August 2010, 10:32
  5. QThread Signal Not Received By Main Thread Slot
    By EF2008 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2010, 08:06

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.