Passing strings to SLOT function
-
Currently, I've written up an app that executes a client socket Slot at runtime that is used to sending data to the server. I'm wondering is it possible to pass a text string when a pushbutton is clicked, to a variable in the slot function? Example, when a pushbutton is clicked, a string containing "abcd" is passed to the message variable in the socket function, and the socket will send "abcd" over to the server.
-
Hi,
For that you would create a slot to receive the clicked signal from the button then call your function requiring the string.
If you are using Qt 5 you could also use a lambda function.
Hope it helps
-
I'm guessing something like this could do what I want?
@
QObject::connect(pushButton,SIGNAL(clicked(),this,SLOT(sendmsg()));
@
@
MainWindow::sendmsg()
{
strings associated with pushbutton...
}
@
@
MainWindow::socket()
{
.......
.......
send(...,sendmsg,...);
.......
.......
}
@
I'm not sure if what I've done is correct. -
Yes, that looks right. sendmsfg should be under public slots.
-
If you have several buttons that will call this slot you could take a look at "QSignalMapper":http://qt-project.org/doc/qt-4.8/qsignalmapper.html