pointer paramters c++
-
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,,
-
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,,
@hye2 said in pointer paramters c++:
QpushButton
QPushButton has no such signal!
Please read documentation: https://doc.qt.io/qt-5/qabstractbutton.html#clicked -
@hye2 said in pointer paramters c++:
QpushButton
QPushButton has no such signal!
Please read documentation: https://doc.qt.io/qt-5/qabstractbutton.html#clicked -
@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 -
@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.
-
@hye2
Search https://wiki.qt.io/New_Signal_Slot_Syntax and https://doc.qt.io/qt-5/signalsandslots.html forlambda
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 styleSIGNAL
/SLOT()
macros and switch over the new styleconnect()
s. -
@hye2
Search https://wiki.qt.io/New_Signal_Slot_Syntax and https://doc.qt.io/qt-5/signalsandslots.html forlambda
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 styleSIGNAL
/SLOT()
macros and switch over the new styleconnect()
s. -
@hye2
Search https://wiki.qt.io/New_Signal_Slot_Syntax and https://doc.qt.io/qt-5/signalsandslots.html forlambda
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 styleSIGNAL
/SLOT()
macros and switch over the new styleconnect()
s. -
@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 ofQLineEdit
s 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; });
- Inside the
-
@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 ofQLineEdit
s 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; });
- Inside the
-
@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 signalclicked(bool)
. So, your 'slot' (which will be the lambda in this case) has to have abool
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
andnumber
are member variables of your class. In this case you need to capturethis
inside the lambda to have access to these. The lambda you are most likely looking for is[this](){ on_button_clicked(state, number); }
. -
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.
-
Ok like this..?
mainwindow.cpp
QpushButton *button = new Qpushbutton("button"); connect(button, Qpushbutton::clicked,[button](CurrentState *state, char number){ //somthing } );
connect(button, Qpushbutton::clicked,[button](CurrentState *state, char number){ //somthing } );
-
You only need to pass
[button]
if you will wish to accessbutton
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 yourCurrentState *state, char number
come from?
-
-
@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 yourconnect()
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.... -
@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 yourconnect()
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....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
-
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
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....
-
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....
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 buttonmachine state just one, but it has muliple button
-
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 buttonmachine state just one, but it has muliple button
@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 buttonYou 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
?)