PDA

View Full Version : Accessing ui object from thread



khelsys
7th October 2009, 11:04
Hi all,
I'm really new of QT programming and I need to acess my ui pointer from a thread to refresh an image that's displayed in my window.
How can i do this? Many thanks to everyone who will help me :)
Gian

I post some code to explain it better:

MainWindow.h:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


MainWindow.cpp:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
MyThread* myT = new MyThread();
myT->run();
}


getFrameThread.cpp:


void MyThread::run()
{
forever {
//MainWindow *ui = MainWindow::ui;
CvCapture* camera;
camera = cvCreateCameraCapture(0);
assert(camera);
IplImage * cvimage=cvQueryFrame(camera);
assert(cvimage);
// IMG ELABORATION SKIPPED
MainWindow::ui->imgLabel->setPixmap(CVUtils::getImage(cvimage)); // This is the row i want to fix
}
}


getFrameThread.h:


#include "CVUtils.h"
#include <QThread>

class MyThread : public QThread
{
Q_OBJECT
public:
MyThread();

public:
void run();
MainWindow* ui;
};

Ginsengelf
7th October 2009, 11:39
Hi, you can't manipulate a widget from another thread than the main thread where the widget was created.

To solve your problem, create a slot in your main window to update the label and emit a signal instead of the MainWindow::ui->imgLabel->setPixmap(...) call.

Ginsengelf

khelsys
7th October 2009, 12:05
Hi, you can't manipulate a widget from another thread than the main thread where the widget was created.

To solve your problem, create a slot in your main window to update the label and emit a signal instead of the MainWindow::ui->imgLabel->setPixmap(...) call.

Ginsengelf
It's quite difficult for me.. Can you quickly explain how to do that in terms of places where to put the code (and what kind of code) ?
Thank you so much

Gian