Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Signal/slot connection doesn't work.

    General and Desktop
    5
    6
    422
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      Demorald last edited by

      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
      
      
      1 Reply Last reply Reply Quote 0
      • V
        VintoreZZ last edited by VintoreZZ

        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)

        1 Reply Last reply Reply Quote 2
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

          @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?

          Qt has to stay free or it will die.

          1 Reply Last reply Reply Quote 0
          • A
            AnneRanch last edited by AnneRanch

            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.

            JonB 1 Reply Last reply Reply Quote 0
            • JonB
              JonB @AnneRanch last edited by JonB

              @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).

              1 Reply Last reply Reply Quote 0
              • A
                AnneRanch last edited by

                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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post