[solved]Call class function in another cpp file
-
OK, according to what I understood from your design,
MainWindow
spawns aCustomCode
instance when requested. Therefore when constructing theCustomCode
instance you know whichMainWindow
instance you have to connect to. If I were you I'd use the callingMainWindow
instance as parent of theCustomCode
instance. This would allow to connect thesend_test()
signal to thetest()
slot of yourMainWindow
instance. -
How could I do that?
If I do this in mainwindow.cpp:
codewindow = new CustomCode; codewindow->setParent(this); connect(codewindow,SIGNAL(send_test()),SLOT(test())); codewindow->show();
This makes it compile but instead of a seperate window the current mainwindow gets overwritten with the codewindow and the send_test() is in jsfunctions.h, not in CustomCode.h. Would I have to make a function that calls another function that calls the signal? Or am I missing it here? Sorry
Edit:
Succes!
I used : (in mainwindow.cpp)
void MainWindow::on_actionAdd_custom_logic_triggered() { codewindow = new CustomCode; QObject::connect(&jsfunction,SIGNAL(send_test()),this,SLOT(test())); codewindow->show(); }
added
extern jsfunctions jsfunction;
to mainwindow.h and customcode.h
addedjsfunctions jsfunction;
in jsfunctions.cppand changed jsfunctions.h to
signals: void send_test(); public slots: void call_test() { emit send_test(); }
Now it works! test() gets properly called and my UI actually changes. Many thanks! :)
PS: How do I change the topic to solved?
-
I don't think it is necessary to have a static instance of
jsfunctions
to do this. Why did you add it? As far as I understand, thejsfunctions
instance was meant to be used in theCustomCode
instance? Assuming my hypothesis is correct, I would do theconnect
in theCustomCode
, where both theMainWindow
andjsfunctions
instance are used:void MainWindow::on_actionAdd_custom_logic_triggered() { codewindow = new CustomCode( this ); codewindow->show(); }
CustomCode::CustomCode( QObject* parent ) : QMainWindow(parent) , ui(new Ui::CustomCode) , jsfunction( new jsfunctions() ) { connect( jsfunction, SIGNAL( send_test() ), parent, SLOT( test() ) ); } @TheHawk said: > PS: How do I change the topic to solved? You can simply edit the topic title (IIRC by editing your first post),
-
I have no particular reason why I chose it to be static, I did it because after googling some errors it was suggested to use static.
Anyhow, your code indeed works as well and your assumption was correct! :)
I do have a little issue now. Callingcodewindow = new CustomCode( this );
makes the new window always on top. I previously calledcodewindow = new CustomCode;
, this made the 2 windows independant (if you clicked the one in the back it came to front). I don't really see an option how to fix this behavior besides using signals and slots at a click to bring a window up and lower the other one. Am I missing the obvious here?thanks!
-
I think you can tune this behaviour with the correct window flag, which is the second argument of the
QMainWindow
constructor.Edit:
As another way to do it, you can pass a null pointer to theQMainWindow
constructor in theCustomCode
constructor, but still use the parent pointer in theconnect
statement. -
I tried all the window flag settings without succes, it always stays on top.
How would I pass a null pointer to the
QMainWindow
constructor in theCustomCode
constructor? Can't really get my head around it. Changing the call to:
codewindow = new CustomCode(0);
orcodewindow = new CustomCode;
creates the desired effect but makes me unable to call the function (since no parent exists I think?).Am I even calling the windowflag correctly?
void MainWindow::on_actionAdd_custom_logic_triggered() { CustomCode *codewindow; codewindow = new CustomCode(0); //QObject::connect(&jsfunction,SIGNAL(send_test()),this,SLOT(test())); codewindow->setWindowFlags(Qt::Window); codewindow->show(); }
-
I got a question though (kinda off-topic). Would it be possible to create a signal from the
MainWindow
to a slot (or function) inside the javascript script that I evaluate insideCustomCode
? So for example:I want to plot a graph using the javascript script. I want that graph to use actual, changing values from
mainwindow
. So I would like to create a signal that triggers upon a value change and then call a function inside the javascript script to update the graph with the new values. -
@TheHawk said:
I got a question though (kinda off-topic). Would it be possible to create a signal from the
MainWindow
to a slot (or function) inside the javascript script that I evaluate insideCustomCode
? So for example:Better ask a new question in a new thread, for the future, it helps when looking for a problem on the forum.
You are free to define any signal / slot you want, so the short answer is yes.
You can emit a signal inMainWindow
when the data needs to be reprocessed by the js script managed byCustomCode
.