Results 1 to 20 of 21

Thread: Thread+send event

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Location
    Augsburg, Germany
    Posts
    75
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread+send event

    You could implement a slot for addItem() and call QMetaObject::invokeMethod() to call it from a thread.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread+send event

    Emit a signal from the thread which can contain as parameters data for the item.
    Connect this signal to a slot in the GUI thread which will create the item and update the widget.

    This is how it must be done.

    Another solution is to post a custom event, but is basically the same thing as emitting a signal, only more code to write.

    The slot in the GUI will get called asynchronously.

    Regards

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Thread+send event

    QCoreApplication::sendEvent() is not thread-safe but QCoreApplication::postEvent() is. Be sure to read notes in docs.
    J-P Nurmi

  4. #4
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread+send event

    .....
    ....
    thx

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread+send event

    Quote Originally Posted by Fastman View Post
    .....
    ....
    thx
    What does that mean?
    Aren't you satisfied with the explanations?
    Then ask some more?
    But first, really, you should search the forum. There have been numerously other similar posts.

    Regards

  6. #6
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread+send event

    No, many thanks, you have very much helped! I have just started to understand with QT, therefore at me it is a lot of questions

  7. #7
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread+send event

    ... can help me

    my code:

    Qt Code:
    1. class HSM : public QDialog
    2. {
    3. Q_OBJECT
    4. public:
    5. CProjectManager *m_pPrj;
    6. HSM(QWidget *parent = 0, Qt::WFlags flags = 0);
    7. ~HSM();
    8. private:
    9. Ui::HSMClass ui;
    10. Server server;
    11. private slots:
    12. void on_pushButton_clicked();
    13. void SetLine(QString cMsg);
    14. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. HSM::HSM(QWidget *parent, Qt::WFlags flags)
    2. : QDialog(parent, flags)
    3. {
    4.  
    5. ui.setupUi(this);
    6.  
    7. QObject::connect(this, SIGNAL(SendMSG(QString)),
    8. this, SLOT(SetLine(QString cMsg)));// ???????
    9. }
    10.  
    11. HSM::~HSM()
    12. {
    13. //m_pPrj->FreeInst();
    14. }
    15.  
    16. void HSM::on_pushButton_clicked()
    17. {
    18. //some code
    19. }
    20.  
    21. void HSM::SetLine(QString cMsg)
    22. {
    23. ui.listWidget->addItem(cMsg);
    24. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class FortuneThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. FortuneThread(int socketDescriptor,QObject *parent);
    6. ~FortuneThread();
    7. void run();
    8. signals:
    9. void error(QTcpSocket::SocketError socketError);
    10. private:
    11. int socketDescriptor;
    12. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. FortuneThread::FortuneThread(int socketDescriptor,QObject *parent)
    2. : QThread(parent), socketDescriptor(socketDescriptor)
    3. {
    4.  
    5. }
    6.  
    7. FortuneThread::~FortuneThread()
    8. {
    9.  
    10. }
    11. void FortuneThread::run()
    12. {
    13. Connection connection;
    14. if(!connection.setSocketDescriptor(socketDescriptor))
    15. {
    16. emit error(connection.error());
    17. return;
    18. }
    19. connect(&connection, SIGNAL(disconnected()), this, SLOT(quit()));
    20. exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Connection : public QTcpSocket
    2. {
    3. Q_OBJECT
    4. public:
    5. Connection(QObject *parent = 0);
    6. private slots:
    7. void processReadyRead();
    8. signals:
    9. void SendMSG(QString cMsg);
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Connection::Connection(QObject *parent)
    2. : QTcpSocket(parent)
    3. {
    4. QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
    5. }
    6.  
    7. void Connection::processReadyRead()
    8. {
    9.  
    10. //some code
    11. //
    12. //
    13. emit SendMSG("new_line");
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Server : public QTcpServer
    2. {
    3. Q_OBJECT
    4. public:
    5. Server(QObject *parent = 0);
    6. signals:
    7. void newConnection(Connection *connection);
    8. protected:
    9. void incomingConnection(int socketDescriptor);
    10. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Server::Server(QObject *parent):QTcpServer(parent)
    2. {
    3. if (!listen(QHostAddress::Any,1718)) {
    4. return;
    5. }
    6. }
    7.  
    8. void Server::incomingConnection(int socketDescriptor)
    9. {
    10.  
    11. FortuneThread *thread = new FortuneThread(socketDescriptor,this);
    12. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    13. thread->start();
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    But at connection of the client record all the same does not appear Where I was mistaken?

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread+send event

    In the constructor of HSM.
    There is no signal HSM::SendMSG(). The signal is in Connection. If you look at the debug output you will see a warning.

    You could chain the signals. Connect SendMsg to a a signal in the FortuneThread. This signal should be connected in the GUI, to the slot SetLine.

    This is because the GUI thread does not have access to Connection.

    Where do you instantiate and start the thread?

    Regards

  9. #9
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread+send event

    Quote Originally Posted by marcel View Post

    Where do you instantiate and start the thread?
    Qt Code:
    1. void Server::incomingConnection(int socketDescriptor)
    2. {
    3. FortuneThread *thread = new FortuneThread(socketDescriptor,this);
    4. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    5. thread->start();
    6. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread+send event

    Yes, but this line:
    Qt Code:
    1. QObject::connect(this, SIGNAL(SendMSG(QString)),
    2. this, SLOT(SetLine(QString cMsg)));// ???????
    To copy to clipboard, switch view to plain text mode 
    is incorrect.

    In incommingConnection, add:
    Qt Code:
    1. connect(thread, SIGNAL(SendMSG(QString)), this, SLOT(SetLine(QString)));
    To copy to clipboard, switch view to plain text mode 
    Add a signal in the FortuneThread class:
    Qt Code:
    1. void SendMSG(QString);
    To copy to clipboard, switch view to plain text mode 
    and in FortuneThread::run(), add:
    Qt Code:
    1. connect(&connection, SIGNAL(SendMSG(QString)), this, [B]SIGNAL[/B](SendMSG(QString)));
    To copy to clipboard, switch view to plain text mode 
    Regards

  11. #11
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread+send event

    Thanks for your answers, I shall try tomorrow. I shall necessarily write that has turned out

  12. #12
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread+send event

    OK. If you do things correctly, it will work.

    Regards

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

    Fastman (27th July 2007)

  14. #13
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread+send event

    Quote Originally Posted by marcel View Post
    OK. If you do things correctly, it will work.

    Regards

    OOOO !!!! Realy Work !!!!!!
    Marcel = Guru

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. Main Thread Event loop
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 12:10
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  4. Replies: 11
    Last Post: 7th July 2006, 13:09
  5. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55

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.