Search code examples
c++qt6

QT6 - signal/slot between 2 classes


I have created a new class for qpushbutton, I am trying to establish signal slot communication between this class and my mainwindow class

connect(&MyPushButton, &pushbutton::pb_isChecked, this, &MainWindow::MainWindowPBClicked, Qt::DirectConnection);

I used this code but as output only

qDebug("pushButtonClicked");

I can get this output. After that, the slot I called with "emit" does not work.

void pushbutton::pushButtonClicked()
{

    if (isChecked())
    {
        qDebug("pushButtonClicked");
        **emit pb_isChecked();**
    }
}

all code in my project;

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow ui;
    ui.show();

    return app.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include "pushbutton.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public slots:
    void MainWindowPBClicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    pushbutton MyPushButton;
    connect(&MyPushButton, &pushbutton::pb_isChecked, this, &MainWindow::MainWindowPBClicked, Qt::DirectConnection);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::MainWindowPBClicked()
{
    qDebug("MainWindowPBClicked");
}

pushbutton.h

#ifndef PUSHBUTTON_H
#define PUSHBUTTON_H

#include <QWidget>
#include <QPushButton>

class pushbutton : public QPushButton
{
    Q_OBJECT
public:
    explicit pushbutton(QWidget *parent = nullptr);

public slots:
    void pushButtonClicked();
signals:
    void pb_isChecked();


};

#endif // PUSHBUTTON_H

pushbutton.cpp

#include "pushbutton.h"
#include "mainwindow.h"

pushbutton::pushbutton(QWidget *parent)
    : QPushButton{parent}
{
    setAcceptDrops(true);
    connect(this, &QPushButton::clicked, [this]() {
        pushButtonClicked();
    });
}

void pushbutton::pushButtonClicked()
{

    if (isChecked())
    {
        qDebug("pushButtonClicked");
        emit pb_isChecked();
    }
}

Solution

  • CASE 1: Not creating button in UI file: In your class MainWindow, variable MyPushButton is a local variable that gets destroyed after it end its scope in constructor. You need to create a dynamic variable of pushButton like this:

    pushbutton *MyPushButton = new pushButton(this);
    connect(MyPushButton, SIGNAL(pb_isChecked()), this, SLOT(MainWindowPBClicked()), Qt::DirectConnection);
    

    CASE 2: Creating button in UI file: In this case you cannot use your custom signal pb_isChecked(). You will need to use standard QPushButton signal such as clicked()

    connect(ui->MyPushButton, SIGNAL(clicked()), this, SLOT(MainWindowPBClicked()), Qt::DirectConnection);