Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Where did the signals go when I triggered them from the ui?

Where did the signals go when I triggered them from the ui?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 992 Views 1 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.
  • L Offline
    L Offline
    lansing
    wrote on last edited by
    #1

    I'm getting confuse. I have a lineEdit and a plainTextEdit, and I want to trigger a search function when I hit "enter" on the lineEdit. I can do that in the ui by choosing "returnPressed()" as the signal, but where is this signal and the connect call in the cpp?

    Only the slot was generated with the name "on_xxx_returnPressed" in the cpp and header file, is this an implicit format required for the magic to happen? I want the name of the slot to be more meaningful, but when I changed it, the magic goes away.

    J.HilkJ 1 Reply Last reply
    0
    • L lansing

      I'm getting confuse. I have a lineEdit and a plainTextEdit, and I want to trigger a search function when I hit "enter" on the lineEdit. I can do that in the ui by choosing "returnPressed()" as the signal, but where is this signal and the connect call in the cpp?

      Only the slot was generated with the name "on_xxx_returnPressed" in the cpp and header file, is this an implicit format required for the magic to happen? I want the name of the slot to be more meaningful, but when I changed it, the magic goes away.

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      @lansing thats a feature of QMetaObject::connectSlotsByName

      if you don't want to rely on the auto connection by function name, you'll have to do a QObject::connect declaration yourself.

      take a look at the QObject docu page, should get you started nicely.

      http://doc.qt.io/qt-5/qobject.html#connect


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      5
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        The "magic" happens in QMetaObject::connectSlotsByName which is awful because, as you noticed already, it breaks really fast.

        You can add your own connect in the constructor after the setupUi() call: connect(ui->lineEdit,&QLineEdit::returnPressed,this,&MyClass::mySlot);

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4
        • L Offline
          L Offline
          lansing
          wrote on last edited by
          #4

          Ok thank you guys, that solved the mystery for me. I have watched tutorials on Youtube and they're all teaching this black box magic, but on the project I work on, they don't do it like that.

          Also good to learn that I can reference any existing signal in the connect call.

          J.HilkJ 1 Reply Last reply
          0
          • L lansing

            Ok thank you guys, that solved the mystery for me. I have watched tutorials on Youtube and they're all teaching this black box magic, but on the project I work on, they don't do it like that.

            Also good to learn that I can reference any existing signal in the connect call.

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @lansing Qt's Signal/Slot mechanismn is much more powerful than one would think.

            its a save way to pass data between threads, you can even forgo the Slot all together and connect a signal direct to a lambda.

            Signals and Slots can individually(manualy) be called from your code, etc.

            Lambda example from the docu:

            QByteArray page = ...;
            QTcpSocket *socket = new QTcpSocket;
            socket->connectToHost("qt-project.org", 80);
            
            QObject::connect(socket, &QTcpSocket::connected, [=] () {
                    socket->write("GET " + page + "\r\n");
                });
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            L 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @lansing Qt's Signal/Slot mechanismn is much more powerful than one would think.

              its a save way to pass data between threads, you can even forgo the Slot all together and connect a signal direct to a lambda.

              Signals and Slots can individually(manualy) be called from your code, etc.

              Lambda example from the docu:

              QByteArray page = ...;
              QTcpSocket *socket = new QTcpSocket;
              socket->connectToHost("qt-project.org", 80);
              
              QObject::connect(socket, &QTcpSocket::connected, [=] () {
                      socket->write("GET " + page + "\r\n");
                  });
              
              L Offline
              L Offline
              lansing
              wrote on last edited by
              #6

              @J.Hilk

              Why sometime they use SIGNAL() and SLOT() and sometime they use "&" in the argument?

              J.HilkJ 1 Reply Last reply
              0
              • L lansing

                @J.Hilk

                Why sometime they use SIGNAL() and SLOT() and sometime they use "&" in the argument?

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @lansing the Syntax changed from Qt4 to Qt5, for compatibility reasons, you can still use both, but it is recommanded to use the new QT5 version with the function pointers.

                Take a look here:
                http://doc.qt.io/qt-5/signalsandslots.html
                and here
                https://wiki.qt.io/New_Signal_Slot_Syntax


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                L 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @lansing the Syntax changed from Qt4 to Qt5, for compatibility reasons, you can still use both, but it is recommanded to use the new QT5 version with the function pointers.

                  Take a look here:
                  http://doc.qt.io/qt-5/signalsandslots.html
                  and here
                  https://wiki.qt.io/New_Signal_Slot_Syntax

                  L Offline
                  L Offline
                  lansing
                  wrote on last edited by
                  #8

                  @J.Hilk

                  Okay got it, so the "&" is the newer way.

                  mrjjM 1 Reply Last reply
                  0
                  • L lansing

                    @J.Hilk

                    Okay got it, so the "&" is the newer way.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @lansing
                    Yes its the so called new syntax even not so new anymore.
                    https://wiki.qt.io/New_Signal_Slot_Syntax

                    Its highly recommended to use this syntax as it fails at compile time if something is wrong.
                    the SIGNAL()/SLOT() fails at runtime.

                    Also QMetaObject::connectSlotsByName as mentioned should only be used for test code
                    as it breaks so easy if someone renames a widget or rename the slot.
                    And it first show when app runs, making it a truly no go for production code as
                    that is very unwanted in terms of support and debugging.

                    1 Reply Last reply
                    2

                    • Login

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