PDA

View Full Version : Not able to access functions of MainWindow functions from a mainwindow.cpp



venkacw4
2nd December 2016, 05:31
I have a MainWindow class setup using Qt in Visual Studio. In the mainwindow.cpp file I have created a thread using pthread called 'solder_thread' which when created calls another function called 'perform_solder'. Inside this 'perform_solder', I am trying to access functions and variables declared in the MainWindow class, but I get an error message which says 'IntelliSense: identifier "move_stage_solder" is undefined'


mainwindow.cpp:

#include "mainwindow.h"

void * perform_solder(void * arg);

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{

connect(ui->pb_feeder, SIGNAL(clicked()), SLOT(enable_feeder_arduino()));

}

void MainWindow::enable_feeder_arduino(){

pthread_t solder_thread;

if (pthread_create(&solder_thread, NULL, &perform_solder, NULL)){
ui->status_message->setText("Error in creating the serial communication thread");
exit(-1);
}

}

void MainWindow::move_stage_solder(double xpos, double ypos){

/*Do something*/

}


void * perform_solder(void * arg){

move_stage_solder(0,0); /*This is where the error shows up*/

}

Lesiok
2nd December 2016, 06:48
In your code there is no function move_stage_solder. You have only method MainWindow::move_stage_solder. Fundamentals of C ++ unrelated to Qt.

venkacw4
6th December 2016, 20:09
Yeah, but I want to access the move_stage_solder member function of the MainWindow class from inside perform_solder. Can you help me with that?

d_stranz
6th December 2016, 20:52
As was said, this is basic C++. To access a non-static method for any class, you need a pointer or reference to an instance of that class. So you need find a way to get a pointer to your MainWindow instance so that it is accessible from within the scope of perform_solder().

Hint: Look at using QThread instead of pthread. You could keep a pointer to your MainWindow class instance as a member variable of your worker thread class.