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

Connection ui button to Slot in file.cpp



  • I created two buttons in the central widget of my window from Qtdesigner. But I can not find the buttons to establish a connection to a slot in the file.cpp of my class. I have a slot "affichefenetrecene" and "affichefenetrecenep" that I want to connect to my buttons.
    That's myfile.cpp

    #include "fenprincipale.h"
    #include "ui_fenprincipale.h"
    #include "fencene.h"
    #include "fencenep.h"
    
    FenPrincipale::FenPrincipale(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::FenPrincipale)
    {
        ui->setupUi(this);
        fencen = new QWidget;
        setCentralWidget(fencen);
    
    
        QStackedWidget *centralfencen = new QStackedWidget;
        stackedWidget->addWidget(fencen);
        stackedWidget->addWidget(FenCene());
        stackedWidget->addWidget(FenCenep());
    
        connect(ui->bttoncene, SIGNAL(clicked()), this, SLOT(Affichefencene()));
        connect(ui->bttoncenep, SIGNAL(clicked()), this, SLOT(Affichefencenep()));
    }
    
    FenPrincipale::~FenPrincipale()
    {
        delete ui;
    }
    
    void FenPrincipale::Affichefencene()
    {
        centralfencen->setCentralWidget(FenCene());
    }
    
    void FenPrincipale::Affichefencenep()
    {
       centralfencen->setCentralWidget(FenCenep());
    }
    
    

    I will have to create them manually?
    How can I use the buttons in my file.cpp.



  • Does the class header file include the Q_OBJECT macro? It is required so that the metadata for signals/slots are included. At least that used to be a requirement. I've been in pyqt land for too long...


  • Qt Champions 2017

    Did you declare them under the public slots ? Can you show your header files ? You cannot find means what ? Are you not able to compile the program ? Slot is not called when you click on the button ? Please explain.


  • Qt Champions 2019

    @Nafab213 said in Connection ui button to Slot in file.cpp:

    I will have to create them manually?

    You mean the slots? Yes, you have to implement them.
    What are their names actually? Because in the connect calls they have upper-case first later, but you say you have "affichefenetrecene" and "affichefenetrecenep". C++ is case sensitive.


  • Moderators

    @Nafab213
    how does your header file looks like, did you actually place Affichefencene and Affichefencenep under the slots macro ? You're using the old style qt4 connect so that is a requirement.

    I suggest the following, as it will either work or give you compile time errors:

    connect(ui->bttoncene, &QPushButton::clicked, this, &FenPrincipale::Affichefencene);
        connect(ui->bttoncenep,  &QPushButton::clicked, this, &FenPrincipale::Affichefencenep);
    


  • @dheerendra said in Connection ui button to Slot in file.cpp:

    Did you declare them under the public slots ? Can you show your header files ? You cannot find means what ? Are you not able to compile the program ? Slot is not called when you click on the button ? Please explain.

    is public slots: actually required in the declarations or is it just a nice visual queue? My foggy memory seems to indicate that it was not strictly required, as long as the methods were accessible where they are connected.


  • Moderators

    @Kent-Dorfman
    requiered for Qt4 macros, visual queue for Qt5 syntax


Log in to reply