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...
-
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.
-
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.@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. -
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.@Nafab213
how does your header file looks like, did you actually placeAffichefencene
andAffichefencenep
under theslots
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);
-
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.
@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.
-
@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.
@Kent-Dorfman
requiered for Qt4 macros, visual queue for Qt5 syntax