How to me from a separate thread add a line in listWidget??
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread a93130. Receiver 'HSMClass' (of type 'HSM')
Printable View
How to me from a separate thread add a line in listWidget??
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread a93130. Receiver 'HSMClass' (of type 'HSM')
You could implement a slot for addItem() and call QMetaObject::invokeMethod() to call it from a thread.
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
QCoreApplication::sendEvent() is not thread-safe but QCoreApplication::postEvent() is. Be sure to read notes in docs.
.....
....
thx :o
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:)
... can help me
my code:
Code:
{ Q_OBJECT public: CProjectManager *m_pPrj; ~HSM(); private: Ui::HSMClass ui; Server server; private slots: void on_pushButton_clicked(); };
Code:
{ ui.setupUi(this); } HSM::~HSM() { //m_pPrj->FreeInst(); } void HSM::on_pushButton_clicked() { //some code } { ui.listWidget->addItem(cMsg); }
Code:
{ Q_OBJECT public: ~FortuneThread(); void run(); signals: private: int socketDescriptor; };
Code:
{ } FortuneThread::~FortuneThread() { } void FortuneThread::run() { Connection connection; if(!connection.setSocketDescriptor(socketDescriptor)) { emit error(connection.error()); return; } connect(&connection, SIGNAL(disconnected()), this, SLOT(quit())); exec(); }
Code:
{ Q_OBJECT public: private slots: void processReadyRead(); signals: };
Code:
{ } void Connection::processReadyRead() { //some code // // emit SendMSG("new_line"); }
Code:
{ Q_OBJECT public: signals: void newConnection(Connection *connection); protected: void incomingConnection(int socketDescriptor); };
Code:
{ return; } } void Server::incomingConnection(int socketDescriptor) { FortuneThread *thread = new FortuneThread(socketDescriptor,this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); }
But at connection of the client record all the same does not appear Where I was mistaken?
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
Yes, but this line:
is incorrect.
In incommingConnection, add:
Add a signal in the FortuneThread class:
and in FortuneThread::run(), add:
Regards
Thanks for your answers, I shall try tomorrow. I shall necessarily write that has turned out:)
OK. If you do things correctly, it will work.
Regards
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???
How do you know when a connection should be closed?
At me it is as follows written:
Code:
void Connection::processReadyRead() { QVector<QString> v_File; char *szData; QDomDocument doc; if( this->isReadable()) { qint64 qnSize = this->bytesAvailable(); if( qnSize > 0) { szData = new char [qnSize]; qint64 qnReadSize = this->read( szData, qnSize); szData[qnReadSize]=0; } } ParseXML *xml; xml = new ParseXML(szData); //xml.Get_Param("head", ""); //cHead = xml.get_param; xml->Get_Param("login", ""); cLogin = xml->get_param; xml->Get_Param("pass", ""); cPass = xml->get_param; xml->Get_Param("cmd", ""); cCmd = xml->get_param; xml->Get_Param("group", ""); cGroup = xml->get_param; xml->Get_Param("id_proc", ""); cId_proc = xml->get_param; xml->Get_Param("priority", ""); cPriotity = xml->get_param; xml->Get_Param("obj", "name"); cObj = xml->value; xml->Get_Param("file",""); v_File = xml->vFile; db_work database; switch(cCmd.toInt()) { case 1: //ADD TASK { for (int i = 0; i < v_File.size(); ++i) { database.AddTask(cCmd, cLogin, cPass, cGroup, cId_proc, cPriotity, v_File.at(i), cObj); } } break; case 2: //DELETE TASK { } break; case 3: //DELETE OBJECT { } break; case 4: //DELETE FILE { } break; case 5: //GET INFO { } break; } emit SendMSG(szData); delete(xml); database.CloseDB(); //QApplication::beep(); this->disconnectFromHost(); this->waitForDisconnected(); }
After that process needs to be finished.
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
hmmm...
For made comments on a line
Code:
..... ..... ..... this->disconnectFromHost(); // if(this->state() == QAbstractSocket::ConnectedState) - !!! this->waitForDisconnected(5000); }
and here that has received in messages
QAbstractSocket::waitForDisconnected() is not allowed in UnconnectedState