pointer paramters c++
-
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.hpublic slots:
void on_button_clicked(CurrentState *state ,char number); -
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.
-
@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++:
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.
-
@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,,
-
@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
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 }
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); }
.