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. pointer paramters c++
Forum Updated to NodeBB v4.3 + New Features

pointer paramters c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
30 Posts 7 Posters 3.8k 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.
  • H hye2

    @ChrisW67

    Sorry it doesn't work...
    The build works, but the value doesn't connect..

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #9

    @hye2 what type is button ? does it actually have a signal clicked with arguments ?


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hye2
      wrote on last edited by
      #10
      mainwindow.cpp
      
      mainwindow::mainwindow(Qwidget *parent) : QmainWindow(parent), ui(new Ui::MainWindow)
      {
      ui ->setupUi(this);
      QpushButton *button = new Qpushbutton("button");
      connect(button, SIGNAL(clicked(CurrentState*,char)), this,   SLOT(on_button_clicked(CurrentState*,char)));
      }
      
      MainWindow::~MainWindow()
      {
      delete ui;
      }
      
      void MainWindow::on_button_clicked(CurrentState *state,char number)
      {
      ui->button->setText(state->value);
      }
      
      mainwindow.h
      
      ~~
      public slots:
      void on_button_clicked(CurrentState *state,char number);
      

      Here is my code,,

      jsulmJ 1 Reply Last reply
      0
      • H hye2
        mainwindow.cpp
        
        mainwindow::mainwindow(Qwidget *parent) : QmainWindow(parent), ui(new Ui::MainWindow)
        {
        ui ->setupUi(this);
        QpushButton *button = new Qpushbutton("button");
        connect(button, SIGNAL(clicked(CurrentState*,char)), this,   SLOT(on_button_clicked(CurrentState*,char)));
        }
        
        MainWindow::~MainWindow()
        {
        delete ui;
        }
        
        void MainWindow::on_button_clicked(CurrentState *state,char number)
        {
        ui->button->setText(state->value);
        }
        
        mainwindow.h
        
        ~~
        public slots:
        void on_button_clicked(CurrentState *state,char number);
        

        Here is my code,,

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #11

        @hye2 said in pointer paramters c++:

        QpushButton

        QPushButton has no such signal!
        Please read documentation: https://doc.qt.io/qt-5/qabstractbutton.html#clicked

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply
        2
        • jsulmJ jsulm

          @hye2 said in pointer paramters c++:

          QpushButton

          QPushButton has no such signal!
          Please read documentation: https://doc.qt.io/qt-5/qabstractbutton.html#clicked

          H Offline
          H Offline
          hye2
          wrote on last edited by hye2
          #12

          @jsulm said in pointer paramters c++:

          QPushButton has no such signal!

          oh.. you mean..
          I have to use void QAbstractButton::clicked(bool checked = false) this..? sorry

          jsulmJ 1 Reply Last reply
          0
          • H hye2

            @jsulm said in pointer paramters c++:

            QPushButton has no such signal!

            oh.. you mean..
            I have to use void QAbstractButton::clicked(bool checked = false) this..? sorry

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #13

            @hye2 You can only use signals which exist. If you want to pass additional parameters to your slot switch to Qt5 connect syntax and use a lambda as slot.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            H 1 Reply Last reply
            0
            • jsulmJ jsulm

              @hye2 You can only use signals which exist. If you want to pass additional parameters to your slot switch to Qt5 connect syntax and use a lambda as slot.

              H Offline
              H Offline
              hye2
              wrote on last edited by
              #14

              @jsulm

              I see. Can you help me a little?... I've never used Lambda before...

              JonBJ 1 Reply Last reply
              0
              • H hye2

                @jsulm

                I see. Can you help me a little?... I've never used Lambda before...

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

                @hye2
                Search https://wiki.qt.io/New_Signal_Slot_Syntax and https://doc.qt.io/qt-5/signalsandslots.html for lambda to see a couple of examples. Otherwise lambdas are a feature of C++ nothing to do with Qt, you can look them up in a C++ reference/tutorial. And while you are here stop using your old style SIGNAL/SLOT() macros and switch over the new style connect()s.

                H 2 Replies Last reply
                3
                • JonBJ JonB

                  @hye2
                  Search https://wiki.qt.io/New_Signal_Slot_Syntax and https://doc.qt.io/qt-5/signalsandslots.html for lambda to see a couple of examples. Otherwise lambdas are a feature of C++ nothing to do with Qt, you can look them up in a C++ reference/tutorial. And while you are here stop using your old style SIGNAL/SLOT() macros and switch over the new style connect()s.

                  H Offline
                  H Offline
                  hye2
                  wrote on last edited by
                  #16

                  @JonB

                  Thank you! :)

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @hye2
                    Search https://wiki.qt.io/New_Signal_Slot_Syntax and https://doc.qt.io/qt-5/signalsandslots.html for lambda to see a couple of examples. Otherwise lambdas are a feature of C++ nothing to do with Qt, you can look them up in a C++ reference/tutorial. And while you are here stop using your old style SIGNAL/SLOT() macros and switch over the new style connect()s.

                    H Offline
                    H Offline
                    hye2
                    wrote on last edited by
                    #17

                    @JonB

                    [](CurrentState *state, char number){ //something }

                    it is right..?

                    JonBJ S 2 Replies Last reply
                    0
                    • H hye2

                      @JonB

                      [](CurrentState *state, char number){ //something }

                      it is right..?

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

                      @hye2 said in pointer paramters c++:

                      [](CurrentState *state, char number){ //something }

                      Yes, that is the right syntax:

                      • Inside the [...] you pass any context/local variables from where you are defining the lambda that you want the lambda to have access to, if any.
                      • Inside the (...) you declare the parameters which the signal passes to the slot, if any.

                      For example, a slot for QLineEdit::textChanged(const QString &text) signal applied to a whole bunch of QLineEdits where we want to know which one was changed, as well as the new text:

                      for (QLineEdit *lineEdit : bunchOfLineEdits)
                          connect(lineEdit, &QLineEdit::textChanged, [lineEdit](const QString &text) { qDebug() << lineEdit->objectName() << text; });
                      
                      H 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @hye2 said in pointer paramters c++:

                        [](CurrentState *state, char number){ //something }

                        Yes, that is the right syntax:

                        • Inside the [...] you pass any context/local variables from where you are defining the lambda that you want the lambda to have access to, if any.
                        • Inside the (...) you declare the parameters which the signal passes to the slot, if any.

                        For example, a slot for QLineEdit::textChanged(const QString &text) signal applied to a whole bunch of QLineEdits where we want to know which one was changed, as well as the new text:

                        for (QLineEdit *lineEdit : bunchOfLineEdits)
                            connect(lineEdit, &QLineEdit::textChanged, [lineEdit](const QString &text) { qDebug() << lineEdit->objectName() << text; });
                        
                        H Offline
                        H Offline
                        hye2
                        wrote on last edited by
                        #19

                        @JonB

                        Ok like this..?

                        mainwindow.cpp

                        QpushButton *button = new Qpushbutton("button");
                        connect(button, Qpushbutton::clicked,[button](CurrentState *state, char number){ //somthing } );
                        
                        JonBJ 1 Reply Last reply
                        0
                        • H hye2

                          @JonB

                          [](CurrentState *state, char number){ //something }

                          it is right..?

                          S Offline
                          S Offline
                          SimonSchroeder
                          wrote on last edited by
                          #20

                          @hye2 said in pointer paramters c++:

                          [](CurrentState *state, char number){ //something }
                          it is right..?

                          Well, the syntax is right for a lambda. However, in the context of what you are trying to achieve it will not help. QPushButton has a signal clicked(bool). So, your 'slot' (which will be the lambda in this case) has to have a bool as parameter or no parameter at all (you can drop parameters from the end of the function call when using slots).

                          I assume that state and number are member variables of your class. In this case you need to capture this inside the lambda to have access to these. The lambda you are most likely looking for is [this](){ on_button_clicked(state, number); }.

                          1 Reply Last reply
                          1
                          • J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #21

                            I'm getting the feeling, you're trying to run before you can crawl.

                            why don't you simply subclass QPushButton, add the signal you want and emit it where and when you want it, with the parameters you specified.


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            1
                            • H hye2

                              @JonB

                              Ok like this..?

                              mainwindow.cpp

                              QpushButton *button = new Qpushbutton("button");
                              connect(button, Qpushbutton::clicked,[button](CurrentState *state, char number){ //somthing } );
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #22

                              @hye2

                              connect(button, Qpushbutton::clicked,[button](CurrentState *state, char number){ //somthing } );

                              • You only need to pass [button] if you will wish to access button in your { ... } lambda body code, else it's not needed.

                              • QPushbutton::clicked, [...](CurrentState *state, char number) cannot be right because the signature for the signal is void QAbstractButton::clicked(bool checked = false), and you cannot alter that fact. You can add further parameters to the slot in the lambda's [...], but you cannot alter what is inside the (...) unless you define your own signal which passes extra/different parameters. So where exactly do your CurrentState *state, char number come from?

                              1 Reply Last reply
                              2
                              • H Offline
                                H Offline
                                hye2
                                wrote on last edited by hye2
                                #23
                                This post is deleted!
                                JonBJ 1 Reply Last reply
                                0
                                • H hye2

                                  This post is deleted!

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

                                  @hye2 said in pointer paramters c++:

                                  It come from other cpp(inside project) for connect to device

                                  That does not mean much to me. What does it have to do with the QPushButton::clicked signal? Why/how does it get passed from a signal to your slot? Can your connect() statement "see" these variables? Or are these variables just as "visible" to your slot code? You need to understand this in order for you or anybody else to answer what you will need, saying "My boss wants to create a Gui using the existing code.." won't help....

                                  H 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @hye2 said in pointer paramters c++:

                                    It come from other cpp(inside project) for connect to device

                                    That does not mean much to me. What does it have to do with the QPushButton::clicked signal? Why/how does it get passed from a signal to your slot? Can your connect() statement "see" these variables? Or are these variables just as "visible" to your slot code? You need to understand this in order for you or anybody else to answer what you will need, saying "My boss wants to create a Gui using the existing code.." won't help....

                                    H Offline
                                    H Offline
                                    hye2
                                    wrote on last edited by hye2
                                    #25

                                    Oh, you asked me that. I got it wrong.

                                    State is to get a value from the machine's current state, so when you click on a button, it sends a value to another function and prints the changed state value back on the label, and the number is to get the current machine's number.

                                    So I tried to use state, number

                                    Here is my sample code

                                    void MainWindow::on_button_clicked(Secret *secret, CurrentState *state, char number)
                                    {
                                    double value;
                                    Qstring value1;
                                    value1 = ui ->valueLine -> text();
                                    value = value1.toDouble();
                                    
                                    char i;
                                    quency(&secret[0], value);
                                    for(i = 0; i <number; i++){
                                    }
                                         ui -> Name -> setText(state->quency_value);
                                    }
                                    

                                    Before I tried to get the value elsewhere, but I gave up because I couldn't use state

                                    JonBJ 1 Reply Last reply
                                    0
                                    • H hye2

                                      Oh, you asked me that. I got it wrong.

                                      State is to get a value from the machine's current state, so when you click on a button, it sends a value to another function and prints the changed state value back on the label, and the number is to get the current machine's number.

                                      So I tried to use state, number

                                      Here is my sample code

                                      void MainWindow::on_button_clicked(Secret *secret, CurrentState *state, char number)
                                      {
                                      double value;
                                      Qstring value1;
                                      value1 = ui ->valueLine -> text();
                                      value = value1.toDouble();
                                      
                                      char i;
                                      quency(&secret[0], value);
                                      for(i = 0; i <number; i++){
                                      }
                                           ui -> Name -> setText(state->quency_value);
                                      }
                                      

                                      Before I tried to get the value elsewhere, but I gave up because I couldn't use state

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

                                      @hye2

                                      State is to get a value from the machine's current state

                                      I'm afraid this isn't clear enough to give you an answer. What does this have to do with the button, or clicking it? Do you have multiple buttons each of which each are (somehow) associated with a different machine/state? Or do you only have one machine state regardless of button? Or what?

                                      [This post made before you showed code, looking at that now...]

                                      Your code does not help answer the question. It shows your "desired" slot behaviour, but that is not what matters. You are not explaining where the parameters you show this slot taking come from, whether they vary per button etc. I cannot keep asking for clarification on that, so I cannot tell you how to write the signal/slot/lambda till that is clear....

                                      H 1 Reply Last reply
                                      0
                                      • JonBJ JonB

                                        @hye2

                                        State is to get a value from the machine's current state

                                        I'm afraid this isn't clear enough to give you an answer. What does this have to do with the button, or clicking it? Do you have multiple buttons each of which each are (somehow) associated with a different machine/state? Or do you only have one machine state regardless of button? Or what?

                                        [This post made before you showed code, looking at that now...]

                                        Your code does not help answer the question. It shows your "desired" slot behaviour, but that is not what matters. You are not explaining where the parameters you show this slot taking come from, whether they vary per button etc. I cannot keep asking for clarification on that, so I cannot tell you how to write the signal/slot/lambda till that is clear....

                                        H Offline
                                        H Offline
                                        hye2
                                        wrote on last edited by
                                        #27

                                        @JonB

                                        Do you have multiple buttons each of which each are (somehow) associated with a different machine/state? yes
                                        Or do you only have one machine state regardless of button? yes it have one maichine state regardless of button

                                        machine state just one, but it has muliple button

                                        JonBJ 1 Reply Last reply
                                        0
                                        • H hye2

                                          @JonB

                                          Do you have multiple buttons each of which each are (somehow) associated with a different machine/state? yes
                                          Or do you only have one machine state regardless of button? yes it have one maichine state regardless of button

                                          machine state just one, but it has muliple button

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

                                          @hye2 said in pointer paramters c++:

                                          Do you have multiple buttons each of which each are (somehow) associated with a different machine/state? yes
                                          Or do you only have one machine state regardless of button? yes it have one maichine state regardless of button

                                          You are not helping yourself or me with your answers. You need to think about them! This is not possible. I asked you whether you have one single machine state OR separate machine states per button. And you have answered "yes" to both! :( "Yes" you have different machines/states and "Yes" you only have one machine/state....

                                          machine state just one, but it has muliple button

                                          If there is only one machine/state, why do you need to pass it as a parameter in any clicked signal, you can just as easily retrieve it directly from the slot, it does not need to be a parameter from the signal?

                                          Please think about what the situation is/what you need to achieve if you want proper help.

                                          For the last time: what different parameters do you actually need to pass from a button's clicked signal to a slot? What varies per button? (secret? state? number?)

                                          H 1 Reply Last reply
                                          2

                                          • Login

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