【问题标题】:Passing parameters from QtMessageBox signals to buttonClick signal Handler slot将参数从 QtMessageBox 信号传递到 buttonClick 信号处理程序槽
【发布时间】:2016-10-31 23:32:25
【问题描述】:

我正在尝试创建一个弹出窗口,该弹出窗口将 (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 src\mainwindow.cpp:272
QObject::connect:  (receiver name: 'MainWindow')

我理解,因为它在那里声明了 SLOT,但我不知道如何去做我想做的事情,即将 QString 作为内容传递给我的信号,并让它与 buttonClicked() 事件很好地配合qmessagebox 在 OK 点击时抛出。我也可能以错误的方式处理这个问题,如果是这样,请告诉我。非常感谢任何帮助!

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

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    首先,open() 将您的插槽连接到不带参数的 finished() 信号,如果第一个插槽参数是指针(您的情况),则连接到 buttonClicked() 信号。

    第二。你没有正确传递参数。您不能在声明期间执行此操作,插槽接收的参数是在信号发射中设置的,在您的情况下您无法控制。在 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();
    

    【讨论】:

    • 非常感谢!这做到了。我听从了你的建议。我所做的唯一更改是将其重命名为 void MainWindow::msgBoxClosed(QAbstractButton *button)
    • 很高兴它对你有用,谢谢你的指点,我想过,但在编辑复制的代码时错过了,心不在焉。我会更新答案。
    • 我建议在 C++ 中使用 C++ 强制转换而不是 C 强制转换。在这种情况下,static_castqobject_castdynamic_cast
    • 同意,如果这里没记错的话,最好使用 qobject_cast。
    猜你喜欢
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多