Hi!
I'm meaning to grab some data from a network connection, wrap the data into a QFrame and add those QFrames to my gui. The data comes slow, so it has to be a seperate process that doesn't block the gui. Also the user needs to have the possibility to stop the process in the middle. So what I did is this:
void GUI::on_startGrabButton_clicked()
{
// Start the grabber thread.
frameGrabber.start();
}
class FrameGrabber : QThread;
void GUI::on_startGrabButton_clicked()
{
// Start the grabber thread.
frameGrabber.start();
}
To copy to clipboard, switch view to plain text mode
And inside the frameGrabber thread:
void FrameGrabber::run()
{
int framesToGrab =1000;
while(framesToGrab >= 0)
{
MyFrame* f = NetworkConnection::grabFrame();
GUI->addFrame(f);
framesToGrab--;
msleep(10);
}
GUI->grabbingProcessFinished();
}
class MyFrame : QFrame;
void FrameGrabber::run()
{
int framesToGrab =1000;
while(framesToGrab >= 0)
{
MyFrame* f = NetworkConnection::grabFrame();
GUI->addFrame(f);
framesToGrab--;
msleep(10);
}
GUI->grabbingProcessFinished();
}
To copy to clipboard, switch view to plain text mode
The NetworkConnection class will instantiate a MyFrame, which is derived from QFrame. However, since the NetworkConnection doesn't know anything about the GUI, it cannot pass QWidget* parent as an argument to the constructor, so it can't instantiate a full blown QFrame yet. Only when MyFrame is added to the GUI with GUI->addFrame(), I try to set the parent with the setParent() method:
void GUI::addFrame(MyFrame* f)
{
f->setParent(ui.GrabbedFrameBufferArea);
}
void GUI::addFrame(MyFrame* f)
{
f->setParent(ui.GrabbedFrameBufferArea);
}
To copy to clipboard, switch view to plain text mode
This was the plan anyhow, but it doesn't work. I get a lot of this:
QPixmap: It is not safe to use pixmaps outside the GUI thread
QObject::setParent: Cannot set parent, new parent is in a different thread
I don't quite get why it complains about a different thread adding Widgets to the gui. But in any case, what would be the right solution?
Thanks
Cruz
Bookmarks