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. QThread and Error Access violation
Qt 6.11 is out! See what's new in the release blog

QThread and Error Access violation

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 7.0k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello everybody :)

    I have a problem with QThread when i want change my form objects status during thread processing.

    Now see these are my codes...

    this is my mainwindows.h

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QObject>
    #include <QThread>

    class MyThread : public QThread
    {
    Q_OBJECT

    public:
    MyThread(QObject *parent = 0);
    void run();

    signals:
    void MyThreadSignal();

    };

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    public:
    void _send(QString _title, QString _message);

    public slots:
    void MyThreadSlot();

    private:
    Ui::MainWindow *ui;
    myThread *onethread;
    };

    #endif // MAINWINDOW_H

    @

    and this is mainwindows.cpp

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QThread>
    #include <QDebug>

    /* ****** Thread part ****** */
    myThread::myThread(QObject *parent)
    : QThread(parent){}

    void MyThread::run()
    {
    //Run my function
    _send();
    }

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

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_BUTTON_SEND_clicked()
    {

    ui->tabWidget->setDisabled(true); // Disable my tabWidget
    
    onethread = new MyThread(this);
    connect(onethread, SIGNAL(MyThreadSignal()), this, SLOT(_send()));
    onethread->start(QThread::NormalPriority);
    

    }

    void MainWindow::_send()

    {

    ui->tabWidget->setDisabled(false); // Enable my tabWidget // Error in here
    

    }

    @

    Why when I run thread process I can't set enable status for tabWidget ? without thread anything is good work but by thread no ! why ? can anyone help me for solve this problem? :)

    Thanks you.

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

      Hi,

      Are you trying to call MainWindow::_send directly from MyThread::run ?

      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
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        [quote author="SGaist" date="1407411703"]Hi,

        Are you trying to call MainWindow::_send directly from MyThread::run ?[/quote]

        hi SGaist how are you ?
        Thanks for your reply..

        By MainWindow::_send(); i get this error :

        @

        error: cannot call member function 'void MainWindow::_send()' without object
        MainWindow::_send();
        ^
        @

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

          You can't call a function from another class.

          Also, you have a signature mismatch between your header and implementation.

          What exactly are you trying to achieve ?

          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
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            [quote author="SGaist" date="1407412316"]You can't call a function from another class.

            Also, you have a signature mismatch between your header and implementation.

            What exactly are you trying to achieve ?
            [/quote]

            Where is my mismatch can u show me?
            I want to change my tabWidget status from disable to enable after Thread processing...

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

              @
              namespace Ui {
              class MainWindow;
              }

              class MainWindow : public QMainWindow
              {
              Q_OBJECT

              public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
              public:
              void _send(QString _title, QString _message); << here
              }; @

              @
              void MyThread::run()
              {
              //Run my function
              _send(); << there
              }
              @

              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
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                [quote author="SGaist" date="1407442699"]@
                namespace Ui {
                class MainWindow;
                }

                class MainWindow : public QMainWindow
                {
                Q_OBJECT

                public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
                public:
                void _send(QString _title, QString _message); << here
                }; @

                @
                void MyThread::run()
                {
                //Run my function
                _send(); << there
                }
                @

                [/quote]

                Hi again...

                I Chaged to

                @
                public:
                void _send();
                @

                and

                @
                void MainWindow::_send()

                {

                ui->tabWidget->setDisabled(false);

                };
                @

                and

                @
                void MyThread::run()
                {
                //Run my function
                //MainWindow::_send();

                MainWindow *II;
                    II->_send();
                

                }
                @

                but not working !
                Error :

                @
                QObject::connect: No such slot MainWindow::_send() in ..\untitled15\mainwindow.cpp:40
                QObject::connect: (receiver name: 'MainWindow')
                @

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  msue
                  wrote on last edited by
                  #8

                  try to change "public" into "public slots" before your send function.

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    [quote author="msue" date="1407481533"]try to change "public" into "public slots" before your send function.[/quote]

                    Unfortunately ...
                    The program has unexpectedly finished.
                    ..\untitled15.exe crashed

                    :(

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

                      That's normal, MainWindow *ll is uninitialized.

                      Anyway, you should not access MainWindow from your thread, just emit the signal you connected earlier.

                      Also it's seems you are new to C++, I'd recommend getting a good book about 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
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        [quote author="SGaist" date="1407483129"]That's normal, MainWindow *ll is uninitialized.

                        Anyway, you should not access MainWindow from your thread, just emit the signal you connected earlier.

                        Also it's seems you are new to C++, I'd recommend getting a good book about it[/quote]

                        Can you show me a good document or book about this question ?!
                        I mean how to update GUI in Thread process.

                        Thank you.

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

                          Qt's own documentation and examples e.g. the Mandelbrot example

                          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
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #13

                            [quote author="SGaist" date="1407484062"]Qt's own documentation and examples e.g. the Mandelbrot example[/quote]

                            Thank you for your helping... i try to read it :)

                            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