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

Signals and slots in Mainwindow



  • Hi,
    how to pass signal to Mainwindow from other class

    MainWindow *mn=new MainWindow();
    connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
    emit clicked();
    delete mn;
    its not going to slotButton3() method


  • Lifetime Qt Champion

    Hi @Bharth,

    MainWindow *mn=new MainWindow();
    connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
    emit clicked();

    Shouldn't that be a signal from the button?

    delete mn;

    You are deleting the window here, therefore the slot is not called.

    You should try the connect code in a Qt Widgets Application (File > New File or Project > Applications > Qt Widgets Application) created by Qt Creator.

    Regards



  • Is this somewhere in MainWindow? To me this looks like it's from main.cpp

    connect can't know ui, so you need to do something like

    connect(mn->ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
    

    also, which clicked do you emit? I think it sould be

    emit mn->ui->pushbupushButtonclick->clicked();
    

    It's either that or you need to post some more code i think



  • @Bharth said in Signals and slots in Mainwindow:

    MainWindow *mn=new MainWindow();
    connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()))

    simply replace emit clicked(); with ui->pushButtonclick->clicked();
    will make your example work // if you want to emit click signal from code only



  • form.h
    ///////////////////////////////////////////////
    #include <QWidget>
    
    namespace Ui {
    class Form;
    }
    
    class Form : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Form(QWidget *parent = nullptr);
        ~Form();
         static void doSomething();
    public slots:
         void slotButton1();
         void slotButton2();
    signals:
          void clicked();
    
    private:
        Ui::Form *ui;
    };
    form.cpp
    /////////////////////////////////////////////////////
    #include "form.h"
    #include "ui_form.h"
    #include "form2.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    Form::Form(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Form)
    {
        ui->setupUi(this);
        connect(ui->pushButtonclick, SIGNAL(clicked()), this, SLOT(slotButton1()));
    }
    
    Form::~Form()
    {
        delete ui;
    }
    
    
    
    void Form::slotButton1()
    {
    
    MainWindow *mn=new MainWindow();
    
        connect(ui->pushButtonclick,SIGNAL(clicked()),mn,SLOT(slotButton3()));
        emit ui->pushButtonclick->clicked();
        //delete mn;
    }
    form2.h
    ////////////////////////////////////////////////////
    #ifndef FORM2_H
    #define FORM2_H
    
    #include <QWidget>
    
    namespace Ui {
    class Form2;
    }
    
    class Form2 : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Form2(QWidget *parent = nullptr);
        ~Form2();
    
    private:
        Ui::Form2 *ui;
    };
    
    
    form2.cpp
    /////////////////////////////////////////////
    #include "form2.h"
    #include "ui_form2.h"
    
    Form2::Form2(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Form2)
    {
        ui->setupUi(this);
    }
    
    Form2::~Form2()
    {
        delete ui;
    }
    mainwindow.h
    //////////////////////////////////
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    public slots:
        void slotButton();
        void slotButton3();
    public:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    mainwindow.cpp
    //////////////////////////////////
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "form.h"
    #include "form2.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(slotButton()));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::slotButton()
    {
        Form *f1=new Form();
    
        ui->scrollArea->setWidget(f1);
    
    
    }
    
    void MainWindow::slotButton3()
    {
    
         Form2 *f=new Form2();
         ui->scrollArea_2->setStyleSheet("background-color:red;");
    
    }
    


  • Hi,
    Can you provide code tag from editor (Last in row). it's hard to read like that.


  • Lifetime Qt Champion

    @TobbY
    added code tags.



  • @Bharth

    It won't go to slotButton3();

    in your MainWindow make Form1 and Form2 class variable. instead of creating in function.

    Class Form
    declare signal clicked();
    use:
    connect(ui->pushButtonclick, SIGNAL(clicked()), this, SIGNAL(clicked()));
    instead
    connect(ui->pushButtonclick, SIGNAL(clicked()), this, SLOT(slotButton1()));

    Class MainWindow:
    MainWindow .h
    Form *_form1;
    Form2 *_form2;

    MainWindow constructor
    _form1 = new Form();
    connect(form, SIGNAL(clicked()), this, SLOT(slotButton3()));



  • it is going to slotButton3 in mainwindow i given qDebug<<" " its showing but their its not adding another widget class to mainwindow


  • Qt Champions 2019

    @Bharth

    void MainWindow::slotButton3()
    {
    
         Form2 *f=new Form2();
         ui->scrollArea_2->setStyleSheet("background-color:red;");
    
    }
    
    

    You do not add the widget to any parent widget or layout and you do not call show() on it...


Log in to reply