Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QLabel not sized from dimensions in designer during runtime



  • I have a QLabel used to display a QImage. In design mode the label geometry is (9,65), 1384 x 616 as listed in the Property Editor. At runtime when the MainWindow containing the label constructs (i.e. ui->setupUi(this)), the label always appears to be 100x30 at startup which leads to images displaying only a really small portion.

    Why do I have to set it to a fixed size to be anything but 100x30?

    Source for the UI exceeds posting rules or I would have included it here



  • QLabel's scaledContents doesn't help?



  • [quote author="NicuPopescu" date="1382713466"]QLabel's scaledContents doesn't help?[/quote]

    No. For my purposes I need pixels to be 1:1. The property scaledContents distorts the image.


  • Lifetime Qt Champion

    Hi,

    If you want one by one, you can set the fixed size of your label to your image size or put the label in a QScrollArea

    Hope it helps



  • [quote author="SGaist" date="1382728235"]Hi,

    If you want one by one, you can set the fixed size of your label to your image size or put the label in a QScrollArea

    Hope it helps[/quote]

    Not really.
    The size of the image is too big to do that - images could be approx 1GB. I create an aperture in the form of a QPixmap from a QImage contained in a QLabel which traverses the area of the original image size via scroll bars. The scrollArea is usefull but doesn't address the original item asked. I need to have the QLabel containing the image and displayed at the full width and height dimensions as reported in the designer.


  • Lifetime Qt Champion

    Then use my first proposition, use setFixedSize



  • Since the QLAbel is in a layout and I want it scale with resizing the client area, what criteria would you use for a fixed size? The QLabel which is a child of the MainWindow is of indeterminate size before an image is loaded; especially on any given platform and/or monitor.


  • Lifetime Qt Champion

    It's getting less clear now.

    You have an image of a given size, you want to put it in a QLabel which should have the same size as the image ?

    Or should your label only show a portion of the original image. The label size being based on the current size of this widget ?



  • [quote author="SGaist" date="1382999058"]

    Or should your label only show a portion of the original image.[/quote]

    Calling setPixmap with the image dimensions of the label causes a portion of the image to be shown in the label

    for example:
    @
    origImage = new QImage(MappedBuffer, ui->lblImage->width(), ui->lblImage->height(), BITMAP_FORMAT);
    ui->lblImage->setPixmap(QPixmap::fromImage(*origImage, Qt::AutoColor));
    @

    The label could be 640 x 480 whereas the image could be multiples of that.



  • What I understood from the above discussion I came up with the following result :---
    If you want to render an image on QLabel on run time without distortion.
    You can simply do the following snippet of the code -----
    QImage img(":/img/index.jpg");
    ui->label->setGeometry(X,Y,img.width(),img.height());
    ui->label->setPixmap(QPixmap::fromImage(img));



  • bq. Since the QLAbel is in a layout and I want it scale with resizing the client area, what criteria would you use for a fixed size? The QLabel which is a child of the MainWindow is of indeterminate size before an image is loaded; especially on any given platform and/or monitor.

    I only can guess what is your purpose:

    • you need a layout for other widgets to resize them automatically, when main window is resized
      -inside layout you need a QLable initially at fixed size, exactly as defined in designer, and not affected by layout; then QLabel should scale while main window resizes

    then I think you should set in designer the size policy to Maximum, and set in designer both minimum and maximum sizes to the same values

    hope it helps ...

    Cheers!



  • [quote author="NicuPopescu" date="1383050803"]
    I only can guess what is your purpose:

    • you need a layout for other widgets to resize them automatically, when main window is resized
      [/quote]

    Pretty good guess - I thought that was clear.

    [quote]
    -inside layout you need a QLable initially at fixed size, exactly as defined in designer, and not affected by layout; then QLabel should scan while main window resizes

    then I think you should set in designer the size policy to Maximum, and set in designer both minimum and maximum sizes to the same values
    [/quote]

    This serves to fix the size of the QLabel in its layout by setting Minimum and Maximum to the same value. When the main window re-sizes, the label remains a static size.



  • brr ... I said scan instead of scale :)

    anyway, if you need that label's size to scale with new main window size, I think you should do it by code adjusting the minimum and maximum sizes by a factor=new_main_window_size/start_main_window_size



  • What I came up with to get a proper size QLabel after construction:

    @
    void MainWindow::showEvent(QShowEvent* e)
    {
    if(isInitialized)
    return;

    QMainWindow::showEvent(e);
    
    if(e->spontaneous())
        return;
    
    widgetSize = ui->lblImage->size();
    MappedBuffer = new quint8[ui->lblImage->width() * ui->lblImage->height() * BITMAP_FORMAT];
    origImage = new QImage(MappedBuffer, ui->lblImage->width(), ui->lblImage->height(), BITMAP_FORMAT);
    LoadTestImage();
    
    isInitialized = true;
    

    }
    @

    I hope this helps someone



  • imho with what I suggested so far combined with scaledContents=true you get the same result without extra work: MappedBuffer must have app life time and destroy them explicitly; have you mentioned you need to store your original image? what about label's scaling?

    :)



  • The label is going to be the result of the buffer contents. That is, I am not going to scale QImage or QPixmap. These aren't JPG or BMP images. These are high resolution 16 bit grey scale images. I need 1:1 pixel representation and scaling pixmaps is not going to cut it.


Log in to reply