Results 1 to 20 of 27

Thread: QLabel QPixmap change

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2025
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QLabel QPixmap change

    hello, sorry, I'm French so I'm talking with the translator, I want to change the image of a label based on a bool which is elsewhere in my program.
    how can I do it???

    THANKS

    mainwindow.cpp
    Qt Code:
    1. m_mainwindowThread.m_label = new QLabel(m_window);
    2. m_mainwindowThread.afficheLabelArret();
    To copy to clipboard, switch view to plain text mode 

    thread.h
    Qt Code:
    1. QLabel *m_label;
    2. void afficheLabelArret();
    To copy to clipboard, switch view to plain text mode 

    thread.cpp
    Qt Code:
    1. void Thread::afficheLabelArret()
    2. {
    3. if (m_threadGainable.labelModeFroid == true) {
    4. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/froid.jpg"));
    5. else if (m_threadGainable.labelModeChauffage == true) {
    6. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/chauffage.jpg"));
    7. } else {
    8. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/abigael.jpg"));
    9. }
    10. }
    11.  
    12. void Thread::run()
    13. {
    14. while (1) {
    15. afficheLabelArret();
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    I have segfault randomly

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,304
    Thanks
    313
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabel QPixmap change

    You cannot directly change something in the GUI thread from a different thread. The reason you are getting a segfault is probably because the GUI thread and your "Thread" are not synchronized.

    The correct way to do it is to emit a signal from your Thread that is connected to a slot in MainWindow.

    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. // ...
    4. private slots:
    5. void onChangeLabel( int mode );
    6.  
    7. private:
    8. QLabel * m_label;
    9. };
    10.  
    11. class Thread : public QThread
    12. {
    13. // ...
    14. signals:
    15. void changeLabel(int mode );
    16. };
    17.  
    18. MainWindow::MainWindow( QWidget * parent )
    19. : Qwidget( parent )
    20. {
    21. // ...
    22. connect( m_mainwindowThread, &Thread::changeLabel, this, &MainWindow::onChangeLabel );
    23. }
    24.  
    25. void Thread::afficheLabelArret()
    26. {
    27. if (m_threadGainable.labelModeFroid == true) {
    28. emit changeLabel( 0 );
    29. else if (m_threadGainable.labelModeChauffage == true) {
    30. emit changeLabel( 1 );
    31. } else {
    32. emit changeLabel( 2 );
    33. }
    34.  
    35. void MainWindow::onChangeLabel( int mode )
    36. {
    37. switch( mode )
    38. {
    39. case 0:
    40. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/froid.jpg"));
    41. break;
    42.  
    43. case 1:
    44. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/chauffage.jpg"));
    45. break;
    46.  
    47. default:
    48. m_label ->setPixmap(QPixmap("/home/ludo/Qt/test2/build/images/abigael.jpg"));
    49. break;
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    The signal / slot mechanism will make sure that the inter-thread call is synchronized correctly.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2025
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLabel QPixmap change

    ok I'm going to try this tomorrow, do you have a coding organization for:
    I want to manage a heating system with an rpi (raspberry) in c++! and use a graphical interface depending on the states of the GPIOs. go through temperatures.

    if this speaks to you. Thank you in advance for giving me time to this project.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,304
    Thanks
    313
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabel QPixmap change

    I want to manage a heating system with an rpi (raspberry) in c++! and use a graphical interface depending on the states of the GPIOs. go through temperature
    I have not made such a project, but if you Google for "Raspberry Pi thermostat" you might find some good information.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QLabel does not repaint QPixmap
    By __Emanuel in forum Newbie
    Replies: 7
    Last Post: 11th February 2017, 16:18
  2. Replies: 3
    Last Post: 10th April 2011, 16:55
  3. DrawLine over QPixmap within Qlabel
    By Qt Coder in forum Qt Programming
    Replies: 8
    Last Post: 26th March 2009, 13:21
  4. Replies: 2
    Last Post: 20th January 2009, 08:13
  5. Replies: 1
    Last Post: 2nd August 2008, 16:46

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
  •  
Qt is a trademark of The Qt Company.