was not declared in this scope error



  • hi i am doing some qt examples i found online i am doing one for threading but keep getting the error was not declared in this scop

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QString>
    #include "mythread.h"
    
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
    Q_OBJECT
    
    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    MyThread *mythread;
    
    private:
    Ui::Dialog *ui;
    
    public slots:
    void onValueChanged(int);
    
    private slots:
    // for Start Button
    void on_pushButton_clicked();
    
    // for Stop Button
    void on_pushButton_2_clicked();
    };
    
    #endif // DIALOG_H
    
    
    #ifndef MYTHREAD_H
    #define MYTHREAD_H
    
    #include <QThread>
    
    class MyThread : public QThread
    {
    Q_OBJECT
    public:
    explicit MyThread(QObject *parent = 0, bool b = false);
    void run();
    
    /*if Stop = true the thread will break
     *out of the loop, and will be disposed
     */
    bool Stop;
    
    signals:
    //to communicat with gui thread we need to emit a signal
    void valueChanged(int);
    
    public slots:
    
    } ;
    
    #endif // MYTHREAD_H
    

    next is the file with the error says mThread is not declared

    #include "dialog.h"
    #include "ui_dialog.h"
    

    #include "mythread.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    
    //create an instance of MyThread
    mTheard = new MyThread;
    
    // connect signal/slot
    connect(mThread, SIGNAL(valueChanged(int)), this, SLOT(onValueChanged(int)));
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    /*Abosrb the signal emitted from a run() method
     *and reflect the count change to the count label
     *in our dialog
     */
    
    void Dialog::onValueChanged(int count)
    {
    ui->label->setText(QString::number(count));
    }
    
    // Start Button
    void Dialog::on_pushButton_clicked()
    {
    mThread->Stop = true;
    }
    
    //Stop Button
    void Dialog::on_pushButton_2_clicked()
    {
    mThread->Stop = true;
    } 
    
    
    #include "mythread.h"
    #include <QDebug>
    MyThread::MyThread(QObject *parent) :
    QThread(parent), Stop(b)
    {
    
    }
    
    //run() will be called when a thread starts
    void MyThread::run()
    {
    for(int i = 0; i <= 100; i++)
    {
     QMutex mutex;
     //prevents other threads from changing "Stop" value
      mutex.lock();
    
        //emit the signal for the count label
        emit valueCahnged(i);
    
        // slowdown the count change, msec
        this->msleep(500);
    }
    }
    
    
    #include <QApplication>
    #include "dialog.h"
    
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    
    return a.exec();
    }


  • You have a typo:

    //create an instance of MyThread
    mTheard = new MyThread;
    


  • thanks for catching that dude, i fixed it but am still getting the error.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    In your header you declared mythread not mThread

    On a side note, your mutex won't work like you expect it to. First thing, you create a new mutex each time that function is called and second you never unlock it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.