Results 1 to 3 of 3

Thread: Accessing ui object from thread

  1. #1
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Accessing ui object from thread

    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:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5.  
    6. namespace Ui
    7. {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. };
    22.  
    23. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindow)
    3. {
    4. ui->setupUi(this);
    5. MyThread* myT = new MyThread();
    6. myT->run();
    7. }
    To copy to clipboard, switch view to plain text mode 

    getFrameThread.cpp:
    Qt Code:
    1. void MyThread::run()
    2. {
    3. forever {
    4. //MainWindow *ui = MainWindow::ui;
    5. CvCapture* camera;
    6. camera = cvCreateCameraCapture(0);
    7. assert(camera);
    8. IplImage * cvimage=cvQueryFrame(camera);
    9. assert(cvimage);
    10. // IMG ELABORATION SKIPPED
    11. MainWindow::ui->imgLabel->setPixmap(CVUtils::getImage(cvimage)); // This is the row i want to fix
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    getFrameThread.h:
    Qt Code:
    1. #include "CVUtils.h"
    2. #include <QThread>
    3.  
    4. class MyThread : public QThread
    5. {
    6. Q_OBJECT
    7. public:
    8. MyThread();
    9.  
    10. public:
    11. void run();
    12. MainWindow* ui;
    13. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    506
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing ui object from thread

    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

  3. #3
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Accessing ui object from thread

    Quote Originally Posted by Ginsengelf View Post
    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

Similar Threads

  1. Thread Ownership Problem
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 00:18
  2. Accessing TextEdit from a thread.
    By dsmcc in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2008, 08:47
  3. Accessing data from a worker thread
    By steg90 in forum Qt Programming
    Replies: 20
    Last Post: 25th May 2007, 10:20
  4. Replies: 3
    Last Post: 16th May 2007, 11:07
  5. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

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.