invalid conversion error in signal connection
-
hi, i am not sure what do do with this, i have not been able to find information on what this means or why it happens...
error: invalid conversion from ‘uint8_t’ {aka ‘unsigned char’} to ‘uint8_t*’ {aka ‘unsigned char*’} [-fpermissive]
64 | connect(delayer, &QTimer::timeout, this, &MainWindow::on_TGetButton_clicked(message_arr[0]));
| ~~~~~~~~^
| |
| uint8_t {aka unsigned char}~^ -
@agmar You need to understand what connect actually does and what not. It does NOT pass any parameters to the slot (what you're trying to do in your code). It just establishes the connection between signal and slot. Parameter is passed from signal to slot when the signal is emitted. If you want to pass some parameters to slot which are not passed from the signal (like in your case) you need to use a lambda as slot.
-
@agmar Please post code as text!
This code is not valid C++ code. You can't pass data to the slot when you connect the slot to signal! Parameters are passed when signal is emitted. If you want to pass message_arr[0] you should use a lambda as slot. Also your slot seems to take a pointer but you're passing by value. -
-
@agmar said in invalid conversion error in signal connection:
Whats a lambda?
https://en.cppreference.com/w/cpp/language/lambda
Did you also address "You can't pass data to the slot when you connect the slot to signal!"?
-
@agmar You need to understand what connect actually does and what not. It does NOT pass any parameters to the slot (what you're trying to do in your code). It just establishes the connection between signal and slot. Parameter is passed from signal to slot when the signal is emitted. If you want to pass some parameters to slot which are not passed from the signal (like in your case) you need to use a lambda as slot.
-
-
@jsulm said in invalid conversion error in signal connection:
@agmar You need to understand what connect actually does and what not. It does NOT pass any parameters to the slot (what you're trying to do in your code). It just establishes the connection between signal and slot. Parameter is passed from signal to slot when the signal is emitted. If you want to pass some parameters to slot which are not passed from the signal (like in your case) you need to use a lambda as slot.
hm, well , i seem to understand how a lambda works now , thank you for telling me about them :) however, i still cant get the connection to work, the function with an added lambda looked like this :
connect(delayer, &QTimer::timeout, this, [ message_arr[0] ](uint8_t tx_buffer){&MainWindow::on_TGetButton_clicked;});but it just gives an error for " Expected',' or '}' in lambda capture list not sure what that means, but it would be nice if there would be a way to just write the variable name into the function call with the lambda without changing the contents of the function, because i would still have to go into the function and internally change the definition from message_arr[0]=tx_buffer to newVariable = tx_buffer
Is this something that is possible to do?
-
@agmar said in invalid conversion error in signal connection:
connect(delayer, &QTimer::timeout, this, [ message_arr[0] ](uint8_t tx_buffer){&MainWindow::on_TGetButton_clicked;});
QTimer::timeout has no parameter of type uint8_t, so why does your lambda have such a parameter? Also, you can't capture something like message_arr[0] in a lambda, you can capture message_arr.
-
@jsulm ah... i see what you mean, the code now compiles, i just didnt need any parameter emitted by QTimer::timetout.
Is it possible to have the captured parameters renamed to something else? i wanted to use local variables, but if i have to specify message_arr , as it was captured, it will not work out, i think...
maybe something like when we call a regular function with input parameters? -
@agmar said in invalid conversion error in signal connection:
it will not work out, i think...
it will even if message_arr is a local variable
-
@jsulm what in the case of global variables tho?
is it not better to make the functions more reusable by converting the function inputs into locals inside the function? since message_arr is global, having it inside the other functions will make the code less portable and changes will be have to be made everytime a parameter name is changed, maybe that is ok if there is no other option, but surely there should be -
@agmar said in invalid conversion error in signal connection:
is it not better to make the functions more reusable by converting the function inputs into locals inside the function?
If a parameter of a function can be a local variable then the question is: why was it a parameter at all? Local variables and function parameters are different concepts. So, if message_arr is "global" (global variables are bad design btw.) then why do you want to pass it to the slot/lambda as parameter?
But what I actually wanted to say is: you can also capture local variables in a lambda. -
@jsulm said in invalid conversion error in signal connection:
So, if message_arr is "global" (global variables are bad design btw.) then why do you want to pass it to the slot/lambda as parameter?
yes , that is what i mean aswell, i prefer to avoid global variables, but message_arr will require only a few of the bytes to be changed for every message, so it makes sense to have it as global, so every function can edit it slightly instead of redefining 10 bytes of data, since that in the end will have to be defined in every individual function i use if i dont make it global, so it seems logical to use a global for message_arr
That aside , i still want to use local variables inside every function, so for example , when on_TGoButton_Clicked is called, i want the message_arr to appear as tx_buffer because it will need minor modifications and it will be used as a parameter for functions inside on_TGoButton_Clicked , here's an example
void MainWindow::on_TGoButton_clicked(uint8_t tx_buffer)
{MainWindow::writeSerial(tx_buffer); lastPress = "TGo"; printOut(tx_buffer);
}
i would really prefer to have tx_buffer instead of punching in message_arr directly, which, like is said is a hassle if i want to change then name of message_arr for example...
at this point, i can assume there is no optionto specify the input parameter for as i do in MainWindow::writeSerial(tx_buffer);?
-
@agmar I don't know your design, so don't know why message_arr needs to be global. I doubt it really has to. It is really bad design if you're changing a global variable in many places in your app. You should rethink the design.