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. Arguments' type and number agreements when used in connect

Arguments' type and number agreements when used in connect

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 522 Views 2 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #1

    Hi all,

    Just to explore new stuff, I'd like button 2 to be disabled when button 1 is pressed and enabled when it's released.

    int main(int argc, char *argv[])
    {
      QApplication app(argc, argv);
    
      QWidget* window = new QWidget;
      QPushButton* button1 = new QPushButton("Button 1", window);
      QPushButton* button2 = new QPushButton("Button 2", window);
    
      QHBoxLayout* layout = new QHBoxLayout;
      layout->addWidget(button1);
      layout->addWidget(button2);
    
      window->setLayout(layout);
    
    
      QObject::connect(button1, &QPushButton::pressed, button2, 
      &QPushButton::setDisabled);
      QObject::connect(button1, &QPushButton::released, button2, 
      &QPushButton::setEnabled);
      window->show();
    
      return app.exec();
    }
    

    Here it won't run due to inconsistency in the arguments number (setEnabled/Disabled takes a bool in the absence of one sent by pressed/relased).

    How to solve this, if possible using QObject::connect?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @tomy said in Arguments' type and number agreements when used in connect:

      How to solve this, if possible using QObject::connect?

      By using a lambda as receiver.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Just in case you wonder :)

        
            QObject::connect(button1, &QPushButton::pressed, this, [button2]() {
                button2->setDisabled(true);
            });
        
            QObject::connect(button1, &QPushButton::released, this, [button2]() {
                button2->setDisabled(false);
            });
        
        
        1 Reply Last reply
        6
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by mrjj
          #4

          Slight modification to @mrjj code, as the code example is contained inside main, which makes using this as reference pointer impossible.

          QObject::connect(button1, &QPushButton::pressed, button2, [button2]()->void {
                  button2->setDisabled(true);
              });
          
              QObject::connect(button1, &QPushButton::released, button2, [button2]()->void {
                  button2->setDisabled(false) ;
              });
          

          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.

          tomyT 1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            Slight modification to @mrjj code, as the code example is contained inside main, which makes using this as reference pointer impossible.

            QObject::connect(button1, &QPushButton::pressed, button2, [button2]()->void {
                    button2->setDisabled(true);
                });
            
                QObject::connect(button1, &QPushButton::released, button2, [button2]()->void {
                    button2->setDisabled(false) ;
                });
            
            tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #5

            @J.Hilk
            Thanks. I myself also used button2, since it's the original receiver of the signal. Or simply, it's our target.

            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 Extensions
            • Unsolved