QThread and Error Access violation



  • 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.


  • Lifetime Qt Champion

    Hi,

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



  • [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();
    ^
    @


  • Lifetime Qt Champion

    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 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...


  • Lifetime Qt Champion

    @
    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 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')
    @



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



  • [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

    :(


  • Lifetime Qt Champion

    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 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.


  • Lifetime Qt Champion

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



  • [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 :)


Log in to reply
 

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