pointer paramters c++
-
Thanks for help..
I have to change this too? >>void MainWindow::on_button_clicked(CurrentState *state, char number) <<
@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.
-
@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.
-
@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. -
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