Solved Connecting Buttons with custom signals and slots
-
Re:
https://forum.qt.io/topic/120733/connecting-buttons-of-one-page-to-label-of-another-page/13#ifndef MAINWIN_H #define MAINWIN_H #include <QMainWindow> #include<QMessageBox> #include<QPushButton> #include<navigation.h> QT_BEGIN_NAMESPACE namespace Ui { class mainwin; } QT_END_NAMESPACE class mainwin : public QMainWindow { Q_OBJECT public: mainwin(QWidget *parent = nullptr); ~mainwin(); navigation *nav_bar; private slots: void homeButtonClickedSlot(); private slots: void on_home_button_clicked(); void on_about_button_clicked(); void on_contact_button_clicked(); void on_settings_button_clicked(); void on_charts_button_clicked(); private: Ui::mainwin *ui; }; #endif // MAINWIN_H
This is mainwin.h
#include "mainwin.h" #include "ui_mainwin.h" mainwin::mainwin(QWidget *parent) : QMainWindow(parent) , ui(new Ui::mainwin) { ui->setupUi(this); //connection to buttons nav_bar = new navigation; connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot())); } mainwin::~mainwin() { delete ui; } void mainwin::homeButtonClickedSlot() { on_home_button_clicked(); QMessageBox::information(this, "Title", "Hello"); } void mainwin::on_home_button_clicked() { QMessageBox::information(this, "Title" , "Button is clicked"); QStackedWidget *stackWidget = ui->mainApp; stackWidget->setCurrentIndex(0); }
mainwin.cpp
#include "navigation.h" #include "ui_navigation.h" navigation::navigation(QWidget *parent) : QWidget(parent), ui(new Ui::navigation) { ui->setupUi(this); } navigation::~navigation() { delete ui; } void navigation::on_home_clicked() { emit homeButtonClickedSignal(); QMessageBox::information(this,"title","clicked" ); }
navigation.cpp
#ifndef NAVIGATION_H #define NAVIGATION_H #include <QWidget> #include<QPushButton> #include<QMessageBox> namespace Ui { class navigation; } class navigation : public QWidget { Q_OBJECT public: explicit navigation(QWidget *parent = nullptr); ~navigation(); signals: void homeButtonClickedSignal(); private slots: void on_home_clicked(); private: Ui::navigation *ui; }; #endif // NAVIGATION_H
navigation.h
Here the navigation pannel is navigation.ui and whole thing is mainwin.ui
Can anyone say why this is not functioning
-
-
@Thank-You said in Connecting Buttons with custom signals and slots:
why this is not functioning
What is not functioning?
-
@Thank-You I'm almost 100% sure, this "nav_bar" is not the one you see on your screen:
{ ui->setupUi(this); //connection to buttons nav_bar = new navigation; connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot())); }
change it to the following and see if you suddenly have 2 Navbars
{ ui->setupUi(this); //connection to buttons nav_bar = new navigation; nav_bar->resize(100,500); nav_bar->show(); connect(nav_bar,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot())); }
-
@jsulm The buttons are not functioning
-
@J-Hilk You can exactly see my code
https://filebin.net/3o8y2q5zwxb0l0sa
Yes it is the same -
I just checked and its not!!!!!
inside mainwin ui is a navigation item, thats the one you see on screen, the one you create inside the constructor and connect your signals to is a 2nd one and not show!!
You should really test what I wrote and you will see it for yourself instead of simply refusing it.
-
![alt text]( image url)
Yes its working but i dont need this I just want the one in right side not separately
-
@Thank-You
from looking at sour source code,this should work:
{ ui->setupUi(this); //connection to buttons connect(ui->navigation_menu,SIGNAL(homeButtonClickedSignal()),this,SLOT(homeButtonClickedSlot())); }
-
-
@jsulm Yes it did work.
Silly me just using new instead of assigning it to ui->navigation_menu(it is the object name in ui).Thank You
-
@Thank-You said in Connecting Buttons with custom signals and slots:
assigning it to ui->navigation
Not sure what you mean. You should not assign anything to ui->navigation - it is set up when you call
ui->setupUi(this);
-
@jsulm Sorry It was mistake.
I have corrected it.