SLOT problem / C++

SLOT problem / C++

本文关键字:C++ problem SLOT      更新时间:2023-10-16

我正试图找出这个错误。我用Qt Creator做了一个简单的应用程序。

我有三个按钮,其中两个没有启用。然后,当按下第一个按钮时,我想让它们可见,但当我按下按钮时,出现了窗口错误:"程序停止工作"。该程序编译并执行其他所有操作。

QPushButton *dealButton = new QPushButton(tr("Deal cards"));
dealButton->show();
QPushButton *hitButton = new QPushButton(tr("HIT"));
hitButton->show();
hitButton->setEnabled(false);
QPushButton *standButton = new QPushButton(tr("STAND"));
standButton->show();
standButton->setEnabled(false);
...
connect(dealButton, SIGNAL(clicked()), this, SLOT(dealCards()));
...
void MainWindow::dealCards()
{
hitButton->setEnabled(true);
standButton->setEnabled(true);
}

这就是密码。

问题是,您在构造函数中重新声明dealButton和其他函数(或者显示new调用的任何函数)。

你应该在你的类中定义:

private: // probably
  QPushButton *dealButton;

在您的构造函数或gui初始化代码中:

dealButton = new QPushButton(...); // note: not QPushButton *dealButton = ...

您现在所拥有的是创建一个名为dealButton的新变量,该变量是该作用域(函数)的本地变量。该变量隐藏(屏蔽)了类的成员。