Discussion Board

Results 1 to 5 of 5
  1. #1
    Hi Guys,

    I have test() function which is executed using QtConcurrent::run method in different thread. My main thread is GUI thread. When any signal is emmited from test() function, main thread does not catch that so i tried to execute slot OnTestSignal() which executes in main thread and emit signal from there. still the signal is not emmited.

    For more detail i have documented code.

    Code:
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QtConcurrentRun>
    #include <QCoreApplication>
    #include <QTimer>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        // Run test in different thread
       QtConcurrent::run((*this),&MainWindow::test);
    
        QObject::connect(this,SIGNAL(testSignal(int)),ui->progressBar,SLOT(setValue(int)),Qt::QueuedConnection);
    
        /**
          * This signal is emmited and progress bar changes
          */
        emit testSignal(qrand()%100);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::test()
    {
        qDebug() << "start :" << QThread::currentThread() ->signalsBlocked()<< " : " << thread()->signalsBlocked();
        qDebug() << "Thread : " << QThread::currentThread() << " : " << thread();
    
        int i;
        int j;
        while(1) {
            ++i;
    
            if(i > 10) {
                i =0;
                QMetaObject::invokeMethod(this,"onTestSignal",Qt::QueuedConnection);
                //emit testSignal(); /* DOES NOT WORK */
    
            }
        }
    
    }
    
    void MainWindow::onTestSignal()
    {
        // Verify that test executed in different thread and this funtion is executed in main thread
    
        qDebug() << "onTestSignal() : " << QThread::currentThread() << " :Main Thread : " << thread();
    
        /**
          * Executed in main thread still signal is not been emmited, WHY ????
          */
        emit testSignal(qrand()%100);
    
        // Did not work
        QCoreApplication::processEvents();
    }
    What am i doing wrong here ? i should be getting a signal but that not happening

    haws anybody faced similar issue?

  2. #2
    Nokia Developer Champion rahulvala's Avatar
    Join Date
    Oct 2008
    Location
    INDIA
    Posts
    2,294
    #include <QtGui/QApplication>

  3. #3
    Quote Originally Posted by rahulvala View Post
    #include <QtGui/QApplication>
    Do you actually have any suggestion on this ?

  4. #4
    Nokia Developer Champion rahulvala's Avatar
    Join Date
    Oct 2008
    Location
    INDIA
    Posts
    2,294
    What error or warning are you getting exactly ?

  5. #5
    Nokia Developer Moderator gnuton's Avatar
    Join Date
    Mar 2009
    Posts
    1,024
    Hi,
    Does it run test()? If yes.. I'm surprised! And I'm surprised you don't get any error message in the console!
    I cannot really understand how QtConcourent could run in a different thread a class method.
    MAYBE QtCuncurrent could accept static const class methods... but for sure it cannot run your a standard yourclass::test() method,
    because the class object is instanced in a different thread.

Similar Threads

  1. Replies: 2
    Last Post: 2011-06-29, 10:14
  2. How to emit signal from javascript code
    By ram4soft in forum Qt
    Replies: 2
    Last Post: 2011-04-17, 14:31
  3. My signal() doesn't work for standalone app
    By olliwang in forum Python
    Replies: 1
    Last Post: 2008-11-13, 14:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved