[SOLVED] QLabel output freezes when outputting data from separate process
-
Hello,
I have two processes (One from Qt and one external) which I am using QSharedMemory as my IPC for. In my Qt GUI I have a separate thread that emits a signal to update my QLabel outputs every 100 ms. For some reason when I run the application my GUI's QLabel outputs randomly run then freeze, then come back, then freeze again, etc. Sometimes if I move the window around it unfreezes from a frozen state with the current value. I am thinking it might have something to do with thread priority because it seems that the GUI just gives up the processor for something else to run and doesn't update anything. Another thought I had was something with locking/unlocking the shared memory, but I am not sure how that would effect the output of the GUI freezing. I am more inclined to think it is my first assumption. I was wondering what anyone's thoughts were and if I may be wrong with what the problem is. Any feedback is greatly appreciated.
Thanks
-
Hi,
Without any code, it's difficult to diagnose anything. You should at least show the QSharedMemory related code.
-
[quote author="SGaist" date="1370032085"]Hi,
Without any code, it's difficult to diagnose anything. You should at least show the QSharedMemory related code.[/quote]
Below is my code:
@void BasicGui::createSharedMemory()
{
QString SHMEMKEY;
int size = sizeof(double);if(sharedMemory.isAttached()) {sharedMemory.detach();} sharedMemory.setKey("SharedMemory"); if(!sharedMemory.create(size, QSharedMemory::ReadWrite)) { qDebug() << "Unable to create shared memory segment!"; qDebug() << "Error code: " << sharedMemory.errorString(); } qDebug() << "Shared memory was created with a size of " << sharedMemory.size(); SHMEMKEY = sharedMemory.nativeKey(); qDebug() << "Native shared memory key: " << SHMEMKEY; sharedMemory.attach(); pSharedMemory = sharedMemory.data();
}@
So after this I use the following line in another part of code to read the data (gui_data) in the UI:
@gui_data = (gui_mot_data_t*)pSharedMemory;@
I have the GUI updating dats from the shared memory in another thread as follows:
@void GuiUpdate::run()
{
while(1)
{
updateGuiSignal();
Sleep(100);
}
}@I use a signal and slot for this:
@QObject::connect(&guiupdate, SIGNAL(updateGuiSignal()), &basicgui, SLOT(updateGuiSlot()), Qt::DirectConnection);
@So, after running my code I find that some of the UI outputs (lets say positions being continuously updated) just stop updating themselves and randomly come back to life or don't ever come back at all. What can be causing this?
-
You are not supposed to update GUI objects (QLabel in your case) from another thread than the main application thread (more info on "this":http://www.qtforum.org/article/26801/qt4-threads-and-widgets.html thread).
So the solution would be to use signals and slots but
[quote author="rizoritis" date="1370348224"]
I use a signal and slot for this:@
QObject::connect(&guiupdate, SIGNAL(updateGuiSignal()), &basicgui, SLOT(updateGuiSlot()), Qt::DirectConnection);
@[/quote]
I think the problem is using the Qt::DirectConnection. Any reason why it should be direct, refer to the documentation about the "Qt::ConnectionType":http://qt-project.org/doc/qt-4.8/qt.html#ConnectionType-enum options, my guess is that Qt::AutoConnection is the better option.