【问题标题】:Qt: some slots don't get executed in release modeQt:某些插槽在发布模式下不会执行
【发布时间】:2011-10-07 14:59:40
【问题描述】:

我正在用几个复选框和按钮在 Qt (MSVC++2008) 中做一些简单的程序。在调试模式下,一切正常,但我无法分发这样的可执行文件,因为大多数人没有安装 Visual Studio。当我在发布模式下编译它时,只有 2 个按钮起作用。

我使用 Qt Creator 的“绘图工具”(我猜是 Qt Designer)设计了我的窗口。 我的头文件中确实定义了这样的插槽:

private slots:
    void on_goButton_clicked(); // Works fine

    void on_InputCheckBox_stateChanged(int arg1); // Don't work
    void on_outputFileCheckBox_stateChanged(int arg1); // Same as above

    void on_inputBrowseButton_clicked();  // Don't work, since theyre disabled
    void on_outputBrowseButton_clicked(); // Same as above

    void replyFinished(QNetworkReply *);

我对这些信号的实现如下所示:

void MainWindow::on_InputCheckBox_stateChanged(int arg1)
{
    if (arg1 == Qt::Checked)
    {
        ui->inputEdit->setEnabled(true);
        ui->inputBrowseButton->setEnabled(true);
     }
    else
    {
        ui->inputEdit->setEnabled(false);
        ui->inputBrowseButton->setEnabled(false);
    }
}

void MainWindow::on_outputFileCheckBox_stateChanged(int arg1)
{
    if (arg1 == Qt::Checked)
    {
        ui->outputEdit->setEnabled(true);
        ui->outputBrowseButton->setEnabled(true);
    }
    else
    {
        ui->outputEdit->setEnabled(false);
        ui->outputBrowseButton->setEnabled(false);
    }
}

void MainWindow::on_inputBrowseButton_clicked()
{
    QString documents = DesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
    QString filename = QFileDialog::getOpenFileName(
            this,
            tr("Select input file"),
            documents,
            tr("Text files (*.txt);;All files (*)"));
    if (filename.size() == 0)
        return;
    else
         ui->inputEdit->setText(filename);
}

void MainWindow::on_outputBrowseButton_clicked()
{
    QString documents = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
    QString filename = QFileDialog::getOpenFileName(
            this,
            tr("Select output file"),
            documents,
            tr("Text files (*.txt);;All files (*)"));
    if (filename.size() == 0)
        return;
    else
        ui->inputEdit->setText(filename);
}

请原谅我的英语,提前谢谢你。

【问题讨论】:

  • 为了帮助调试,如果您在构造函数中显式调用 connect 而不是使用自动信号/插槽功能会发生什么? connect( ui->InputCheckBox, SIGNAL(statechanged(int)), this, SLOT(on_InputCheckBox_stateChanged(int)) ) ?
  • 我得到Object::connect: No such slot MainWindow::on_InputCheckBox_stateChanged(int) Object::connect: (sender name: 'InputCheckBox') Object::connect: (receiver name: 'MainWindow') 作为应用程序输出。附言感谢您解释如何显式连接到 ui 对象。 :-)
  • 听起来您的 moc 文件可能无法正确生成,请尝试重新运行 qmake 并触摸您的 MainWindow 头文件。
  • 这有帮助。非常感谢!

标签: c++ qt signals-slots


【解决方案1】:

您的代码看起来不错。

尝试运行“make clean”或“qmake”。您的“ui_”或“moc_”文件可能需要更新。

【讨论】:

  • 好吧,我已经按照 Chris 的说明解决了我的问题。自从我的问题 tmesamp 以来,我没有能力让它“解决”,直到 8 小时过去。在这种情况下,我会让你的答案正确,因为它解决了问题。谢谢你的回复。无论如何,感谢所有参与这个问题的人,我希望它对其他正在寻找类似错误的人有所帮助。再次感谢您。
【解决方案2】:

您确定插槽没有被调用 - 或者根本没有按照您的预期执行吗?

“它在调试中有效但在发布中无效”的最常见原因是统一变量。在调试模式下,变量通常设置为某个空值/零值,但在发布模式下不会。

另一个常见问题是带有副作用的调试宏。您(或某些库)是否将 connect() 调用重写为用于调试的宏,并且它在发布时做了一些不同的事情?

是时候进行一些 printf() 调试了?

【讨论】:

  • 他们没有被调用。我尝试在这些插槽中添加qDebug() << "Something";,但没有任何反应。这很奇怪,因为 2 个按钮 - 一个用于退出,一个用于“做事”。我只使用 和 simplecrypt link,但我认为它们都没有重写 connect(),对吗?而且我在我的代码中看不到任何未初始化的变量。我正在使用
  • qDebug() 是否在发布模式下输出任何内容?在 Windows IIRC 中,它写入系统调试器。
  • 确实如此。如果我在 MainWindow 构造函数中添加qDebug() << "Something";,它会输出“某事”。我确实从 IDE - Qt Creator 启动我的程序。
  • 今天我有一个类似的问题,原来是一个未初始化的std::atomic_int,在调试模式下不知何故被设置为零,但不是在发布时。
猜你喜欢
  • 1970-01-01
  • 2019-01-26
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
相关资源
最近更新 更多