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

Connect multiple classes to one without individually connecting each class



  • Hello, I have a QStackedWidget with multiple widgets added (pages) and I would like to connect all the widgets within that QStackedWidget to a single class. Is it possible to connect them all in one going inside the stack widget rather than having to individually connect each one?

    pPageOne = new PageOne();
    pPageTwo = new PageTwo();
    pPageThree = new PageThree();
    
    pStackedWidget = new QStackedWidget(this);
    
    pStackedWidget->addWidget(pPageOne);
    pStackedWidget->addWidget(pPageTwo);
    pStackedWidget->addWidget(pPageThree);
    
    //////////////////////////////
    
    //Can this be done in a more efficient way? I am planning on having 20+ pages
    
    connect(pPageOne, SIGNAL(openCertViewer()), this, SLOT(openCertViewer()));
    connect(pPageTwo, SIGNAL(openCertViewer()), this, SLOT(openCertViewer()));
    connect(pPageThree, SIGNAL(openCertViewer()), this, SLOT(openCertViewer()));

  • Moderators

    @AaronKelsey

    for( int i = 0; i < pStackedWidget->count(); ++i )
        connect(pStackedWidget->widget(i), SIGNAL(openCertViewer()), this, SLOT(openCertViewer()));
    


  • This post is deleted!

Log in to reply