Important: Please read the Qt Code of Conduct -

Sending a signal to my mainwindow

  • hi to everyone
    i have a mainwindow and a qwidget. i create a qwidget class to in my mainwindow class
    now i want to when a button on qwidget clicked i send a signal to my mainwindow and my mainwindow do a slot i define
    how can i do it?
    can you give me an example?

  • Let your widget emit a signal when the button is pressed. Let then your main window connect to that signal.

    // ...
    connect(someButton, SIGNAL(clicked()), this, SIGNAL(somethingTriggered()));

    // ...
    connect(myCustomWidget, SIGNAL(somethingTriggered()), this, SLOT(doSomethingUseful()));

  • Please follow the "tutorial": on the Wiki on this.

  • @MyMainWindow(){
    MyWidget * wdgt = new MyWidget(this);
    connect(wdgt, SIGNAL(mySignal()), this, SLOT(onComeSignal()));

    class MyWidget : public QWidget{ Q_OBJECT
    public: MyWidget(QWidget * parent): QWidget(parent){
    QPushButton * btn = new QPushButton(this);
    connect(btn, SIGNAL(clicked (bool), this, SLOT(onClick(bool)));
    private slots: onClick(bool){emit mySignal();}
    signals: void mySignal();

Log in to reply