【发布时间】: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