[英]Unable to add slot and connect it to a button
I gathered a code of an application called calendar from the base of examples of the Qt Framework. 我从Qt Framework的示例中收集了一个名为calendar的应用程序代码。 I am trying to learn from it and add there some functionality.
我正在尝试从中学习并添加一些功能。 The problem right now that I've got is that I want to implement two function to the two button that I created ( one for increase counting of the days and the second for decrease ).
我现在遇到的问题是,我想对我创建的两个按钮实现两个功能(一个用于增加天数,第二个用于减少天数)。
The code that I added to the function for increasing the days is: 我添加到函数中以增加日期的代码是:
void MainWindow::forward(int *click_forward)
{
click_forward++;
}
and the code added to the function for decreasing the days: 以及添加到函数中以减少使用天数的代码:
void MainWindow::backwards(int *click_backwards)
{
click_backwards--;
}
In the constructor I defined a variable named click which of the int 在构造函数中,我定义了一个名为click的变量。
type, and I sent this variable to the both function by reference: 类型,然后通过引用将此变量发送给both函数:
forward(&click);
backward(&click);
In the header file, in the public slosts area these both functions are 在头文件的公共目录区域中,这两个函数都是
defined as: 定义为:
void forward(int *click_forward);
void backwards(int *click_backwards);
I also implemented two SIGNAL-SLOT connections: 我还实现了两个SIGNAL-SLOT连接:
QObject::connect(nextbtn, SIGNAL(clicked()), this, SLOT(forward(int
&click)));
QObject::connect(beforebtn, SIGNAL(clicked()), this,
SLOT(backwards(int &clickt)));
But for some reasons when I compile the project I receive an information that: 但是由于某些原因,在编译项目时会收到以下信息:
QObject::connect: No such slot MainWindow::forward(int &click)
QObject::connect: No such slot MainWindow::backwards(int &clickt)
I wanted to use pointers in these two functions, just to work on the original variable itself not on the copy. 我想在这两个函数中使用指针,只是为了处理原始变量本身而不是副本。 Could I please ask you to point me out what I am doing wrong.
我能否请您指出我做错了什么。
Thank you, 谢谢,
The problem is that your signal and slot(s) have different signatures: signal has no arguments, but slot has an argument of pointer type. 问题是您的信号和插槽具有不同的签名:signal没有参数,但是slot具有指针类型的参数。 Besides, even if your signals connections would work, the execution of such code wouldn't do anything useful (at least) as you modify the temporary defined variables
click_backwards
etc. 此外,即使您的信号连接有效,执行此类代码也
click_backwards
(至少),因为您修改了临时定义的变量click_backwards
等。
I would solve this in the following way: 我将通过以下方式解决此问题:
Define the class member variables and slots: 定义类成员变量和插槽:
class MainWindow
{
[..]
private slots:
void forward();
void backwards();
private:
int click_forward;
int click_backwards;
}
Define slots: 定义广告位:
void MainWindow::forward()
{
click_forward++;
}
void MainWindow::backwards()
{
click_backwards--;
}
And finally establish connections: 最后建立连接:
QObject::connect(nextbtn, SIGNAL(clicked()), this, SLOT(forward()));
QObject::connect(beforebtn, SIGNAL(clicked()), this, SLOT(backwards()));
如果您这样处理信号和插槽,则会收到编译器错误而不是运行时错误,我个人认为这非常有帮助,因为它只会告诉您由于信号/插槽不兼容而导致它们无法连接
QObject::connect(nextbtn, &QPushButton::clicked, this, &MainWindow::forward);
By the way, you're not increasing the value of the integer, you're increasing the pointer. 顺便说一句,您没有增加整数的值,而是增加了指针。
That's a bug waiting to happen. 那是一个等待发生的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.