Results 1 to 9 of 9

Thread: Image Not displaying in Qlabel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2016
    Posts
    6
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Question Image Not displaying in Qlabel

    Hi guys..I am trying to display a series of image in qlabel..i dono where i am going wrong. when i click the qpushbutton it displays only the last image rather displaying from beginning. Below is my code

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. QLabel *FirstLabel = NULL;
    3. QLabel *SecondLabel = NULL;
    4. QLabel *ThirdLabel = NULL;
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8.  
    9. FirstLabel = new QLabel(this);
    10. FirstLabel->move(0,240);
    11. FirstLabel->resize(this->x()+400,this->y()+240);
    12. FirstLabel->show();
    13.  
    14. QPushButton *train_button = new QPushButton(this);
    15. train_button->setText(tr("something"));
    16. train_button->move(200,200);
    17. train_button->show();
    18.  
    19. connect(train_button, SIGNAL(clicked()),
    20. this, SLOT (PlayImage()));
    21. }
    22.  
    23. void MainWindow::PlayImage()
    24. {
    25. char* picture[5] = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};
    26. for(int i = 0;i<=4 ; i++){
    27. QPixmap pixmapObject(picture[i]);
    28. FirstLabel->setPixmap(pixmapObject);
    29. update();
    30. }
    31. }
    32.  
    33. MainWindow::~MainWindow()
    34. {
    35. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QLabel>
    6. #include <string>
    7. #include <QPushButton>
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MainWindow(QWidget *parent = 0);
    15. ~MainWindow();
    16. void updateinfo();
    17. void Play();
    18.  
    19. public slots:
    20. void PlayImage();
    21. };
    22.  
    23. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.resize(800,480);
    9. w.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 26th July 2016 at 10:23. Reason: missing [code] tags

  2. #2
    Join Date
    Jul 2016
    Posts
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Image Not displaying in Qlabel

    put in your mainwindow.h:
    Qt Code:
    1. #include <QPushButton> //delete this line
    2. class QLabel;
    To copy to clipboard, switch view to plain text mode 

    then declare private section:
    Qt Code:
    1. private:
    2. QLabel *FirstLabel;
    3. QLabel *SecondLabel;
    4. QLabel *ThirdLabel;
    To copy to clipboard, switch view to plain text mode 

    in mainwidow.cpp
    Qt Code:
    1. FirstLabel = new QLabel(this);
    2. FirstLabel->move(0,240);
    3. FirstLabel->resize(this->x()+400,this->y()+240);
    4. FirstLabel->show();
    5.  
    6. train_button = new QPushButton(this);
    7. train_button->setText(tr("something"));
    8. train_button->move(200,200);
    9. train_button->show();
    10.  
    11. connect(train_button, SIGNAL(clicked()),
    12. this, SLOT (PlayImage()));
    13.  
    14.  
    15. void MainWindow::PlayImage()
    16. {
    17. //char* picture[5] = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};
    18. QStringList picture = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};
    19. for(int i = 0;i<=picture.count() ; i++){
    20. QPixmap pixmapObject(picture[i]);
    21. FirstLabel->setPixmap(pixmapObject);
    22. update(); //What is it?
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

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

    ranjithrajrrr (27th July 2016)

  4. #3
    Join Date
    Jun 2016
    Posts
    6
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Image Not displaying in Qlabel

    Thank you for your reply. I read that update function in qt refreshed the label every time it iterates.By the way i found what was the mistake i was doing. I need to call repaint function( FirstLabel->repaint ) instead of update function. with that change it works fine.

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

    Default Re: Image Not displaying in Qlabel

    Quote Originally Posted by ranjithrajrrr View Post
    Thank you for your reply. I read that update function in qt refreshed the label every time it iterates
    Your update() call is actually for the main window itself, the label will call its own update() method in setPixmap() anyway.

    Quote Originally Posted by ranjithrajrrr View Post
    By the way i found what was the mistake i was doing. I need to call repaint function( FirstLabel->repaint ) instead of update function. with that change it works fine.
    It is still wrong, but of course your choice.

    Cheers,
    _

  6. #5
    Join Date
    Jun 2016
    Posts
    6
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Image Not displaying in Qlabel

    Thanks for ur reply anda_skoa. Please explain me why repaint is a bad idea.. it does the same thing as update() fucntion does r8t?

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Image Not displaying in Qlabel

    update() schedules an update of a widget, multiple calls to update within one iteration of the event loop will only cause one update of the widget instead of unnecessarily for every update.

    repaint forces a redraw that might not even be seen because it is immediately followed by another redraw or paint-for-update.

    Your code's problem is that you loop through images without ever allowing any image to be shown.

    I seriously wonder how even repaint would help here since the repaints for each image are immediately after each other.
    The user will only see the last image unless each drawing is so slow that there is a tiny chance of glimpsing one of the others in between.

    Actual image sequence displaying usually doesn't rely on slowness, it specifies in which interval images are shown.
    Then triggers a new image to be shown whenever that happens.

    Cheers,
    _

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

    ranjithrajrrr (28th July 2016)

  9. #7
    Join Date
    Jun 2016
    Posts
    6
    Thanks
    4
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Image Not displaying in Qlabel

    understood anda.. Thanks for your help..

  10. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Image Not displaying in Qlabel

    Quote Originally Posted by ranjithrajrrr View Post
    Hi guys..I am trying to display a series of image in qlabel..i dono where i am going wrong. when i click the qpushbutton it displays only the last image rather displaying from beginning.
    You have a close loop in which you "display" the images.

    So what your code does is:
    - it loops through your array of image file names
    - each iteration loads the image, sets it on the label and ask for update
    - when the loop ends the event loop processed the update requests and the label displays the image that is now set

    What you probably want to do is to use a QTimer or QTimeLine to advance through the array.
    One image at a time, giving the event loop the chance to actually process the label's update request and the user to actually see the image.

    Cheers,
    _

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

    ranjithrajrrr (27th July 2016)

  12. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Image Not displaying in Qlabel

    This:
    Qt Code:
    1. char* picture[5] = {"D://1.png","D://2.jpg","D://3.jpg","D://4.jpg","D://5.jpg"};
    To copy to clipboard, switch view to plain text mode 
    should probably be:
    Qt Code:
    1. char* picture[5] = {"D:/1.png","D:/2.jpg","D:/3.jpg","D:/4.jpg","D:/5.jpg"};
    To copy to clipboard, switch view to plain text mode 
    to be sure the double forward-slashes are not causing files to not be found.

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

    ranjithrajrrr (27th July 2016)

Similar Threads

  1. geting QLabel text ontop of other QLabel displaying image
    By krystosan in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2013, 17:35
  2. Qlabel not displaying images other than development machine
    By arunkumaraymuo1 in forum Qt Programming
    Replies: 3
    Last Post: 23rd August 2012, 14:52
  3. Problem with displaying image using Qlabel
    By deck99 in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2011, 00:23
  4. Displaying an image
    By seltra in forum Newbie
    Replies: 2
    Last Post: 3rd October 2010, 19:30
  5. Replies: 6
    Last Post: 21st September 2009, 10:55

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.