简体   繁体   English

传递 Function 指针 - 我做错了什么?

[英]Passing Function Pointer - What am I doing Wrong?

I have the following function to link a button on a ui file with a function within the class.我有以下 function 将 ui 文件上的按钮与 class 中的 function 链接。

void Window::connectButton() {
    connect(ui->pushButton, SIGNAL(released()), this, SLOT(clear()));
}

What I actually want to achieve is to link the button with a function in a derived class.我真正想要实现的是将按钮与派生的 class 中的 function 链接。 I can't reuse the connect() function above because I cannot access ui->pushButton from the derived class.我无法重用上面的connect() function,因为我无法从派生的 class 访问ui->pushButton pushButton。 So what I ended up with was this:所以我最终得到的是:

void Window::connectButton(void (*func)(void)) {
    connect(ui->pushButton, SIGNAL(released()), this, SLOT(func()));
}

In case it's useful, this function is implemented in the derived class as:如果有用,这个 function 在派生的 class 中实现为:

void Transmit::setWindow() {
    windowTitle();
    setWindowSize();

    connectButton(clear);
    //clear is a redefined function for the derived class
    //inherited from the Window class
}

I keep on getting the issue saying that func is not used in the connectButton(void (*)()) function and that func2 cannot be passed into connectButton(void (*)()) .我不断收到问题说func没有在connectButton(void (*)()) function 中使用,并且func2不能传递到connectButton(void (*)())

This is my first experiment with function pointers so can anyone point me in the direction of my mistakes or, if more viable, a better way of implementing the code.这是我对 function 指针的第一次实验,所以任何人都可以指出我的错误方向,或者,如果更可行,更好的实现代码的方法。

Thanks in advance提前致谢

To connect a signal with a function, you need to use the "New" Signal Slot Syntax要将信号与 function 连接,您需要使用“新”信号插槽语法

void Window::connectButton(void (*func)(void)) {
    connect(ui->pushButton, &QPushButton::released, func);
}

If you want to connect to member functions, then you can use lambdas.如果你想连接到成员函数,那么你可以使用 lambdas。 The easiest way to support that is by using std::function支持它的最简单方法是使用std::function

void Window::connectButton(std::function<void()> func) {
    connect(ui->pushButton, &QPushButton::released, func);
}

I have no idea what func2 is, but the more immediate problem is that Qt's SLOT() macro is not actually working with function pointers at all, but it's really taking func() as a char constant.我不知道func2是什么,但更直接的问题是 Qt 的SLOT()宏实际上根本无法使用 function 指针,但它确实将func()作为 char 常量。

Easiest way out will be to move the SLOT() -macro to the calling function, ie:最简单的方法是将SLOT() -宏移动到调用 function,即:

void Window::connectButton(const char* method) {
    connect(ui->pushButton, SIGNAL(released()), this, method);
}

void Transmit::setWindow() {
    connectButton(SLOT(clear()));
}

The above is assuming that this in Window::connectButton() actually refers to the object that you want to call clear() on.以上假设Window::connectButton()中的this实际上是指您要调用clear()的 object。 Otherwise, you'd use:否则,您将使用:

void Window::connectButton(QObject* receiver, const char* method) {
    connect(ui->pushButton, SIGNAL(released()), receiver, method);
}

void Transmit::setWindow() {
    connectButton(myobject, SLOT(clear()));
}

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

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