将参数从QtMessageBox信号传递到buttonClick信号处理程序槽
Passing parameters from QtMessageBox signals to buttonClick signal Handler slot
我试图创建一个弹出窗口,将(1)是非模态的,(2)承载上下文数据,将在以后处理时,用户单击ok事件。到目前为止,我有下面的代码确实弹出作为一个非模态。我知道msgBox->open(this, SLOT(msgBoxClosed(QAbstractButton *))
和msgBoxClosed(QAbstractButton *button)
工作,但当我将QStringList collisionSections
添加到SLOT参数时。我得到这个错误:
QObject::connect: No such slot MainWindow::msgBoxClosed(QAbstractButton *, collisionSections) in srcmainwindow.cpp:272
QObject::connect: (receiver name: 'MainWindow')
我理解,因为它在那里声明槽,但我不知道如何去做我想做的事情,这是在QString作为内容传递给我的信号,并让它与qmessagebox在OK单击上抛出的buttonClicked()事件很好地发挥作用。我也可能是接近这个错误的方式,请让我知道如果是这样。任何帮助都非常感激!
void MainWindow::do_showCollisionEvt(QStringList collisionSections)
{
QString info = "Resolve sections";
for (QString section : collisionSections)
{
if (!section.isEmpty())
{
info.append(" [" + section + "] ");
qDebug() << "Emitting node off for:" << section;
emit nodeOff(section);
}
}
QMessageBox *msgBox = new QMessageBox;
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setText("Collision event detected!");
msgBox->setInformativeText(info);
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setDefaultButton(QMessageBox::Ok);
msgBox->setModal(false);
msgBox->open(this, SLOT(msgBoxClosed(QAbstractButton *, collisionSections)));
}
void MainWindow::msgBoxClosed(QAbstractButton *button, QStringList collisionSections) {
QMessageBox *msgBox = (QMessageBox *)sender();
QMessageBox::StandardButton btn = msgBox->standardButton(button);
if (btn == QMessageBox::Ok)
{
for (QString section : collisionSections)
{
if (!section.isEmpty())
{
qDebug() << "Emitting nodeON for:" << section;
emit nodeOn(section);
}
}
}
else
{
throw "unknown button";
}
}
首先,open()将您的插槽连接到finished()信号,没有参数,或者如果第一个插槽参数是指针(您的情况),则连接到buttonClicked()信号。
。您没有正确地传递参数。您不能在声明期间这样做,slot接收的参数是在信号发射中设置的,在您的情况下,您无法控制。在SLOT中,你只能声明参数TYPES而不是它们的值(注意SLOT只是一个宏,实际上SLOT(…)的结果只是一个字符串(char*))。
我建议你对消息框使用setProperty()方法,例如:
msgBox->setModal(false);
msgBox->setProperty("collisionSections", collisionSections);
msgBox->open(this, SLOT(msgBoxClosed(QAbstractButton *)));
你的位置可以是这样的:
void MainWindow::msgBoxClosed(QAbstractButton *button) {
QMessageBox *msgBox = (QMessageBox *)sender();
QStringList collisionSections = msgBox->property("collisionSections").toStringList();
相关文章:
- Qt VTK交互风格的信号到小部件
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 线程之间的布尔停止信号
- 将依赖名称显式标记为类型名和模板的奇怪之处
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 为什么g++在未执行的代码处标记强制转换错误
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 有可能在信号处理程序中设置promise吗
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 在条件变量中触发错误信号的频率是多少
- 在 C++ 中访问数组负索引处的内存不会返回垃圾
- 在 std::无符号字符的向量处存储 int 的十六进制表示形式
- 在多个时间戳处执行函数
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 调试和自由执行中的信号处理
- 升压信号2将插槽传递到成员功能以断开连接
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 将参数从QtMessageBox信号传递到buttonClick信号处理程序槽