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. How to replicate a click to a parent without subclassing the widget?
Forum Updated to NodeBB v4.3 + New Features

How to replicate a click to a parent without subclassing the widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 636 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.
  • R Offline
    R Offline
    Roberrt
    wrote on last edited by Roberrt
    #1

    How do I replicate a click into a QPushButton to her parent (a QFrame) using a lambda connection?

        connect(but, &QPushButton::clicked, this, [this, but]        
        {        
                  //but->parentWidget()-> ?          
        });
    
    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      A click is a series of mouse press and release events on the same widget. QPushButton emits a clicked signal when it gets a release after a press. A QFrame does not have a notion of a "click". It's not a widget that is meant to be interactive, so "replicate click in QFrame" doesn't have any meaning.

      You can send press/release events though. See QCoreApplication::sendEvent. You could send a mouse press followed by mouse release events, but you won't get a "clicked" signal from a QFrame. It doesn't have one. If you don't subclass QFrame those events will be just ignored, as that's the default implementation for QFrame.

      What are you trying to achieve with this? Sounds like a wrong solution to a problem that can be tackled in another way.

      R 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        A click is a series of mouse press and release events on the same widget. QPushButton emits a clicked signal when it gets a release after a press. A QFrame does not have a notion of a "click". It's not a widget that is meant to be interactive, so "replicate click in QFrame" doesn't have any meaning.

        You can send press/release events though. See QCoreApplication::sendEvent. You could send a mouse press followed by mouse release events, but you won't get a "clicked" signal from a QFrame. It doesn't have one. If you don't subclass QFrame those events will be just ignored, as that's the default implementation for QFrame.

        What are you trying to achieve with this? Sounds like a wrong solution to a problem that can be tackled in another way.

        R Offline
        R Offline
        Roberrt
        wrote on last edited by Roberrt
        #3

        @Chris-Kawa i got what you mean, I'm going with another solution.

        Just a doubt that comes to my mind, what happens if for example I call a connection like:

        connect(but, &QPushButton::clicked, this, [this, but]
        

        twice, with repeated parameters in the same button?

        the previous connection is canceled?
        or do I need somehow to 'cancel' the previous connection?

        Chris KawaC 1 Reply Last reply
        0
        • R Roberrt

          @Chris-Kawa i got what you mean, I'm going with another solution.

          Just a doubt that comes to my mind, what happens if for example I call a connection like:

          connect(but, &QPushButton::clicked, this, [this, but]
          

          twice, with repeated parameters in the same button?

          the previous connection is canceled?
          or do I need somehow to 'cancel' the previous connection?

          Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You'll get two connections and your lambda will be called twice when the event occurs.

          For regular functions you can use additional parameter to connect: Qt::UniqueConnection. That will not create another connection if one with the same parameters already exists, but this will not work with inline lambdas, as those are always unique.

          connect also returns a QMetaObject::Connection object, which you can use to disconnect it when you no longer need it.

          R 1 Reply Last reply
          3
          • Chris KawaC Chris Kawa

            You'll get two connections and your lambda will be called twice when the event occurs.

            For regular functions you can use additional parameter to connect: Qt::UniqueConnection. That will not create another connection if one with the same parameters already exists, but this will not work with inline lambdas, as those are always unique.

            connect also returns a QMetaObject::Connection object, which you can use to disconnect it when you no longer need it.

            R Offline
            R Offline
            Roberrt
            wrote on last edited by
            #5

            @Chris-Kawa said in How to replicate a click to a parent without subclassing the widget?:

            QMetaObject::Connection

                QMetaObject::Connection con = connect(but, &QPushButton::clicked, this, [this]
                {
                });
            
                // And when I no longer need it:
                disconnect(con);
            

            Just this or do I need something more?

            Chris KawaC 1 Reply Last reply
            0
            • R Roberrt

              @Chris-Kawa said in How to replicate a click to a parent without subclassing the widget?:

              QMetaObject::Connection

                  QMetaObject::Connection con = connect(but, &QPushButton::clicked, this, [this]
                  {
                  });
              
                  // And when I no longer need it:
                  disconnect(con);
              

              Just this or do I need something more?

              Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Just this or do I need something more?

              That's all.

              R 1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                Just this or do I need something more?

                That's all.

                R Offline
                R Offline
                Roberrt
                wrote on last edited by
                #7

                @Chris-Kawa

                Thank you, last question, do you know if its "safe" to do such thing:

                // Try disconnecting any existing connection even if there'snt any
                QObject::disconnect(but, &QPushButton::clicked, this, nullptr);
                
                QMetaObject::Connection con = connect(but, &QPushButton::clicked, this, [this]
                {
                });
                

                I run it and I didn't see any error or exception, but IDK if its 'safe'

                Chris KawaC 1 Reply Last reply
                0
                • R Roberrt

                  @Chris-Kawa

                  Thank you, last question, do you know if its "safe" to do such thing:

                  // Try disconnecting any existing connection even if there'snt any
                  QObject::disconnect(but, &QPushButton::clicked, this, nullptr);
                  
                  QMetaObject::Connection con = connect(but, &QPushButton::clicked, this, [this]
                  {
                  });
                  

                  I run it and I didn't see any error or exception, but IDK if its 'safe'

                  Chris KawaC Online
                  Chris KawaC Online
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  @Roberrt Depends on what you mean safe. It's not gonna crash or anything like that. It disconnects everything that's connected to the clicked signal of that particular button. I don't think Qt connects anything to QPushButton's clicked signal internally, so it should be just your connections. This should be fine in simple cases, but in general it's better to be explicit. You never know what connections will be made in future refactoring. You can cause someone to loose a lot of hair trying to figure out why their connections get severed. For some classes Qt also makes some connections internally e.g. when you set a data model on a view. It's easy to break some functionalities doing such bulk disconnections.

                  You can always replace that lambda with a normal function and avoid all that disconnecting trouble using Qt::UniqueConnection.

                  1 Reply Last reply
                  1

                  • Login

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