Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Slot called two times for one signal



  • 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 !!


  • Lifetime Qt Champion

    Hi,

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



  • @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.



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


  • Lifetime Qt Champion

    @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.



  • 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!


Log in to reply