I have a qt widget GUI with a button that signals a slot in the QMainWindow. This slot calls a function that is defined in the header of main(), but seems to be undefined once its called from the slot.
Roughly, the problem like this:
#include <required libraries like stdio.h>
#include "window.h"
void testfunc() {printf("I really want to print this");}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
My MainWindow object's constructor looks basically like
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_radioButton_pressed()
{
testfunc(); //Error: Not in this scope.
}
I'm not sure why the problem occurs. I have tried unsuccessfully changing the order of the #includes in main() to
#include <stdio.h>
void testfunc() {printf("I really want to print this");}
#include "window.h"
Perhaps the problem occurs because the QApplication 'a' that is called by 'a.exec()' that handles QMainWindow's slot does not have testfunc() in its scope?
Another odd thing is some libraries that I have #included already in main() must be re-included in mainwindow.h to be referenced.
There is also some "Dynamic linking" occuring in one of the libraries in main, i'm not sure if this is causing problems.
What is going on?
How do I extend the scope of the function to the slot?
edit: Ok, I have a "library.h" I #include and use in "main.cpp". This library has classes and function definitions that are used the the definition a class in "window.h" and its member functions in "window.cpp", and an object of this class is constructed in main().
Do I have to #include "library.h"
on the top of window.cpp and/or window.h?
#include "file"
only means "insert the contents of the file 'file' right here". It doesn't affect any files except the one where it occurs.