1

I need to know what am I doing wrong.

I tried researching about it but I can't really find anything that it's related to my case. I am new to QT and debugging signal and slots is kinda technical for me.

What I wanted to do is just simple: make a thread that will continuously send signal to my QProgressBar widget.

Here's my essential code snippets:

thread.h

class MyThread : public QThread
{

public:
    MyThread(QWidget * parent = 0);


signals:
    void valueChanged(int value);

protected:
    void run();
};

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MyThread * test = new MyThread(this);
    connect(test,SIGNAL(valueChanged(int)),ui->progressBar,SLOT(setValue(int)));
    test->start();
}

thread.cpp

MyThread::MyThread(QWidget * parent)
{

}




void MyThread::run(){

emit valueChanged(10);  //for simplicity

}


void MyThread::valueChanged(int value){

}

I only have a single progressBar on my UI and my main is the same as the default.

Anyway, upon running of the code. I kept on getting this no such signal from my thread class. May I know what am I doing wrong?. I would also like to clarify if my understanding is right regarding signals and slots in my own words: it means that the slot will be triggered everytime the signal is called.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Carlos Miguel Colanta
  • 2,685
  • 3
  • 31
  • 49

2 Answers2

7

I believe the error message is due to a missing Q_OBJECT macro at the top of your MyThread declaration. The documentation at http://doc.qt.io/qt-5/signalsandslots.html explains this is necessary for any class that wants to declare signals and slots.

Change your class definition to:

class MyThread : public QThread
{
    Q_OBJECT

    public:
        MyThread(QWidget * parent = 0);

    signals:
        void valueChanged(int value);

    protected:
        void run();
};

Take a look at the linked documentation, specifically the A Small Example section, for a complete explanation why this is needed.

trooper
  • 4,444
  • 5
  • 32
  • 32
3

You must not implement a signal in a .cpp file. MOC will do that and there must only be one implementation.

Just delete this part:

void MyThread::valueChanged(int value){

}

If your code works, that might be luck because the linker throws away the right implementation. You should not rely on that.

Simon Warta
  • 10,850
  • 5
  • 40
  • 78