Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. was not declared in this scope error

was not declared in this scope error

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    svncple
    wrote on last edited by
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by
      #2

      You have a typo:

      //create an instance of MyThread
      mTheard = new MyThread;
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        svncple
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved