Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal/slot connection doesn't work.

Signal/slot connection doesn't work.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 968 Views
  • 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 Offline
    D Offline
    Demorald
    wrote on last edited by
    #1

    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
    0
    • V Offline
      V Offline
      VintoreZZ
      wrote on last edited by VintoreZZ
      #2

      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
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on last edited by Anonymous_Banned275
          #4
          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.

          JonBJ 1 Reply Last reply
          0
          • A Anonymous_Banned275
            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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @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
            0
            • A Offline
              A Offline
              Anonymous_Banned275
              wrote on last edited by
              #6

              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
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved