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. Problem with signal and slot
Forum Updated to NodeBB v4.3 + New Features

Problem with signal and slot

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 2.9k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Stallman
    wrote on last edited by
    #1

    I'm totally new to QT but I have studied lots of its' documents. However, when I come to code, lots of problem show up.

    But I just can't figure it out what happened to these code:

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "model.h"
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
    {

    ui->setupUi(this);
    

    model *m= new model;

    connect(ui->horizontalSlider,SIGNAL(this->valueChanged(int)),m,SLOT(m.setTemp(double) ) );
    

    }

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

    I don't know why the compiler is always complaining about the @connect() method@

    QObject is an inaccessible base of 'model'

    I'll appreciate you if any examples code with explanations are provided. (signal and slots)
    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You have several errors:

      • The signals and slots signature must match when using thie version of connect

      • You must not give the object in SIGNAL nor in SLOT, just the method.

      • What is model ? a private QObject ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • O Offline
        O Offline
        ogoffart
        wrote on last edited by
        #3

        I beleive you declare your class like this:

        @
        class model : QObject
        @

        But in that case you forgot the public keyword, otherwise it default to private inheritence which is not what you want. So it should be:
        @
        class model : public QObject
        @

        Then, like SGaist said, in the SIGNAL and SLOT, you have to put the exact signature, and the argument must match

        @
        connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),m,SLOT(setTemp(int) ) );
        @

        Which means you need to change setTemp to take an int and not a double. However, if you are using Qt5, i recommand the other syntax which allow automatic conversion of the argument from int to double:

        @
        connect(ui->horizontalSlider,&QSlider::valueChanged,m,&model::setTemp );
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Stallman
          wrote on last edited by
          #4

          SGaist, Olivier Goffart:
          Thank you so much. I really made the mistake which I make a private inheritance. Btw: I found the new sytnax documentation:
          http://qt-project.org/wiki/New_Signal_Slot_Syntax

          Thanks!!

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Stallman
            wrote on last edited by
            #5

            Well, I haven't finished my design yet. My plan is to let user drag the slide to set the tempertaure variable. And the the mehtod setTemp(int) in the class model will call another signal method
            changeColor() to set a QWidget (I don't know what widget can I use, a label?) to show the color.

            Just like:

            @
            #ifndef MODEL_H
            #define MODEL_H
            #include <QObject>
            class model:public QObject
            {
            public:
            model();
            void setTemp(int temparature);
            private:
            double temparature;

            signals:
            void changeColor();
            };

            #endif // MODEL_H

            But I have several quesions here:

            @

            1. The function arguments in the SIGNAL() and SLOT() should be equal but I don't have an argument for the method changeColor();

            2. I want to use the method changeColor() to decide the color to be represented with some if else judgement. But I think it's a little redundant. I ask for a good design. Any good suggestion?

            3. Should I write the connect function in the MainWindow class or where?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You can e.g. add a QColor parameter to changeColor so you have only once place that handles that.

              Yes, a QLabel is fine for that.

              Where will your QLabel be ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Stallman
                wrote on last edited by
                #7

                I will put my QLabel in MainWindow class. But I how can resolve the signal and slot problem? They don't have correspoding arguments.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Then add a slot to your MainWindow that takes a QColor parameter and update the QLabel content in there.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Stallman
                    wrote on last edited by
                    #9

                    Ok, should I define the slot function in MainWindow or can I define the slot function in another class like A and then inheriting A?

                    I don't want to put all the code together in one class~

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      If you are thinking about inheriting both from QMainWindow and from A then no, you can't you can only inherit from one QObject and it also must be the first class to be inherited from.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Stallman
                        wrote on last edited by
                        #11

                        All right. Thank you.

                        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