1

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?

2
  • The header system is very primitive - #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.
    – molbdnilo
    Commented Apr 8, 2015 at 15:05
  • So I have two .cpp files. "main.cpp" and "window.cpp", and a "window.h" file, where "window" is a class. I create a window object in main(). I want to use the same libraries across all these three files. Do I have to #include the libraries across all three, or just the two .cpp files, just main.cpp, or is there some other rule to help me keep track of all this?
    – JoseOrtiz3
    Commented Apr 8, 2015 at 15:34

2 Answers 2

4

How does the file that has void MainWindow::on_radioButton_pressed() know what testfunc() is if you don't include it? Some way you need to include testfunc() in that file. One way is to move testfunc() into its own header and included it where its needed.

2
  • I thought that #include is basically a "copy and paste", which is why I figured testfunc() was in the scope of mainwindow.h and its associated mainwindow.cpp. No? I have to look more closely at my code, will return to edit my question. Thank you.
    – JoseOrtiz3
    Commented Apr 8, 2015 at 15:06
  • You were correct, but I couldn't include the header because the header used dynamic linking and for some reason couldn't be included without errors. I found one that did not use it. Go figure?
    – JoseOrtiz3
    Commented Apr 9, 2015 at 18:28
0

You need to know the difference between define and declare a function. Read this first:

http://www.cprogramming.com/declare_vs_define.html

so you need to declare the function in a header file, call it myglobalvars.h. Include myglobalvars.h ,with the declaration in the main.cpp and in the window.h. and define it in the main.cpp.

At compile time the main has the declaration and the definition of the function, the window has only the declaration, but at link time the implementation of the function is found in the main, so the window can call it from there!

Have fun!

Not the answer you're looking for? Browse other questions tagged or ask your own question.