Next/back buttons
-
Hi all,
I'm trying to implement the following:
Clicking
Next
/Back
button should navigate through1
,2
,3
,4
buttons above and change its color. Let's say if I haveslots
for each of number buttons to change its property.How do I implement using
signals and slots
?Thanks in advance
-
@viniltc said in Next/back buttons:
How do I implement using signals and slots ?
Like any other? What exactly is the problem? You do not need a slot for each of these 1/2/3/4 buttons, just one which is connected to Next button.
In the slot connected to Next button check which of the 1/2/3/4 buttons was selected before and then take the next one. -
// ####################################################### // ############### MainWindow Constructor ################ int m_currentBtn = 0; QPushButton * btn1 = new QPushButton(this, "1"); QPushButton * btn2 = new QPushButton(this, "2"); QPushButton * btn3 = new QPushButton(this, "3"); QPushButton * btn4 = new QPushButton(this, "4"); QPushButton * btnNxt = new QPushButton(this, "Next"); QPushButton * btnPrev = new QPushButton(this, "Back"); QButtonGroup * btnGrp = new QButtonGroup(this); btnGrp->addButton(btn1, 1); btnGrp->addButton(btn2, 2); btnGrp->addButton(btn3, 3); btnGrp->addButton(btn4, 4); btn1->setStylesheet("Style"); // initial style (start with btn1) m_currentBtn = 1; connect(btnNxt, &QPushButton::clicked, this, &MainWindow::nextBtn); connect(btnPrev, &QPushButton::clicked, this, &MainWindow::prevBtn); connect(this, &MainWindow::buttonChanged, this, &MainWindow::paintBtn); // ################################################# void MainWindow::nextBtn() { m_currentBtn++; if(m_currentBtn > btnGrp->buttons().size()) { m_currentBtn = 1 } emit buttonChanged(m_currentBtn); } void MainWindow::prevBtn() { m_currentBtn--; if(m_currentBtn < 1) { m_currentBtn = btnGrp->buttons().size(); } emit buttonChanged(m_currentBtn); } void MainWindow::paintBtn(int id) { for(int i = 1; i < btnGrp->buttons().size() + 1; i++) { if(i == id) { btnGrp->button(id)->setStylesheet("Style"); } else { btnGrp->button(id)->setStylesheet(""); // reset all btns to default } } }
-
@Pl45m4 said in Next/back buttons:
QPushButton * btn1 = new QPushButton(this, "1");
QPushButton * btn2 = new QPushButton(this, "2");
QPushButton * btn3 = new QPushButton(this, "3");
QPushButton * btn4 = new QPushButton(this, "4");
QPushButton * btnNxt = new QPushButton(this, "Next");
QPushButton * btnPrev = new QPushButton(this, "Back");
QButtonGroup * btnGrp = new QButtonGroup(this);hi bro
i see many error in your code .
u can use -> just in pointer no in variable for example btnGrp->buttons()->size() is mistake u should writebtnGrp->buttons().size() ;
secondly you have 4 button but wen btn1 selected and click in back you emit 5 , but btn5 is not exist ["m_currentBtn = btnGrp->buttons()->size() + 1;"] remove +1;
and verry error ...
for using Signal and slot you can write like :connect(btnNxt, SIGNAL(clicked(bool)), this, SLOT(nextBtn())); connect(btnPrev, SIGNAL(clicked(bool)), this, SLOT(prevBtn())); connect(this, SIGNAL(buttonChanged(int)), this, SLOT(paintBtn(int)));
in your class in Header (MainWindow.h ) you can write :
private slots: void nextBtn(); void prevBtn(); void paintBtn(int id); signals: void buttonChanged(int);
now when user click on next or prev (emit buttonChanged) run paintBtn(int id) .
good luck
-
I never had the intention to provide compilable code incl. headers etc... I wrote this to point OP ( @viniltc ) into the right direction.
@mr-ZX said in Next/back buttons:
writebtnGrp->buttons().size()
But yes, you are right in this case :)
@mr-ZX said in Next/back buttons:
secondly you have 4 button but wen btn1 selected and click in back you emit 5 , but btn5 is not exist ["m_currentBtn = btnGrp->buttons()->size() + 1;"] remove +1;
I messed it up, because I copied it from the if-statement above...
@mr-ZX said in Next/back buttons:
and verry error ...
for using Signal and slot you can write likeMy signals and slots are correct... I used the newer, Qt5 syntax.
@mr-ZX said in Next/back buttons:
in your class in Header (MainWindow.h ) you can write :
private slots:
void nextBtn();
void prevBtn();
void paintBtn(int id);
signals:
void buttonChanged(int);As I said before, I never had the intention to write complete and fully compilable program code with de- and constructors, headers and so on...