Results 1 to 20 of 24

Thread: Passing strings to SLOT function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

    I was wondering is the following codes would work,


    QObject::connect(pushButton,SIGNAL(clicked(),this, SLOT(sendmsg()));
    ...
    ...
    MainWindow::sendmsg()
    {
    strings associated with pushbutton...
    }
    ...
    ...
    MainWindow::socket()
    {
    .......
    .......
    send(...,sendmsg,...);
    .......
    .......
    }

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing strings to SLOT function

    Something like this.
    Slot definition somewhere :
    Qt Code:
    1. slots :
    2. void receiveMessage( QString message );
    To copy to clipboard, switch view to plain text mode 
    signal definition in MainWindow :
    Qt Code:
    1. signals :
    2. void sendMessage(QString message);
    To copy to clipboard, switch view to plain text mode 
    and method MainWindow::socket :
    Qt Code:
    1. MainWindow::socket()
    2. {
    3. .......
    4. .......
    5. emit sendMessage(sendmsg());
    6. .......
    7. .......
    8. }
    To copy to clipboard, switch view to plain text mode 
    Of course You must put somewhere this code :
    Qt Code:
    1. QObject::connect(address_of_MainWindow,SIGNAL(sendMessage(QString)),addres_of_message_receiver, SLOT(receiveMessage( QString message )));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing strings to SLOT function

    If it is a fixed string per button, check QSignalMapper

    Cheers,
    _

  4. #4
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing strings to SLOT function

    I probably should rephrase my question. Currently i have a socket thread(not slot), that executes at runtime when the app starts. the socket thread will wait in the background for a string to be passed to it from the pushbuttons, and when a pushbutton is pressed, example a string containing "abcd" will be pass to the socket thread's "send()" function and finally over to the server side, which is another device. Afterwhich, the socket will remain open until another button is pressed, and repeat the same procedure again.

    I don't think the method Lesiok posted can do this, since receiveMessage is another slot within the same app to receive the strings.

    Yes, it would be a fixed string per pushbutton, e.g pushbutton1-> "abcd", pushbutton2->
    "defg".

  5. #5
    Join Date
    Jan 2013
    Posts
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing strings to SLOT function

    Isn't what has been said already a solution?
    Using SIGNALS/SLOTS?

    Main Thread
    Qt Code:
    1. signals:
    2. void sendMessage(QString message);
    To copy to clipboard, switch view to plain text mode 

    SocketSender
    Qt Code:
    1. slots:
    2. void send(QString message);
    To copy to clipboard, switch view to plain text mode 

    And when you click a button, set it to

    Qt Code:
    1. emit sendMessage("ABCD");
    To copy to clipboard, switch view to plain text mode 

    That would then EMIT the SIGNAL sendMessage('ABCD') and your SLOT would connect and get the QString value and send it appropriately?

    After you create your socket thread, using the CONNECT to link the SIGNALS/SLOTS

    Qt Code:
    1. connect(this, SIGNAL(sendMessage(QString), socketThread, SLOT(send(QString)));
    To copy to clipboard, switch view to plain text mode 
    Last edited by zerokewl; 7th September 2013 at 12:49.

  6. #6
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing strings to SLOT function

    I'm not sure why I'm having an error saying that SIGNAL was not declared.

    Qt Code:
    1. mainwindow.cpp:14:78: error: macro "SIGNAL" passed 3 arguments, but takes just 1
    2. mainwindow.cpp: In constructor ‘MainWindow::MainWindow(QWidget*)’:
    3. mainwindow.cpp:14:16: error: ‘SIGNAL’ was not declared in this scope
    4. make: *** [mainwindow.o] Error 1
    To copy to clipboard, switch view to plain text mode 


    Here is my code

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_main.h"
    3. #include "socketthread.h"
    4. #include <QApplication>
    5.  
    6. using namespace std;
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. connect(this, SIGNAL(sendMessage(QString), SocketThread, SLOT(send(QString)));
    14. }
    15.  
    16. MainWindow::~MainWindow()
    17. {
    18. delete ui;
    19. }
    20.  
    21. void MainWindow::on_pushButton_clicked()
    22. {
    23. emit sendMessage("ABCD");
    24. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui
    7. {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow {
    12. Q_OBJECT
    13.  
    14. public:
    15. MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. signals:
    19. void sendMessage(QString message);
    20.  
    21. private slots:
    22. void on_pushButton_clicked();
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26.  
    27. };
    28.  
    29. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    socketthread.cpp
    Qt Code:
    1. void SocketThread::run()
    2. {
    3. .....
    4. .....
    5. string_in = send(); // not sure how to connect the Qstring value here
    6. .....
    7. .....
    8. }
    To copy to clipboard, switch view to plain text mode 

    socketthread.h
    Qt Code:
    1. #ifndef SOCKET_H
    2. #define SOCKET_H
    3. #include <QThread>
    4.  
    5. class SocketThread : public QThread
    6. {
    7. Q_OBJECT
    8.  
    9. public slots:
    10. void send(QString message);
    11.  
    12. private:
    13. void run();
    14. };
    15. #endif // SOCKET_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by ashboi; 7th September 2013 at 13:32.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing strings to SLOT function

    Your error indicates that you are using SIGNAL wrong and indeed there is a closing parentheses missing.
    And it is using the wrong name, the signal's name is sendMessage.

    so over all it should be
    Qt Code:
    1. SIGNAL(sendMessage(QString))
    To copy to clipboard, switch view to plain text mode 

    Or, as I said, even simpler using a QSIgnalMapper

    Qt Code:
    1. {
    2. ui->setupUi(this);
    3.  
    4. QSignalMapper *mapper = new QSignalMapper(this;
    5.  
    6. mapper->setMapping(ui->pushButton, "ABCD");
    7. connect(ui->pushButton, SIGNAL(clicked()), mapper, SLOT(map()));
    8.  
    9. connect(mapper, SIGNAL(mapped(QString)), SocketThread, SLOT(send(QString)));
    10. }
    To copy to clipboard, switch view to plain text mode 

    Assuming SocketThread is still owned by the main thread and has not been moved "into itself", you will also either require a Qt::QueuedConnection for the connect or mutex safe-guarding in the slot.

    All in all I would advise to first make it work without threads. QTcpSocket works event based for a reason!

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    ashboi (7th September 2013)

  9. #8
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing strings to SLOT function

    Ah, missed that ')'.

    Now I'm having another error at
    Qt Code:
    1. connect(this, SIGNAL(sendMessage(QString)), SocketThread, SLOT(send(QString)));
    To copy to clipboard, switch view to plain text mode 
    and I can't seem to figure out whats wrong with it.

    Qt Code:
    1. mainwindow.cpp: In constructor ‘MainWindow::MainWindow(QWidget*)’:
    2. mainwindow.cpp:15:58: error: expected primary-expression before ‘,’ token
    3. make: *** [mainwindow.o] Error 1
    To copy to clipboard, switch view to plain text mode 

    On another note, my socket client is using zmq as a transport layer, and it executes at runtime when the app's GUI starts, currently, the I have the socket using 'cout' to check for errors/feeback from the server, and wait for messages to send over to the server. Initially, I was hoping that I can do something like, when a button is clicked, the strings "abcd" would be passed to the socket, and printed in the terminal, just like if a user were to manually type in "abcd" into the terminal, to show its actually working.

  10. #9
    Join Date
    Jan 2013
    Posts
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing strings to SLOT function

    you forgot a ) here

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. connect(this, SIGNAL(sendMessage(QString), SocketThread, SLOT(send(QString)));
    7. }
    To copy to clipboard, switch view to plain text mode 

    it should be
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. connect(this, SIGNAL(sendMessage(QString)), SocketThread, SLOT(send(QString)));
    7. }
    To copy to clipboard, switch view to plain text mode 

    I wrote this as you Edited your last update fixing the missing bracket..

    Can you please post your Constructor with new code?
    Last edited by zerokewl; 7th September 2013 at 14:36.

  11. #10
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing strings to SLOT function

    Sure thing, here it is

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. connect(this, SIGNAL(sendMessage(QString)), SocketThread, SLOT(send(QString)));
    7. }
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Jan 2013
    Posts
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing strings to SLOT function

    Where is SocketThread instantiated from ?

    Should you have something like?

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. SocketThread *socketThread = new SocketThread();
    7. connect(this, SIGNAL(sendMessage(QString)), socketThread, SLOT(send(QString)));
    8. }
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to zerokewl for this useful post:

    ashboi (7th September 2013)

  14. #12
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing strings to SLOT function

    socketThread is instantiated from main.cpp, I guess it would be easier if it was instantiated in mainwindow.cpp instead. No more errors on this.

    But I have a question on how should I pass the string into my socket slot function? I tried
    Qt Code:
    1. string_in = send();
    To copy to clipboard, switch view to plain text mode 
    but results in an argument not being provided.

    Currently this is what I have

    socketthread.cpp
    Qt Code:
    1. void SocketThread::run()
    2. {
    3. ...connect....
    4. .................
    5. while(1){
    6. string string_in = ""; // I would like to pass the string into string_in
    7. ....
    8. .......
    9. }
    10. exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  15. #13
    Join Date
    Jan 2013
    Posts
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing strings to SLOT function

    The connect signal

    Qt Code:
    1. connect(this, SIGNAL(sendMessage(QString)), SocketThread, SLOT(send(QString)));
    To copy to clipboard, switch view to plain text mode 

    Sends the Emitted QString to the socketThread::send(QString message).
    Once you emit the signal and it is recieved from the socketThread::send(QString message) you could do this.

    Qt Code:
    1. class socketThread{
    2. private:
    3. QString message;
    4. }
    5.  
    6. void socketThread::send(QString message){
    7. this->message = message;
    8. }
    To copy to clipboard, switch view to plain text mode 


    And then inside your run

    Qt Code:
    1. void SocketThread::run()
    2. {
    3. ...connect....
    4. .................
    5. while(1){
    6. string string_in = message.toStdString() //convert QString to string
    7. ....
    8. .......
    9. }
    To copy to clipboard, switch view to plain text mode 

  16. #14
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing strings to SLOT function

    Quote Originally Posted by ashboi View Post
    Currently this is what I have

    socketthread.cpp
    Qt Code:
    1. void SocketThread::run()
    2. {
    3. ...connect....
    4. .................
    5. while(1){
    6. string string_in = ""; // I would like to pass the string into string_in
    7. ....
    8. .......
    9. }
    10. exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    With this run() method You never receive any signal because thread event loop is never started. What are You doing in this "never ending" loop ?

Similar Threads

  1. Passing argument to slot.
    By TCB13 in forum Newbie
    Replies: 2
    Last Post: 22nd February 2012, 19:52
  2. Passing data via signal/slot
    By gib in forum Qt Programming
    Replies: 4
    Last Post: 1st November 2010, 05:49
  3. Passing an integer to a slot
    By bizmopeen in forum Newbie
    Replies: 6
    Last Post: 30th October 2009, 09:51
  4. Passing QsqlRecords in a function
    By ambarish_singh in forum Qt Programming
    Replies: 7
    Last Post: 13th August 2009, 11:10
  5. passing arguments in SLOT
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2008, 21:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.