简体   繁体   English

在main()中全局声明的函数不在qt小部件插槽的范围内

[英]function declared globally in main() is not in scope of qt widget slot

I have a qt widget GUI with a button that signals a slot in the QMainWindow. 我有一个带有按钮的qt小部件GUI,该按钮表示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. 该插槽调用在main()的标头中定义的函数,但是一旦从插槽调用它,则似乎未定义。

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 我的MainWindow对象的构造函数基本上像

#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 我尝试将main()中的#includes的顺序更改为

#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? 可能是因为处理QMainWindow的插槽的'a.exec()'调用的QApplication'a'在其作用域内没有testfunc()而发生问题?

Another odd thing is some libraries that I have #included already in main() must be re-included in mainwindow.h to be referenced. 另一个奇怪的事情是,我已经在main()中已包含#include的某些库必须重新包含在mainwindow.h中才能被引用。

There is also some "Dynamic linking" occuring in one of the libraries in main, i'm not sure if this is causing problems. main中的其中一个库中也发生了一些“动态链接”,我不确定这是否引起了问题。

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". 编辑:好的,我有一个“ library.h”,我#include并在“ 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(). 该库具有类和函数定义,在“ window.h”中使用了该类的定义,在“ window.cpp”中使用了其成员函数,并且在main()中构造了该类的对象。

Do I have to #include "library.h" on the top of window.cpp and/or window.h? 我是否必须在window.cpp和/或window.h的顶部#include "library.h"

How does the file that has void MainWindow::on_radioButton_pressed() know what testfunc() is if you don't include it? 如果不包含void MainWindow::on_radioButton_pressed()的文件,怎么不知道它是什么testfunc() Some way you need to include testfunc() in that file. 您需要以某种方式在该文件中包括testfunc() One way is to move testfunc() into its own header and included it where its needed. 一种方法是将testfunc()移到其自己的标头中,并将其包含在需要的位置。

You need to know the difference between define and declare a function. 您需要知道定义声明函数之间的区别。 Read this first: 请先阅读以下内容:

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

so you need to declare the function in a header file, call it myglobalvars.h. 因此,您需要在头文件中声明该函数,并将其命名为myglobalvars.h。 Include myglobalvars.h ,with the declaration in the main.cpp and in the window.h. 在main.cpp和window.h中包括myglobalvars.h和声明 and define it in the main.cpp. 并在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! 编译时main具有函数的声明和定义, 窗口仅具有声明,但是在链接时,函数的实现位于main中 ,因此窗口可以从那里调用它!

Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM