[solved]Call class function in another cpp file
-
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
.