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. Performance of a one time signal/slot connection?
Forum Updated to NodeBB v4.3 + New Features

Performance of a one time signal/slot connection?

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

    I have a vector that is going to store a bunch of the same widget with a signal, and I want to dynamically connect them to the same slot at runtime.

    At runtime, I will have an action that will trigger a function that's going to dynamically create signal/slot connections from the selected widget.

    void MainWindow::transformNumber(int num) // this will triggered by arrow key
    {    
        int currentIndex = m_ui->SomeWidget->currentIndex();
    
        MyWidget * core= m_myVector[currentIndex].core;
       
        connect(core, &MyWidget ::signalNumberChanged,
                    this, &MainWindow::slotUpdateNumber);
    
        core->changeNumber(num); // this will fire signalNumberChanged
    }
    

    As you can see the connection was created inside the triggered function at runtime, and with this code, the slot is going to loop more and more each time with more key press, which is not good.

    I did found a solution on StackOverflow to create a custom connection that will delete itself after first call. But my question is will there be a performance issue if I'm to trigger this function 60 times a second for like 15 minutes?

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

      Then do you connection only once when you fill m_myVector. If you are not able to do so (don't see a reason why it should not be possible) then take a look at Qt::UniqueConnection.
      When you don't trigger a signal the slot will not be executed so there is no performance impact.

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

      L 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Then do you connection only once when you fill m_myVector. If you are not able to do so (don't see a reason why it should not be possible) then take a look at Qt::UniqueConnection.
        When you don't trigger a signal the slot will not be executed so there is no performance impact.

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

        @Christian-Ehrlicher

        How do I add the connection to my vector?

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

          @lansing said in Performance of a one time signal/slot connection?:

          How do I add the connection to my vector?

          Loop through the container and do a connect maybe?

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

          L 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @lansing said in Performance of a one time signal/slot connection?:

            How do I add the connection to my vector?

            Loop through the container and do a connect maybe?

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

            @Christian-Ehrlicher

            This is what I got. Is this correct?

            struct MyPair{
                MyWidget * core;
                QMetaObject::Connection connection;
            }
            
            // mainwindow.cpp
            
            MyPair mp;   
            mp.core= new MyWidget ();        
            mp.connection = connect(mp.core, &MyWidget::signalNumberChanged,
                                        this, &MainWindow::slotUpdateNumber); 
            
            m_myVector.append(mp);
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @lansing said in Performance of a one time signal/slot connection?:

              Is this correct?

              More or less, why do you need to store the connection?

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

              L 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @lansing said in Performance of a one time signal/slot connection?:

                Is this correct?

                More or less, why do you need to store the connection?

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

                @Christian-Ehrlicher

                Didn't you say to add it to the vector?

                Pl45m4P 1 Reply Last reply
                0
                • L lansing

                  @Christian-Ehrlicher

                  Didn't you say to add it to the vector?

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

                  @lansing

                  He was talking about something like this:

                  
                  QVector<MyWidget> vec;
                  MyWidget *w = new MyWidget();
                  
                  // Make connection before MyWidget is added to vector
                  connect(w, &MyWidget ::signalNumberChanged,
                                  this, &MainWindow::slotUpdateNumber);
                  
                  vec.push_back(w);

                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  L 1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

                    @lansing

                    He was talking about something like this:

                    
                    QVector<MyWidget> vec;
                    MyWidget *w = new MyWidget();
                    
                    // Make connection before MyWidget is added to vector
                    connect(w, &MyWidget ::signalNumberChanged,
                                    this, &MainWindow::slotUpdateNumber);
                    
                    vec.push_back(w);
                    L Offline
                    L Offline
                    lansing
                    wrote on last edited by
                    #9

                    @Pl45m4

                    Oh, thank you, this looks better

                    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