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 Offline
    H Offline
    hye2
    wrote on last edited by
    #1

    hello

    i want to use pointer paramters like >>void MainWindow::on_button_clicked(CurrentState *state, char number)

    so

    i do
    connect(button, SIGNAL(clicked(CurrentState,char)), this , SLOT(on_button_clicked(CurrentState,char)))

    but it doesn't work how can i use pointer paramters to clicked()

    also
    mainwindow.h

    public slots:
    void on_button_clicked(CurrentState *state ,char number);

    1 Reply Last reply
    0
    • Kent-DorfmanK Online
      Kent-DorfmanK Online
      Kent-Dorfman
      wrote on last edited by Kent-Dorfman
      #2

      I dont remebmer if pointers are registered with the MOC metadata system as allowable params in signals, and to be frank, I don't use them that way...I do know that size_t is a registered and allowable type, and in a platform dependent fashion you can usually directly cast between size_t and a pointer. Does that bit of witchcraft help?

      If you have to pass complex types in signals then I question your design. Something like CurrentState smells o me an awful lot like an enum. which are essentially incognito integers.

      C 1 Reply Last reply
      0
      • H Offline
        H Offline
        hye2
        wrote on last edited by
        #3

        The currentstate is intended to receive mechanical data inside the my company... so i must use that...

        1 Reply Last reply
        0
        • Kent-DorfmanK Kent-Dorfman

          I dont remebmer if pointers are registered with the MOC metadata system as allowable params in signals, and to be frank, I don't use them that way...I do know that size_t is a registered and allowable type, and in a platform dependent fashion you can usually directly cast between size_t and a pointer. Does that bit of witchcraft help?

          If you have to pass complex types in signals then I question your design. Something like CurrentState smells o me an awful lot like an enum. which are essentially incognito integers.

          C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          @Kent-Dorfman said in pointer paramters c++:

          I dont remebmer if pointers are registered with the MOC metadata system as allowable params in signals,

          Signals sending pointers exist in the standard API, e.g. QApplication::focusChanged(), so it has to be possible to create a matching slot.

          @hye2 You should be seeing a run time message regarding failure to make the connection.

          If you wish to marshal a pointer then surely it should look like this:

          // old style
          connect(
            button, SIGNAL(clicked(CurrentState*,char)), 
            this,   SLOT(on_button_clicked(CurrentState*,char))
          );
          

          Of course, "button" has to have a signal declared with a pointer and emit that signal with a pointer.

          H 2 Replies Last reply
          0
          • C ChrisW67

            @Kent-Dorfman said in pointer paramters c++:

            I dont remebmer if pointers are registered with the MOC metadata system as allowable params in signals,

            Signals sending pointers exist in the standard API, e.g. QApplication::focusChanged(), so it has to be possible to create a matching slot.

            @hye2 You should be seeing a run time message regarding failure to make the connection.

            If you wish to marshal a pointer then surely it should look like this:

            // old style
            connect(
              button, SIGNAL(clicked(CurrentState*,char)), 
              this,   SLOT(on_button_clicked(CurrentState*,char))
            );
            

            Of course, "button" has to have a signal declared with a pointer and emit that signal with a pointer.

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

            @ChrisW67

            Thanks for help..

            I have to change this too? >>void MainWindow::on_button_clicked(CurrentState *state, char number) <<

            J.HilkJ 1 Reply Last reply
            0
            • H hye2

              @ChrisW67

              Thanks for help..

              I have to change this too? >>void MainWindow::on_button_clicked(CurrentState *state, char number) <<

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

              @hye2 said in pointer paramters c++:

              on_button_clicked

              you should also change the function name or you will get conflicts/problems with the connect function by name feature of Qt.


              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
              • C ChrisW67

                @Kent-Dorfman said in pointer paramters c++:

                I dont remebmer if pointers are registered with the MOC metadata system as allowable params in signals,

                Signals sending pointers exist in the standard API, e.g. QApplication::focusChanged(), so it has to be possible to create a matching slot.

                @hye2 You should be seeing a run time message regarding failure to make the connection.

                If you wish to marshal a pointer then surely it should look like this:

                // old style
                connect(
                  button, SIGNAL(clicked(CurrentState*,char)), 
                  this,   SLOT(on_button_clicked(CurrentState*,char))
                );
                

                Of course, "button" has to have a signal declared with a pointer and emit that signal with a pointer.

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

                @ChrisW67

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

                jsulmJ J.HilkJ 2 Replies Last reply
                0
                • H hye2

                  @ChrisW67

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

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

                  @hye2 said in pointer paramters c++:

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

                  Then there should be warnings in terminal at runtime.
                  But you should switch to new Qt5 connect syntax, so you will get compiler error if your connect is wrong.

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

                  1 Reply Last reply
                  0
                  • 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

                                          • Login

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