AAF*_*AFF 4 c++ qt signals-slots qdialog qt-designer
当我运行以下函数时,对话框显示所有内容。问题是按钮无法连接。OK 和 Cancel 不响应鼠标点击。
void MainWindow::initializeBOX(){
QDialog dlg;
QVBoxLayout la(&dlg);
QLineEdit ed;
la.addWidget(&ed);
//QDialogButtonBox bb(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
//btnbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
la.addWidget(buttonBox);
dlg.setLayout(&la);
if(dlg.exec() == QDialog::Accepted)
{
mTabWidget->setTabText(0, ed.text());
}
}
Run Code Online (Sandbox Code Playgroud)
在运行时,cmd 中的错误显示:没有像 accept() 和 reject() 这样的插槽。
您在连接中指定了错误的接收器。它是具有accept()和reject()插槽的对话框,而不是主窗口(即this)。
因此,您只需要:
connect(buttonBox, SIGNAL(accepted()), &dlg, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), &dlg, SLOT(reject()));
Run Code Online (Sandbox Code Playgroud)
现在,当您单击按钮时,对话框将关闭,并exec()返回QDialog::AcceptedOK 或QDialog::RejectedCancel。
| 归档时间: |
|
| 查看次数: |
7353 次 |
| 最近记录: |