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

Thread: Thread+send event

  1. #1
    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 Thread+send event

    Last edited by Fastman; 25th July 2007 at 10:28.

  2. #2
    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.

  3. #3
    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

  4. #4
    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

  5. #5
    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

  6. #6
    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

  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

    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

  8. #8
    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?

  9. #9
    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

  10. #10
    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 

  11. #11
    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

  12. #12
    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

  13. #13
    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

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

    Fastman (27th July 2007)

  15. #14
    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

  16. #15
    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
    OOOO !!!! Realy Work !!!!!!
    Marcel = Guru
    Not really .
    I have about 4000 posts to go until I become a Guru.

    Regards

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

    I still have a question. The problem in that that after will fulfil Connection:: processReadyRead () process does not come to the end, and all processes remain in memory! How to make that after the termination of processing of connection of the client process it was killed???

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

    How do you know when a connection should be closed?

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

    At me it is as follows written:
    Qt Code:
    1. void Connection::processReadyRead()
    2. {
    3. QVector<QString> v_File;
    4. char *szData;
    5. QString cHead = "";
    6. QString cCmd = "";
    7. QString cLogin = "";
    8. QString cPass = "";
    9. QString cGroup = "";
    10. QString cId_proc = "";
    11. QString cFile = "";
    12. QString cObj = "";
    13. QString cPriotity = "";
    14.  
    15.  
    16.  
    17. if( this->isReadable())
    18. {
    19. qint64 qnSize = this->bytesAvailable();
    20. if( qnSize > 0)
    21. {
    22. szData = new char [qnSize];
    23. qint64 qnReadSize = this->read( szData, qnSize);
    24. szData[qnReadSize]=0;
    25. }
    26. }
    27.  
    28.  
    29. ParseXML *xml;
    30. xml = new ParseXML(szData);
    31. //xml.Get_Param("head", "");
    32. //cHead = xml.get_param;
    33.  
    34. xml->Get_Param("login", "");
    35. cLogin = xml->get_param;
    36.  
    37. xml->Get_Param("pass", "");
    38. cPass = xml->get_param;
    39.  
    40. xml->Get_Param("cmd", "");
    41. cCmd = xml->get_param;
    42.  
    43. xml->Get_Param("group", "");
    44. cGroup = xml->get_param;
    45.  
    46. xml->Get_Param("id_proc", "");
    47. cId_proc = xml->get_param;
    48.  
    49. xml->Get_Param("priority", "");
    50. cPriotity = xml->get_param;
    51.  
    52. xml->Get_Param("obj", "name");
    53. cObj = xml->value;
    54.  
    55. xml->Get_Param("file","");
    56. v_File = xml->vFile;
    57.  
    58. db_work database;
    59.  
    60. switch(cCmd.toInt())
    61. {
    62. case 1: //ADD TASK
    63. {
    64. for (int i = 0; i < v_File.size(); ++i)
    65. {
    66. database.AddTask(cCmd, cLogin, cPass, cGroup,
    67. cId_proc, cPriotity, v_File.at(i), cObj);
    68. }
    69.  
    70. }
    71. break;
    72.  
    73. case 2: //DELETE TASK
    74. {
    75.  
    76. }
    77. break;
    78.  
    79. case 3: //DELETE OBJECT
    80. {
    81.  
    82. }
    83. break;
    84.  
    85. case 4: //DELETE FILE
    86. {
    87.  
    88. }
    89. break;
    90.  
    91. case 5: //GET INFO
    92. {
    93.  
    94. }
    95. break;
    96.  
    97. }
    98.  
    99. emit SendMSG(szData);
    100.  
    101. delete(xml);
    102. database.CloseDB();
    103. //QApplication::beep();
    104.  
    105. this->disconnectFromHost();
    106. if(this->state() == QAbstractSocket::ConnectedState)
    107. this->waitForDisconnected();
    108. }
    To copy to clipboard, switch view to plain text mode 

    After that process needs to be finished.

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

    It means that you don't get the disconnected() signal. If you would, then the quit slot for the thread will get called, causing exec() to exit.

    The default delay for waitForDisconnected is 30 seconds.
    Try giving it a smaller delay, like 5 seconds ( pass 5000 to waitForDisconnected ).

    If you still don't get the disconnected signal, then it means that there is still data pending to be read. But giving it an explicit timeout, it should force a disconnected() signal.

    Regards

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

    hmmm...

    For made comments on a line
    Qt Code:
    1. .....
    2. .....
    3. .....
    4. this->disconnectFromHost();
    5. // if(this->state() == QAbstractSocket::ConnectedState) - !!!
    6. this->waitForDisconnected(5000);
    7. }
    To copy to clipboard, switch view to plain text mode 

    and here that has received in messages

    QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  2. Main Thread Event loop
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 13:10
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 01:49
  4. Replies: 11
    Last Post: 7th July 2006, 14: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, 22: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.