Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. bool is converted to another value in signal-slot mechanism?
Qt 6.12 LTS is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by A Former User
    #1

    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]

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

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

      U 1 Reply Last reply
      0
      • ? A Former User

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

        U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        @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???

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

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

              1 Reply Last reply
              3
              • U Offline
                U Offline
                user4592357
                wrote on last edited by
                #7

                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) ;)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved