Results 1 to 14 of 14

Thread: QLabel size, for image display

  1. #1
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QLabel size, for image display

    Hi there,
    the small imageViewer i write has two big problems: i can't find out the size of the label, so i cant resize it. And i don't know wo to find out what the new size is when the application gets resized.

    A normal QLabel::size() doesn't report what i need. ATM, i have a 2000px*1500px image loaded in an QLabel, and set
    Qt Code:
    1. imageLabel->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
    To copy to clipboard, switch view to plain text mode 
    to avoid that the label automaticaly adjusts its size so display the whole image. I have to places, where i use QLabels to display images, one is the overview page, where i have 5 QLabels in a QStackedLayout, and one in the imageViewer itself. Both are added to a Layout.
    Any hints?
    C167

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel size, for image display

    You can ask QLabel to scale the image, by setting QLabel::scaledContents property to true. Other solution is to add scrollbars using QScrollArea.

    When it comes to sizes, size() returns valid data only after you show the widget for the first time.

  3. #3
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    hm... but that resizes them by stretching... that might look ugly when viewing photos, where the proportions are important. I thought about QImage::scaled(), but for that i need the actual size and (thats important) get noticed if the window gets resized (this resizes a QVBoxLayout in one case, where the label uses the most place it gets per default).

    I added a
    Qt Code:
    1. qDebug() << label0->size();
    To copy to clipboard, switch view to plain text mode 
    immediatly after setting the pixmap. the result is always the same: QSize(100, 30).

    I already searched for a signal that gets emited when when resizing, but found none... The resizing of the images itself should not be the problem, as mentioned above, using QImage.
    Or is there a better way? painting many pixmaps into one QLabel in a short period of time is rather slow.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel size, for image display

    Cant u get the size from size() as Jacek said ??
    also QLabel is inherited from QWidget, so geometry() will also be available.

  5. #5
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    I got a new hdd, so i can't continue developing. Would it be possible to get the size if i add a QWidget into the QVBoxLayout and add the label to it? or any other constellation?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel size, for image display

    Yes, but you have to show that widget or at least wait for it to be polished, before you will get a valid size.

  7. #7
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    nope, i tried several things to get the size, using widgets, labels etc. every time i want to have the size i get a QSize(100,30) (or the equivalent using geometry()) so i have no chance to resize the images using QImage

  8. #8
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    Quote Originally Posted by C167 View Post
    nope, i tried several things to get the size, using widgets, labels etc. every time i want to have the size i get a QSize(100,30) (or the equivalent using geometry()) so i have no chance to resize the images using QImage
    I hope this code help you.
    Qt Code:
    1. QLabel *lab=new QLabel(0);
    2. QPixmap *p=new QPixmap("plane.jpg");
    3. QPixmap p1(p->scaled ( 300,300, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
    4. lab->setPixmap(p1);
    5. lab->show();
    6. lab->setFixedHeight(p1.height());
    7. lab->setFixedWidth(p1.width());
    To copy to clipboard, switch view to plain text mode 
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  9. #9
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    Qt Code:
    1. lab->setFixedHeight(p1.height());
    2. lab->setFixedWidth(p1.width());
    To copy to clipboard, switch view to plain text mode 
    you can also use
    Qt Code:
    1. lab->adjustSize();
    To copy to clipboard, switch view to plain text mode 
    in spite of above.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  10. #10
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    thanks
    now i understand where i was wrong. your code is much easier than my resize-function. Now the main problem to solve: how to get the maximum size the label can occupy?
    I attached two screenshots. The first shows the splash, i changed the window-size to be a bit more than the fixed size of the label. Here, you can see the following structure:
    Qt Code:
    1. QMainWindow->setCentralWidget(QStackedWidget)->QWidget->QStackedLayout->QLabel
    To copy to clipboard, switch view to plain text mode 
    where the label at the end holds the image that you can see. this should always use the maximum size, like your code does. I thought i could get the size by using a QWidget instead of the label and attach the label to the widgets layout (i used QVBoxLayout).

    The next screenshot shows the same problem. The area with a black border is a QLabel, that should also display images, but now with Qt::KeepAspectRatio. The Label should resize when window resizes, but by keeping the proportions of the file (image.size=label.size). Would be quite easy i thought some weeks ago...
    Attached Images Attached Images

  11. #11
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    Qt Code:
    1. how to get the maximum size the label can occupy
    To copy to clipboard, switch view to plain text mode 
    Use maximumSize() and setMaximumSize() for this.
    And use this code for your problem;
    Qt Code:
    1. #include <QtGui>
    2. int main (int argc, char **argv)
    3. {
    4.  
    5. QApplication app(argc,argv);
    6.  
    7. QLabel *lab=new QLabel(&w);
    8. QPixmap *p=new QPixmap("plane.jpg");
    9. QPixmap p1(p->scaled ( lab->width(),lab->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
    10. lab->setPixmap(p1);
    11. lab->show();
    12. lab->adjustSize();
    13. w.show();
    14. w.resize(300,200);
    15. return app.exec();
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  12. #12
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    Quote Originally Posted by ashukla View Post
    Qt Code:
    1. how to get the maximum size the label can occupy
    To copy to clipboard, switch view to plain text mode 
    Use maximumSize() and setMaximumSize() for this.
    i mean the maximum size inside the widget, the maximum is QSize(16777215, 16777215)...

    Quote Originally Posted by ashukla View Post
    And use this code for your problem;
    Qt Code:
    1. #include <QtGui>
    2. int main (int argc, char **argv)
    3. {
    4.  
    5. QApplication app(argc,argv);
    6.  
    7. QLabel *lab=new QLabel(&w);
    8. QPixmap *p=new QPixmap("plane.jpg");
    9. QPixmap p1(p->scaled ( lab->width(),lab->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
    10. lab->setPixmap(p1);
    11. lab->show();
    12. lab->adjustSize();
    13. w.show();
    14. w.resize(300,200);
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    that generates a nixe little window with the picture at 100*30 in the top left corner...

  13. #13
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QLabel size, for image display

    You can set and use after the maximumSize of a widget using maximumSize() and setMaximumSize().
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  14. #14
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: QLabel size, for image display

    Quote Originally Posted by C167 View Post
    i mean the maximum size inside the widget, the maximum is QSize(16777215, 16777215)...


    that generates a nixe little window with the picture at 100*30 in the top left corner...
    How is that a solution? If I read the comments correctly you are adjusting the label to be displayed as BIG as it can be. yet it displays a 100 x 30 image.
    I am experiencing the same problem and don't seem to be able to display an image other than 100 x 30 size

Similar Threads

  1. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57
  2. QLabel, large, rich text, font size
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2007, 11:56
  3. Replies: 4
    Last Post: 10th December 2006, 09:04
  4. QScrollArea display custom QLabel
    By spawnwj in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2006, 03:38
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22:14

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.