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

Signal/slot connection doesn't work.



  • I launch a Colours class window through my main.cpp:

    int main(int argc, char *argv[]) {
    
      QApplication app(argc, argv);
    
      Colours window;
    
      window.resize(500, 500);
      window.setWindowTitle("Colours");
      window.show();
    
    
      return app.exec();
    }
    
    

    This window does some work, drawing shapes etc.

    
    Colours::Colours(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Colours)
    {
        ui->setupUi(this);
    }
    
    Colours::~Colours()
    {
        delete ui;
    }
    void Colours::paintEvent(QPaintEvent *e) {
    
      Q_UNUSED(e);
    
      doPainting();
    
    }
    
    class MyButton : public QWidget {
    
     public:
         MyButton(QWidget *parent = 0);
    };
    
    MyButton::MyButton(QWidget *parent)
        : QWidget(parent) {
    
      QPushButton *quitBtn = new QPushButton("Quit", this);
      quitBtn->setGeometry(200, 40, 75, 30);
    
      connect(quitBtn, &QPushButton::clicked, qApp, &QApplication::quit);
    }
    
    
    
    void Colours::doPainting() {
    
      QPainter painter(this);
    
      painter.setRenderHint(QPainter::Antialiasing);
      painter.setPen(QColor("#d4d4d4"));
    
      painter.setBrush(QBrush("#8B4513"));
      painter.drawRect(100, 150, 300, 300);
    
    
    
      painter.setBrush(QBrush("#D2691E"));
      painter.drawChord(100, 50, 300, 200, 0, 16*180);
    
      painter.setBrush(QBrush("#A0522D"));
      painter.drawRect(200, 350, 100, 100);
    
      painter.setBrush(QBrush("#8B4513"));
      painter.drawRect(280, 400, 10, 10);
    
    
      QBrush tb(Qt::transparent);
      ui->Button->setPalette(QPalette(tb, tb, tb, tb, tb, tb, tb, tb, tb));
      ui->Button->setFlat(true);
    
      QFont f("Arial", 15, QFont::Bold);
      ui->lineEdit->setFont(f);
    
    void Colours::on_Button_clicked()
    {
        QMessageBox::about(this, "Header", "Information");
    }
    
    
    }
    
    

    But when it comes to using a colours.ui form, nothing works: I create a button in it, go to slot, and regardless of what i type there, nothing happens. Initially i wanted to draw some other shapes using this button, but even that QMessageBox thing there just does not work. I just press the button and nothing happens.

    Any thoughts what causes it?

    colours.h:

    #ifndef COLOURS_H
    #define COLOURS_H
    
    #include <QWidget>
    #include <QDialog>
    #include <QMainWindow>
    #include "ui_mainwindow.h"
    
    namespace Ui {
    class Colours;
    }
    
    class Colours : public QDialog {
    
      public:
        Colours(QWidget *parent = 0);
        ~Colours();
    
      protected:
        void paintEvent(QPaintEvent *e);
    
    private slots:
        void on_pushButton_clicked();
    
        void on_Button_clicked();
    
        void on_save_clicked();
    
    private:
        void doPainting();
         Ui::Colours *ui;
    };
    #endif // COLOURS_H
    
    


  • Aren't you missing Q_OBJECT macro? It is neccessary to make signals/slots work.

    class Colours : public QDialog {
    Q_OBJECT //This is missing
      public:
        Colours(QWidget *parent = 0);
        ~Colours();
    
      protected:
        void paintEvent(QPaintEvent *e);
    
    private slots:
        void on_pushButton_clicked();
    
        void on_Button_clicked();
    
        void on_save_clicked();
    
    private:
        void doPainting();
         Ui::Colours *ui;
    };
    #endif // COLOURS_H
    

    After adding the macro, run qmake (right click on your project -> run qmake)


  • Qt Champions 2019

    @Demorald said in Signal/slot connection doesn't work.:

    MyButton::MyButton(QWidget *parent)
    : QWidget(parent) {

    QPushButton *quitBtn = new QPushButton("Quit", this);
    quitBtn->setGeometry(200, 40, 75, 30);

    connect(quitBtn, &QPushButton::clicked, qApp, &QApplication::quit);
    }

    What do you try to achieve with this? And even if this is not really importat - where do you instantiate it so you can click on this button?



    1. Verify "connect"
      if (connect(.....) )
      ......

    2. Verify "button pushed " - change the function to return value and check for it - in pseudo code or do simple print to validate the function is executed.

      int on_button_pushed ()
      .{
      print " on_button_pushed " executing...

      and / or

      return 0; // success
      }

    if(on_button_pushed())
    print OK
    else
    print failed

    You can do simple - limited to "standard" methods - signal / slot design in QtDesigner - "Edit signal / slot" . The "connect" is done using "drag and drop".
    The "problem" is - you do not have way to check the build code - it needs to be "function" verified.



  • @AnneRanch
    If you are doing return 0; // success you won't want if(on_button_pushed()) print OK as that will give the opposite result from intended. Better make it bool TheClass::on_button_pushed () and have it return true on success. But if on_button_pushed () is a slot which you connect() to a signal you can't check the return value anyway (though of course you can still put qDebug()/print statements inside it if you wish).



  • Sorry , I did not pay attention - the decisions were reversed.
    However, I have a "natural" aversion against using #define(d) bool , true etc.
    I do understand that endless discussions about what is " true " zero or anything else have been conducted and do not wish to start another one here.

    But I hope my (main) point of checking functions returns as an aid in debugging has been understood by the original poster.

    Cheers


Log in to reply