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. Slot called two times for one signal
Forum Updated to NodeBB v4.3 + New Features

Slot called two times for one signal

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 426 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.
  • F Offline
    F Offline
    fzminwl
    wrote on last edited by
    #1

    I have created some custom signal in a class BrowserTab (inherit from QMainWindow) class which receive and emit another signal :

    BrowserTab::BrowserTab(QWidget *parent, WebPageView *view
                           ,QWebEngineProfile *profile, QString url)
        : QMainWindow(parent)
        , m_Profile(profile)
        , m_webView(view)
    {
       //some initialisations
      [...]
         connect(m_webView, &WebPageView::newWindowRequested,  
           [this](BrowserTab* tab){ emit newWindowTabRequired(tab);
         });
    }
    
    [...]
    

    It's parent ViewTabWidget will receive this signal and call the associated slot.
    My problem is that when the signal is emitted, the slot is called two time.
    I didn't add another connection for this signal, this is the only one!

    ViewTabWidget::ViewTabWidget(QWebEngineProfile *profile,
                                TabsBehavior::Tabs how, QWidget * parent)
        : QTabWidget(parent)
        , fznavName(tr("Fz Navigator"))
        , newTabTitle(tr("New Tab"))
        , m_profile(profile)
    {
        if(how == ViewTabWidget::NewTab){
             BrowserTab *tab = createNewBrowserTab();
             setUpTabConnexions(tab);
             addTab(tab, newTabTitle);
         }
         [...]
    }
    
    void ViewTabWidget::setUpTabConnexions(BrowserTab* newTab)
    {
      [...]
    //The slot is called two time when signal is emitted
      connect(newTab, &BrowserTab::newWindowTabRequired,
              this, &ViewTabWidget::handleNewBrowserTab);
     }
    
    //And here the code for the slot : 
    void ViewTabWidget::handleNewBrowserTab(BrowserTab *tab){
        this->addNewTab(tab, ViewTabWidget::BrowserWindow);
    }
    

    I know that i can add the optional argument Qt::UniqueConnection in the connect() function to avoid the slot being called two time but i don't understand why the slot is called two times.

    connect(newTab, &BrowserTab::newWindowTabRequired,
            this, ViewTabWidget::handleNewBrowserTab, Qt::UniqueConnection)
    

    So i need to understand why the slot is called two times.

    A little help would be very grateful and sorry for my poor English !!

    JonBJ Christian EhrlicherC 2 Replies Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Might be a silly question but are you sure that your method setting connection is only called once ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      0
      • F fzminwl

        I have created some custom signal in a class BrowserTab (inherit from QMainWindow) class which receive and emit another signal :

        BrowserTab::BrowserTab(QWidget *parent, WebPageView *view
                               ,QWebEngineProfile *profile, QString url)
            : QMainWindow(parent)
            , m_Profile(profile)
            , m_webView(view)
        {
           //some initialisations
          [...]
             connect(m_webView, &WebPageView::newWindowRequested,  
               [this](BrowserTab* tab){ emit newWindowTabRequired(tab);
             });
        }
        
        [...]
        

        It's parent ViewTabWidget will receive this signal and call the associated slot.
        My problem is that when the signal is emitted, the slot is called two time.
        I didn't add another connection for this signal, this is the only one!

        ViewTabWidget::ViewTabWidget(QWebEngineProfile *profile,
                                    TabsBehavior::Tabs how, QWidget * parent)
            : QTabWidget(parent)
            , fznavName(tr("Fz Navigator"))
            , newTabTitle(tr("New Tab"))
            , m_profile(profile)
        {
            if(how == ViewTabWidget::NewTab){
                 BrowserTab *tab = createNewBrowserTab();
                 setUpTabConnexions(tab);
                 addTab(tab, newTabTitle);
             }
             [...]
        }
        
        void ViewTabWidget::setUpTabConnexions(BrowserTab* newTab)
        {
          [...]
        //The slot is called two time when signal is emitted
          connect(newTab, &BrowserTab::newWindowTabRequired,
                  this, &ViewTabWidget::handleNewBrowserTab);
         }
        
        //And here the code for the slot : 
        void ViewTabWidget::handleNewBrowserTab(BrowserTab *tab){
            this->addNewTab(tab, ViewTabWidget::BrowserWindow);
        }
        

        I know that i can add the optional argument Qt::UniqueConnection in the connect() function to avoid the slot being called two time but i don't understand why the slot is called two times.

        connect(newTab, &BrowserTab::newWindowTabRequired,
                this, ViewTabWidget::handleNewBrowserTab, Qt::UniqueConnection)
        

        So i need to understand why the slot is called two times.

        A little help would be very grateful and sorry for my poor English !!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @fzminwl
        How many times does setUpTabConnexions() get called, and so do the connect(), for a given tab? Put in a qDebug()/breakpoint to find out.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Might be a silly question but are you sure that your method setting connection is only called once ?

          F Offline
          F Offline
          fzminwl
          wrote on last edited by
          #4

          @SGaist @JonB It is called whenever a new tab is created and ready to be added to the ViewTabWidget widget.

          1 Reply Last reply
          0
          • F fzminwl

            I have created some custom signal in a class BrowserTab (inherit from QMainWindow) class which receive and emit another signal :

            BrowserTab::BrowserTab(QWidget *parent, WebPageView *view
                                   ,QWebEngineProfile *profile, QString url)
                : QMainWindow(parent)
                , m_Profile(profile)
                , m_webView(view)
            {
               //some initialisations
              [...]
                 connect(m_webView, &WebPageView::newWindowRequested,  
                   [this](BrowserTab* tab){ emit newWindowTabRequired(tab);
                 });
            }
            
            [...]
            

            It's parent ViewTabWidget will receive this signal and call the associated slot.
            My problem is that when the signal is emitted, the slot is called two time.
            I didn't add another connection for this signal, this is the only one!

            ViewTabWidget::ViewTabWidget(QWebEngineProfile *profile,
                                        TabsBehavior::Tabs how, QWidget * parent)
                : QTabWidget(parent)
                , fznavName(tr("Fz Navigator"))
                , newTabTitle(tr("New Tab"))
                , m_profile(profile)
            {
                if(how == ViewTabWidget::NewTab){
                     BrowserTab *tab = createNewBrowserTab();
                     setUpTabConnexions(tab);
                     addTab(tab, newTabTitle);
                 }
                 [...]
            }
            
            void ViewTabWidget::setUpTabConnexions(BrowserTab* newTab)
            {
              [...]
            //The slot is called two time when signal is emitted
              connect(newTab, &BrowserTab::newWindowTabRequired,
                      this, &ViewTabWidget::handleNewBrowserTab);
             }
            
            //And here the code for the slot : 
            void ViewTabWidget::handleNewBrowserTab(BrowserTab *tab){
                this->addNewTab(tab, ViewTabWidget::BrowserWindow);
            }
            

            I know that i can add the optional argument Qt::UniqueConnection in the connect() function to avoid the slot being called two time but i don't understand why the slot is called two times.

            connect(newTab, &BrowserTab::newWindowTabRequired,
                    this, ViewTabWidget::handleNewBrowserTab, Qt::UniqueConnection)
            

            So i need to understand why the slot is called two times.

            A little help would be very grateful and sorry for my poor English !!

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @fzminwl said in Slot called two times for one signal:

            I know that i can add the optional argument Qt::UniqueConnection in the connect() function to avoid the slot being called two time but i don't understand why the slot is called two times.

            Did you actually try it? If it works it means you do the connect twice as @JonB and @SGaist already said.

            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
            0
            • F Offline
              F Offline
              fzminwl
              wrote on last edited by
              #6

              Like said @SGaist, that's just a silly question.
              @SGaist said in Slot called two times for one signal:

              Might be a silly question but are you sure that your method setting connection is only called once ?

              I didn't pay attention to the methods that are called in the addNewTab() method. Indeed, setUpTabConnexions() is called twice at some point.

              After using qDebug() and breakpoints, and I was able to find the cause of this problem.
              Sorry to have bothered you!

              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