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. difference between connect functions
QtWS25 Last Chance

difference between connect functions

Scheduled Pinned Locked Moved Solved General and Desktop
signals&slotsconnect
7 Posts 3 Posters 810 Views
  • 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.
  • C Offline
    C Offline
    Corvette653
    wrote on last edited by
    #1

    Hi.
    Until today I thought these lines do the same

        connect(combo, &QComboBox::currentIndexChanged, this, &MainWindow::ComboChanged);/*doesn't work*/
        connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(ComboChanged(int)));/*works*/
    

    and I was using first one cause it is more legible(at least in my eyes).
    Unfortunately it doesn't because like you can see in comments only one of them works.
    My question is very simple - why?

    mainwindow.h:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        MainWindow();
        ~MainWindow() {}
        QComboBox *combo = new QComboBox;
        QGridLayout *mainLayout = new QGridLayout;
        QWidget *mainWidget = new QWidget;
    public slots:
        void ComboChanged(int i);
    };
    

    mainwindow.cpp:

    #include "mainwindow.h"
    MainWindow::MainWindow(){
        combo->addItem("index 0");
        combo->addItem("index 1");
        mainLayout->addWidget(combo);
        mainWidget->setLayout(mainLayout);
        setCentralWidget(mainWidget);
    //  connect(combo, &QComboBox::currentIndexChanged, this, &MainWindow::ComboChanged); - doesn't work
        connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(ComboChanged(int)));/* - works*/
    }
    void MainWindow::ComboChanged(int i){qDebug() << "everything works fine";}
    
    VRoninV 1 Reply Last reply
    0
    • C Corvette653

      Hi.
      Until today I thought these lines do the same

          connect(combo, &QComboBox::currentIndexChanged, this, &MainWindow::ComboChanged);/*doesn't work*/
          connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(ComboChanged(int)));/*works*/
      

      and I was using first one cause it is more legible(at least in my eyes).
      Unfortunately it doesn't because like you can see in comments only one of them works.
      My question is very simple - why?

      mainwindow.h:

      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      public:
          MainWindow();
          ~MainWindow() {}
          QComboBox *combo = new QComboBox;
          QGridLayout *mainLayout = new QGridLayout;
          QWidget *mainWidget = new QWidget;
      public slots:
          void ComboChanged(int i);
      };
      

      mainwindow.cpp:

      #include "mainwindow.h"
      MainWindow::MainWindow(){
          combo->addItem("index 0");
          combo->addItem("index 1");
          mainLayout->addWidget(combo);
          mainWidget->setLayout(mainLayout);
          setCentralWidget(mainWidget);
      //  connect(combo, &QComboBox::currentIndexChanged, this, &MainWindow::ComboChanged); - doesn't work
          connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(ComboChanged(int)));/* - works*/
      }
      void MainWindow::ComboChanged(int i){qDebug() << "everything works fine";}
      
      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      only one of them works.

      it's explained in the docs: https://doc.qt.io/qt-5/qcombobox.html#currentIndexChanged

      I thought these lines do the same

      They do achieve the same result in 2 different ways: https://wiki.qt.io/New_Signal_Slot_Syntax

      Basically in new code you should never use the old SIGNAL()/SLOT() method

      "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
      • C Offline
        C Offline
        Corvette653
        wrote on last edited by
        #3

        Thank you so much for this link.
        I think I understand everything except one.
        "There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject."
        what if I delete an object?
        for example change the last line in above code:

        void MainWindow::ComboChanged(int i){delete combo;}
        
        jsulmJ VRoninV 2 Replies Last reply
        0
        • C Corvette653

          Thank you so much for this link.
          I think I understand everything except one.
          "There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject."
          what if I delete an object?
          for example change the last line in above code:

          void MainWindow::ComboChanged(int i){delete combo;}
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Corvette653 combo is not the receiver, combo is the emitter. You should not delete widgets like this, do

          combo->deleteLater();
          

          instead.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Corvette653 combo is not the receiver, combo is the emitter. You should not delete widgets like this, do

            combo->deleteLater();
            

            instead.

            C Offline
            C Offline
            Corvette653
            wrote on last edited by
            #5

            @jsulm
            does this line automatically disconnect or should I do this by myself like here?

            //connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::ComboChanged);
            //...
            void MainWindow::ComboChanged(int i){
                disconnect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::ComboChanged);
                combo->deleteLater();
            }
            
            jsulmJ 1 Reply Last reply
            0
            • C Corvette653

              @jsulm
              does this line automatically disconnect or should I do this by myself like here?

              //connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::ComboChanged);
              //...
              void MainWindow::ComboChanged(int i){
                  disconnect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::ComboChanged);
                  combo->deleteLater();
              }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Corvette653 No need to disconnect manually.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • C Corvette653

                Thank you so much for this link.
                I think I understand everything except one.
                "There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject."
                what if I delete an object?
                for example change the last line in above code:

                void MainWindow::ComboChanged(int i){delete combo;}
                
                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                @Corvette653 said in difference between connect functions:

                "There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject."

                That refers to the 3 arguments version of connect. Take this example: connect(lineEdit,&QLineEdit::textChanged,[label](const QString& text){label->setText(text);});
                if you call delete label; Qt can't know it should not execute the slot anymore and will crash when trying to call label->setText. As mentioned in the docs, in the following phrase, the solution is to add label as a context argument (connect(lineEdit,&QLineEdit::textChanged,label,[label](const QString& text){label->setText(text);});) so that Qt knows that when label is destroyed it should not execute the slot anymore

                "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
                3

                • Login

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