Results 1 to 4 of 4

Thread: image shown in Qlabel -> size increases

  1. #1
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default image shown in Qlabel -> size increases

    Hi,

    I have a main window, I've set a gridlayout on it. I placed some QLabels into the grids where I show images using QLabel::setPixmap(). Thnew images should be shown at some user interactions: pushing buttons, clicking on radiobuttons etc. Now I cannot imagine why but the size of the labels get bigger and bigger on each click. The images are loaded in this way:
    ui->label_img->setPixmap(myimg.scaled(ui->label_img->width(),ui->label_img->height(),Qt::KeepAspectRatio));

    This is what I would like to solve without setting any size fixed. (window should stay "flexible")
    http://youtu.be/YgzcuOXF5RI

    Szilvi
    Szilvi

  2. #2
    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 shown in Qlabel -> size increases

    The pixmap size follows the label. What in your code is changing the size of the label?

  3. #3
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: image shown in Qlabel -> size increases

    I've changed now the QLabels' sizePolicy to "Ignored" in the in-built Qt Designer. Now it works fine. But why does it increase otherwise?
    Szilvi

  4. #4
    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 shown in Qlabel -> size increases

    Do you have a style sheet attached to the label? Does it add a margin or padding? If so, each time you scale the image you add the margin/padding to it size.

    This, for example, does not change the size of the label every two seconds:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow: public QMainWindow {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget *p = 0): QMainWindow(p) {
    8.  
    9. QWidget *central = new QWidget(this);
    10. label1 = new QLabel(this);
    11. image.load("test.png");
    12. label1->setPixmap(image);
    13. qDebug() << image.size() << label1->size();
    14. label2 = new QLabel("Label 2", this);
    15.  
    16. QVBoxLayout *layout = new QVBoxLayout(central);
    17. layout->addWidget(label1);
    18. layout->addWidget(label2);
    19.  
    20. central->setLayout(layout);
    21. setCentralWidget(central);
    22.  
    23. connect(&t, SIGNAL(timeout()), SLOT(tick()));
    24. t.start(2000);
    25. }
    26. public slots:
    27. void tick() {
    28. QPixmap temp = image.scaled(label1->width(), label1->height(), Qt::KeepAspectRatio);
    29. qDebug() << temp.size() << label1->size();
    30. label1->setPixmap(temp);
    31. }
    32. private:
    33. QPixmap image;
    34. QLabel *label1 ;
    35. QLabel *label2 ;
    36. QTimer t;
    37. };
    38.  
    39. int main(int argc, char *argv[])
    40. {
    41. QApplication app(argc, argv);
    42.  
    43. MainWindow m;
    44. m.show();
    45. return app.exec();
    46. }
    47. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    unless you add:
    Qt Code:
    1. qApp->setStyleSheet("QLabel { margin: 5px; }");
    To copy to clipboard, switch view to plain text mode 
    in main().

Similar Threads

  1. QLabel size, for image display
    By C167 in forum Qt Programming
    Replies: 13
    Last Post: 25th October 2013, 16:09
  2. trying to adjust size of image in a qlabel
    By mmm286 in forum Newbie
    Replies: 1
    Last Post: 5th November 2009, 06:43
  3. Replies: 1
    Last Post: 2nd August 2008, 15:46
  4. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 14:33
  5. Need to know widget size before first shown
    By durbrak in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2007, 20:37

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.