Qt C++;自定义插槽不工作

Qt C++;自定义插槽不工作,qt,connect,slot,Qt,Connect,Slot,我正在尝试将按钮与名为simu的外部类中定义的插槽连接。该插槽是一个名为startSimu()的函数。对象simulation在我希望连接按钮的同一个类中实例化。代码如下: QPushButton *btn1 = new QPushButton("start simulation"); simu simulation; QObject::connect(btn1, SIGNAL(clicked()), &simulation, SLOT(startSimu())); 代码编译并运行,但

我正在尝试将按钮与名为
simu
的外部类中定义的插槽连接。该插槽是一个名为
startSimu()
的函数。对象
simulation
在我希望连接按钮的同一个类中实例化。代码如下:

QPushButton *btn1 = new QPushButton("start simulation");
simu simulation;
QObject::connect(btn1, SIGNAL(clicked()), &simulation, SLOT(startSimu()));
代码编译并运行,但当我点击按钮时,什么也没发生。函数
startSimu()
如下所示:

void simu::startSimu() {
    std::cout << "aaaa" << std::endl;
}
simu *simulation = new simu; // Will be destroyed soon
QObject::connect(btn1, SIGNAL(clicked()), simulation, SLOT(startSimu()));
我希望有人有线索或提示!
谢谢

在调用插槽之前,您的
模拟对象看起来已被销毁,因为您在堆栈中分配了它:

simu simulation; // Will be destroyed as the execution leave the scope.
QObject::connect(btn1, SIGNAL(clicked()), &simulation, SLOT(startSimu()));
要解决此问题,您可能需要执行以下操作:

void simu::startSimu() {
    std::cout << "aaaa" << std::endl;
}
simu *simulation = new simu; // Will be destroyed soon
QObject::connect(btn1, SIGNAL(clicked()), simulation, SLOT(startSimu()));

请不要忘记在需要时删除
模拟
对象。

在调用插槽之前,您的
模拟
对象似乎已被销毁,因为您在堆栈中分配了它:

simu simulation; // Will be destroyed as the execution leave the scope.
QObject::connect(btn1, SIGNAL(clicked()), &simulation, SLOT(startSimu()));
要解决此问题,您可能需要执行以下操作:

void simu::startSimu() {
    std::cout << "aaaa" << std::endl;
}
simu *simulation = new simu; // Will be destroyed soon
QObject::connect(btn1, SIGNAL(clicked()), simulation, SLOT(startSimu()));

请不要忘记在需要时删除
模拟
对象。

在调用插槽之前,您的
模拟
对象似乎已被销毁,因为您在堆栈中分配了它:

simu simulation; // Will be destroyed as the execution leave the scope.
QObject::connect(btn1, SIGNAL(clicked()), &simulation, SLOT(startSimu()));
要解决此问题,您可能需要执行以下操作:

void simu::startSimu() {
    std::cout << "aaaa" << std::endl;
}
simu *simulation = new simu; // Will be destroyed soon
QObject::connect(btn1, SIGNAL(clicked()), simulation, SLOT(startSimu()));

请不要忘记在需要时删除
模拟
对象。

在调用插槽之前,您的
模拟
对象似乎已被销毁,因为您在堆栈中分配了它:

simu simulation; // Will be destroyed as the execution leave the scope.
QObject::connect(btn1, SIGNAL(clicked()), &simulation, SLOT(startSimu()));
要解决此问题,您可能需要执行以下操作:

void simu::startSimu() {
    std::cout << "aaaa" << std::endl;
}
simu *simulation = new simu; // Will be destroyed soon
QObject::connect(btn1, SIGNAL(clicked()), simulation, SLOT(startSimu()));

请不要忘记在需要时删除模拟对象。

最后要添加的是,不要忘记删除它。此外,我们在某些配置中遇到了cout问题,您可以尝试在开发过程中引发QMessageBox,或者使用qDebug()/qWarning()和自定义消息句柄最后要添加的是,别忘了删除它。另外,我们在某些配置中遇到了cout问题,您可以尝试在开发过程中引发QMessageBox,或者使用带有自定义消息句柄的qDebug()/qWarning()。最后要添加的是,不要忘了删除它。此外,我们在某些配置中遇到了cout问题,您可以尝试在开发过程中引发QMessageBox,或者使用带有自定义消息句柄的qDebug()/qWarning()。最后要添加的是,不要忘记删除它。此外,我们在某些配置中遇到了cout问题,您可以尝试在开发过程中引发QMessageBox,或者使用带有自定义消息句柄的qDebug()/qWarning()