paul23
paul23

Reputation: 9455

qt, signal slots not connecting?

I have a qdialog, with a buttonbox at the bottom; Why isn't this slot not getting fired when a "signal" occurs? The code look like the following:

    std::unique_ptr<MW::GenStd> box(new MW::GenStd(&tOut, &tIn));
    box->ui.ChoiceButtons->addButton ("Ask",
        QDialogButtonBox::AcceptRole );
    box->ui.ChoiceButtons->addButton ("OverWrite",
        QDialogButtonBox::AcceptRole );
    box->ui.ChoiceButtons->addButton ("merge",
        QDialogButtonBox::AcceptRole );
    box->ui.ChoiceButtons->addButton ("Skip",
        QDialogButtonBox::RejectRole );

    QObject::connect(box->ui.ChoiceButtons, SIGNAL(clicked(QPushButton* b)), box.get(), SLOT(OnClick(QPushButton* b)));

    return box->exec();

Where MW::GenStd is a dialog box (and ui.ChoicButtons a buttonbox). The modal dialog is correctly displayed - however it doesn't seem to interact at all.. Pressing the buttons doesn't fire the event. The slot is declared like the following:

public slots:
    void OnClick(QPushButton* b) {
        auto s(b->text());
        if (s == "Merge") {
            setResult(2);
        } else if (s == "Overwrite") {
            setResult(1);
        } else if (s == "Skip") {
            setResult(0);
        } else if (s == "Ask") {
            setResult(3);
        }
    }

};

(I know it's terribly to do such a string comparison here, but it's just as a quick mock up test to validate the buttons). But debugging shows the function isn't ever reached!

EDIT: as suggested looking at the output showed a culprit: Object::connect: No such signal QDialogButtonBox::clicked(QPushButton*) in AskGUISupport.cpp:150 However that seems totally strange as the QDialogButtonBox does have a clicked signal? documentation

Upvotes: 3

Views: 5204

Answers (3)

Lol4t0
Lol4t0

Reputation: 12557

QDialogButtonBox class does not has signal

clicked(QPushButton*).

It has clicked ( QAbstractButton*) insted.

You should be very precise in signatures, when using signals/slots mechanisms. Any casts not allowed, because Qt uses strings internally to check signatures.

You should use clicked (QAbstractButton*) signature and adjust your slot to accpet QAbstractButton*. Make a slot

void OnClick(QAbstractButton* b);

Upvotes: 0

Dmitriy Kachko
Dmitriy Kachko

Reputation: 2914

QDialogButtonBox has a signal clicked ( QAbstractButton * button ) so you need to define a slot void OnClick(QAbstractButton* b) and connect to it. Use QAbstractButton, not QPushButton.

Upvotes: 1

user362638
user362638

Reputation:

Do not use variable names in connect:

QObject::connect(box->ui.ChoiceButtons, SIGNAL(clicked(QPushButton*)), 
                 box.get(), SLOT(OnClick(QPushButton*)));

Upvotes: 2

Related Questions