bool is converted to another value in signal-slot mechanism?



  • I have a class that emits a signal:

    bool myClass::eventFilter(QObject *object, QEvent *event) {
        if(event->type() == QEvent::MouseButtonRelease) {
            auto o = static_cast<someClass *>(object);
            emit clicked(o->getName(), o->isValid())
            return true;
        }
    
        return QScrollArea::eventFilter(object, event);
    }
    

    clicked()'s declaration:

    void clicked(const QString &name, bool isValid);
    

    and I have another class where I do a connect:

    QObject::connect(ob, SIGNAL(clicked(const QString &, bool)), SLOT(load(const QString &, bool)));
    

    in load I print the passed values and I got 80 and 64 for bool:

    void otherClass::load(const QString &name, bool isValid) {
        std::cout << name.toStdString() << ' ' <<  isValid << std::endl;
    }
    

    When I print the values from eventFilter(), I still get 80 for bool. However, if I print the value of isValid() from another place, I get correct 0 or 1.

    Why is this? and how can I get correct bool values in my slot?

    [Edit: fixed markup ~~ @Wieland]


  • Moderators

    Hi! Please show us the declaration of someClass::isValid.



  • @Wieland

    is just returns a bool data member:

    bool someClass::isValid() const {
        return m_isValid;
    }
    

    and m_isValid is set in the constructor

    EDIT: this is weird. there's another thing that makes me bowl my eyes. getName() and isValid() are const member fucntions, and as i found out they are the reason the program works wrong, 80? what is 80? i tried to emit clicked(QString("1234"), false); and that time i get 0 printed correctly.

    so what is the reason for this? can't i call something during emitting a signal???


  • Lifetime Qt Champion

    Hi,

    One thing, you should use qobject_cast and not static_cast when dealing with QObject based classes.

    Did you try to print what you emit before calling emit ?



  • my bad... i hadn't installed the event filter on the watched object. everything works now.

    @SGaist thanks for the advice. changed to qobject_cast. well at least i learned that from my stupid mistake



  • And always check the return of qobject_cast<> as per
    https://forum.qt.io/topic/83689/setmodeldata-crashes-program/14 onward :)



  • i know, i know! my app crashed too :( then i checked the return value and everything is okay now!

    p.s. everyone should learn from their own pain (i mean the crash) ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.