Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Passing strings to SLOT function

  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,536
    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,536
    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 ?

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

    Default Re: Passing strings to SLOT function

    I managed to be error free, but Lesiok is right, no signal is being received when pushbutton is clicked. The terminal just waits for an input, I'm trying to get it to work in a way that when a pushbutton is clicked, the terminal would show that a string is being input and sent.

    In that run function, I'm basically establishing a tcp socket using zeromq as a transport layer and sending/receiving message to and from the server (which is another device).

    From a plain script perspective, when the function executes, the sockets will be established, and wait for a user to cin a specific string and proceed to send that off over to the server, and then it waits again for another input, and repeats the same process over and over.

    I have had a brief look into QTcpSocket, but it seems that it would be a pain(correct me if I'm wrong) to get zmq to work with it, and I need zmq as a cross-platform client/server process to work, with inter-process (all of which I've gotten working).

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Passing strings to SLOT function

    To be honest your "socket thread" doesn't need to be a thread at all. If all the thread does is send a string over network then this can be done from the main thread, significantly simplifying your code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #17
    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

    Quote Originally Posted by Lesiok View Post
    With this run() method You never receive any signal because thread event loop is never started.
    Actually it should, unless ServerThread has been moveToThread() into itself.

    Quote Originally Posted by Lesiok View Post
    What are You doing in this "never ending" loop ?
    That is, of course, a good question.

    Since the suggestion not to use a thread seems to have been ignored, I have to assume it is because of the 0MQ library, i.e. it only works with blocking calls.

    All speculation until we see the actual code for ServerThread::run()

    Cheers,
    _

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

    Default Re: Passing strings to SLOT function

    Yes, that's correct, I'm using 0MQ for my sockets. There isn't any SERVER running on the app itself, the server script is running on another device.

    Here is a simplified version of my socket thread function;

    On serverthread.cpp
    void SocketThread::send(QString message)
    {
    this->message = message;
    }
    void SocketThread::run()
    {
    string addr1 = "tcp://localhost:8886";
    char *addr1_cstr1 = (char *) addr1.c_str();

    zmq::context_t context(1);
    zmq::socket_t sender(context, ZMQ_PUSH);
    sender.connect(addr1_cstr1);

    string addr2 = "tcp://localhost:8887";
    char *addr_cstr2 = (char *) addr2.c_str();

    zmq::socket_t reply(context, ZMQ_SUB);
    reply.connect(addr_cstr2);
    reply.setsockopt(ZMQ_SUBSCRIBE, "", 0);

    string addr3 = "tcp://localhost:8888";
    char *addr_cstr3 = (char *) addr3.c_str();

    while(1)
    {
    string string_in = message.toStdString();
    string to_send = "";

    cout << "Input here: ";
    getline(cin, string_in);
    if(string_in == "test")
    {
    to_send = "test";
    s_send(sender, (char *) to_send.c_str());
    receive_msgs(reply);
    to_send = "";
    }
    }
    exec();
    }
    on socketthread.h
    #ifndef SOCKET_H
    #define SOCKET_H
    #include <QThread>

    class SocketThread : public QThread
    {
    Q_OBJECT
    public slots:
    void send(QString message);

    private:
    void run();
    QString message;
    };
    #endif // SOCKET_H
    Last edited by ashboi; 9th September 2013 at 13:10.

  21. #19
    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

    In this case you need to make sure that
    1) the connect to send(QString) uses connection type Qt:irectConnection (the receiver thread does not run an event loop).
    2) use a QMutex member in SocketThread to protect both accesses to message
    Qt Code:
    1. void SocketThread::send(QString message)
    2. {
    3. QMutexLocker locker(&mutex); // assuming the mutex member is called mutex
    4. this->message = message;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. while(1)
    2. {
    3. mutex.lock();
    4. string string_in = message.toStdString();
    5. mutex.unlock();
    6. ...
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    Default Re: Passing strings to SLOT function

    I've tried the QMutex method, not sure what I might have done wrong, but for some reason I am still unable to receive the string when i click the pushbutton.
    Last edited by ashboi; 10th September 2013 at 13:39.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.