Qt:某些插槽在发布模式下不会执行

发布于 2024-12-08 23:22:05 字数 2222 浏览 0 评论 0原文

我正在 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);
}

请原谅我的英语,并提前感谢您。

I am doing some simple program in Qt (MSVC++2008) with few checkboxes and buttons. In debug mode, everything works fine, but I can't distribute such executable, because most people don't have Visual Studio installed. When I do compile it in release mode, only 2 pushbuttons work.

I have designed my window using Qt Creator's 'drawing tool' (Qt Designer, I guess).
I do have such slots defined in my header file:

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 *);

My implentation of those signals looks like this:

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);
}

Pardon for my English and thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿挽旧人 2024-12-15 23:22:06

你的代码看起来不错。

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

Your code looks good.

Try running "make clean" or "qmake". Your "ui_" or "moc_" files might need updating.

澉约 2024-12-15 23:22:06

您确定插槽没有被调用 - 或者只是没有按照您的预期进行?

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

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

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

Are you sure the slots aren't being called - or simply not doing what you expect?

The most common reason for 'it works in debug but not release' is unitialised variables. In debug mode the variables are generally set to some null/zero value but not in release mode.

Another common problem are debug macros with side effects. Have you (or some lib) rewritten the connect() call as a macro for debugging and it's doing something different in release?

Time for some printf() debugging ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文