Results 1 to 4 of 4

Thread: refresh a QLabel and QImage

  1. #1
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default refresh a QLabel and QImage

    Hello,
    i have a serious problem, when i choose an image to display in a Qwidget (fentroi), it is displayed but the problem when i choose another it can't refresh and it can't display the new image, ,and im working on binarisation and the threshold didn't refresh too i assume that because the QImage that i load is the cause because it dosen't refresh too.

    here my code
    fenetrePrincipale.h (MainWindow)
    Qt Code:
    1. #ifndef FENETREPRINCIPALE_H
    2. #define FENETREPRINCIPALE_H
    3. #include <QtWidgets>
    4. #include "traitement.h"
    5. class FenetrePrincipale : public QMainWindow
    6. {
    7. Q_OBJECT
    8. public :
    9. FenetrePrincipale();
    10. public slots :
    11. void ouvrir();
    12. void traiter();
    13. void traiterOtsu();
    14. private:
    15. QString nomFichier;
    16. QWidget fentroi;
    17. QWidget fenSec;
    18.  
    19.  
    20.  
    21. };
    22.  
    23. #endif // FENETREPRINCIPALE_H
    To copy to clipboard, switch view to plain text mode 

    fenetrePricipale.cpp
    Qt Code:
    1. #include "traitement.h"
    2. #include "otsu.h"
    3. #include "fenetreprincipale.h"
    4.  
    5. FenetrePrincipale::FenetrePrincipale(): QMainWindow()
    6. {
    7.  
    8.  
    9. QMenu *menuFichier = menuBar()->addMenu("Fichier");
    10. QAction *actionOuvrir = new QAction("Ouvrir", this);
    11. QAction *actionQuitter = new QAction("Quitter", this);
    12. menuFichier->addAction(actionOuvrir);
    13. menuFichier->addAction(actionQuitter);
    14.  
    15. QMenu *menuTraitement = menuBar()->addMenu("Traitement");
    16. QAction *actionBinarisation = new QAction("Binarisation", this);
    17. menuTraitement->addAction(actionBinarisation);
    18.  
    19. QObject::connect(actionOuvrir, SIGNAL(triggered(bool)), this, SLOT(ouvrir()));
    20. QObject::connect(actionQuitter, SIGNAL(triggered(bool)), this, SLOT(close()));
    21.  
    22. QObject::connect(actionBinarisation, SIGNAL(triggered(bool)), this, SLOT(traiterOtsu()));
    23. QObject::connect(this, SIGNAL(changer()), this, SLOT(afficher()));
    24. }
    25. void FenetrePrincipale::ouvrir()
    26. {
    27. nomFichier = QFileDialog::getOpenFileName(this, tr("Open File"),"C:/Users/amine/Downloads");
    28.  
    29. QLabel *label =new QLabel(&fentroi);
    30. label->setGeometry(100,100,400,400);
    31. QImage image1(nomFichier);
    32. QPixmap pixmap;
    33. pixmap.convertFromImage(image1);
    34. label->setPixmap(pixmap);
    35. fentroi.show(); // fentroi (fenetre qui affiche l'image) attribut de fenetre principale
    36. label->update();
    37.  
    38. }
    39.  
    40. void FenetrePrincipale::traiterOtsu()
    41. {
    42. OtsuAlgo traitement(nomFichier);
    43. traitement.binarisation();
    44. QImage *im = new QImage(traitement.getImage());
    45. QLabel *label = new QLabel(&fenSec);
    46. label->setGeometry(100,100, 400, 400);
    47. label->setPixmap(QPixmap::fromImage(*im));
    48. QLCDNumber *lcd = new QLCDNumber(&fenSec);
    49. lcd->move(70,70);
    50. lcd->setSegmentStyle(QLCDNumber::Flat);
    51. lcd->display(traitement.getSeuil());
    52. fenSec.show();
    53. }
    To copy to clipboard, switch view to plain text mode 
    please if someone can help me and thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,543
    Thanks
    13
    Thanked 1,577 Times in 1,506 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: refresh a QLabel and QImage

    Your code keeps creating a new label every time it want to update the image. You should be calling setPixmap() on the same QLabel every time.

  3. The following user says thank you to ChrisW67 for this useful post:

    rafik (24th January 2016)

  4. #3
    Join Date
    Jan 2016
    Posts
    22
    Thanks
    13

    Default Re: refresh a QLabel and QImage

    Thanks a lot Mr ChrisW67

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: refresh a QLabel and QImage

    And you don't want to leak the image by allocating it on the heap like you do right now in line 44.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    rafik (25th January 2016)

Similar Threads

  1. Can't refresh QLabel's Pixmap properly
    By deltamac in forum Newbie
    Replies: 3
    Last Post: 7th June 2015, 12:28
  2. refresh QLabel text
    By dragonprog in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2015, 06:57
  3. refresh image in a Qlabel
    By prkhr4u in forum Newbie
    Replies: 12
    Last Post: 22nd October 2013, 07:49
  4. How do I set an QImage on a QLabel?
    By Maluko_Da_Tola in forum Newbie
    Replies: 4
    Last Post: 3rd August 2010, 06:03
  5. display QImage on QLabel
    By akasi in forum Qt Programming
    Replies: 2
    Last Post: 23rd September 2008, 22:48

Tags for this Thread

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.