Results 1 to 12 of 12

Thread: QLabel not sized from dimensions in designer during runtime

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

    Default 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 × 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 100×30 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 100×30?

    Source for the form is below.

    Thanks in advance for any help.

    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading UI file 'mainwindowfX1656.ui'
    3. **
    4. ** Created by: Qt User Interface Compiler version 5.1.1
    5. **
    6. ** WARNING! All changes made in this file will be lost when recompiling UI file!
    7. ********************************************************************************/
    8.  
    9. #ifndef MAINWINDOWFX1656_H
    10. #define MAINWINDOWFX1656_H
    11.  
    12. #include <QtCore/QVariant>
    13. #include <QtWidgets/QAction>
    14. #include <QtWidgets/QApplication>
    15. #include <QtWidgets/QButtonGroup>
    16. #include <QtWidgets/QGridLayout>
    17. #include <QtWidgets/QHBoxLayout>
    18. #include <QtWidgets/QHeaderView>
    19. #include <QtWidgets/QLabel>
    20. #include <QtWidgets/QMainWindow>
    21. #include <QtWidgets/QMenu>
    22. #include <QtWidgets/QMenuBar>
    23. #include <QtWidgets/QPushButton>
    24. #include <QtWidgets/QSlider>
    25. #include <QtWidgets/QSpacerItem>
    26. #include <QtWidgets/QSpinBox>
    27. #include <QtWidgets/QStatusBar>
    28. #include <QtWidgets/QWidget>
    29.  
    30. QT_BEGIN_NAMESPACE
    31.  
    32. class Ui_MainWindow
    33. {
    34. public:
    35. QAction *actionOpen_File;
    36. QAction *actionAbout;
    37. QAction *actionAbout_Qt;
    38. QAction *actionZoom_In;
    39. QAction *actionZoom_Out;
    40. QAction *action_Normal_Size;
    41. QWidget *centralWidget;
    42. QGridLayout *gridLayout;
    43. QHBoxLayout *horizontalLayout_2;
    44. QPushButton *btnChange;
    45. QSpacerItem *horizontalSpacer;
    46. QHBoxLayout *horizontalLayout;
    47. QLabel *label;
    48. QSpinBox *sbScaleImage;
    49. QSlider *sliChangeXPos;
    50. QLabel *xposition;
    51. QLabel *lblImage;
    52. QSlider *sliChangeYPos;
    53. QLabel *yposition;
    54. QMenuBar *menuBar;
    55. QMenu *menu_File;
    56. QMenu *menuHelp;
    57. QMenu *menuView;
    58. QStatusBar *statusBar;
    59.  
    60. void setupUi(QMainWindow *MainWindow)
    61. {
    62. if (MainWindow->objectName().isEmpty())
    63. MainWindow->setObjectName(QStringLiteral("MainWindow"));
    64. MainWindow->resize(1427, 731);
    65. MainWindow->setMinimumSize(QSize(0, 0));
    66. actionOpen_File = new QAction(MainWindow);
    67. actionOpen_File->setObjectName(QStringLiteral("actionOpen_File"));
    68. actionAbout = new QAction(MainWindow);
    69. actionAbout->setObjectName(QStringLiteral("actionAbout"));
    70. actionAbout_Qt = new QAction(MainWindow);
    71. actionAbout_Qt->setObjectName(QStringLiteral("actionAbout_Qt"));
    72. actionZoom_In = new QAction(MainWindow);
    73. actionZoom_In->setObjectName(QStringLiteral("actionZoom_In"));
    74. actionZoom_Out = new QAction(MainWindow);
    75. actionZoom_Out->setObjectName(QStringLiteral("actionZoom_Out"));
    76. action_Normal_Size = new QAction(MainWindow);
    77. action_Normal_Size->setObjectName(QStringLiteral("action_Normal_Size"));
    78. action_Normal_Size->setCheckable(true);
    79. centralWidget = new QWidget(MainWindow);
    80. centralWidget->setObjectName(QStringLiteral("centralWidget"));
    81. gridLayout = new QGridLayout(centralWidget);
    82. gridLayout->setObjectName(QStringLiteral("gridLayout"));
    83. gridLayout->setContentsMargins(-1, 9, -1, 9);
    84. horizontalLayout_2 = new QHBoxLayout();
    85. horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
    86. btnChange = new QPushButton(centralWidget);
    87. btnChange->setObjectName(QStringLiteral("btnChange"));
    88.  
    89. horizontalLayout_2->addWidget(btnChange);
    90.  
    91. horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    92.  
    93. horizontalLayout_2->addItem(horizontalSpacer);
    94.  
    95. horizontalLayout = new QHBoxLayout();
    96. horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
    97. label = new QLabel(centralWidget);
    98. label->setObjectName(QStringLiteral("label"));
    99.  
    100. horizontalLayout->addWidget(label);
    101.  
    102. sbScaleImage = new QSpinBox(centralWidget);
    103. sbScaleImage->setObjectName(QStringLiteral("sbScaleImage"));
    104. sbScaleImage->setEnabled(false);
    105. sbScaleImage->setMinimum(-25);
    106. sbScaleImage->setMaximum(10);
    107. sbScaleImage->setValue(1);
    108.  
    109. horizontalLayout->addWidget(sbScaleImage);
    110.  
    111.  
    112. horizontalLayout_2->addLayout(horizontalLayout);
    113.  
    114.  
    115. gridLayout->addLayout(horizontalLayout_2, 0, 0, 1, 2);
    116.  
    117. sliChangeXPos = new QSlider(centralWidget);
    118. sliChangeXPos->setObjectName(QStringLiteral("sliChangeXPos"));
    119. sliChangeXPos->setMinimum(0);
    120. sliChangeXPos->setMaximum(200);
    121. sliChangeXPos->setPageStep(1);
    122. sliChangeXPos->setValue(0);
    123. sliChangeXPos->setSliderPosition(0);
    124. sliChangeXPos->setOrientation(Qt::Horizontal);
    125. sliChangeXPos->setInvertedAppearance(false);
    126. sliChangeXPos->setInvertedControls(true);
    127. sliChangeXPos->setTickInterval(0);
    128.  
    129. gridLayout->addWidget(sliChangeXPos, 1, 0, 1, 1);
    130.  
    131. xposition = new QLabel(centralWidget);
    132. xposition->setObjectName(QStringLiteral("xposition"));
    133.  
    134. gridLayout->addWidget(xposition, 1, 1, 1, 1);
    135.  
    136. lblImage = new QLabel(centralWidget);
    137. lblImage->setObjectName(QStringLiteral("lblImage"));
    138. lblImage->setStyleSheet(QStringLiteral("background-color: rgb(85, 170, 255);"));
    139. lblImage->setFrameShape(QFrame::Panel);
    140. lblImage->setFrameShadow(QFrame::Plain);
    141. lblImage->setAlignment(Qt::AlignCenter);
    142. lblImage->setTextInteractionFlags(Qt::NoTextInteraction);
    143.  
    144. gridLayout->addWidget(lblImage, 2, 0, 2, 1);
    145.  
    146. sliChangeYPos = new QSlider(centralWidget);
    147. sliChangeYPos->setObjectName(QStringLiteral("sliChangeYPos"));
    148. sliChangeYPos->setPageStep(1);
    149. sliChangeYPos->setOrientation(Qt::Vertical);
    150. sliChangeYPos->setInvertedAppearance(true);
    151.  
    152. gridLayout->addWidget(sliChangeYPos, 2, 1, 1, 1);
    153.  
    154. yposition = new QLabel(centralWidget);
    155. yposition->setObjectName(QStringLiteral("yposition"));
    156.  
    157. gridLayout->addWidget(yposition, 3, 1, 1, 1);
    158.  
    159. MainWindow->setCentralWidget(centralWidget);
    160. menuBar = new QMenuBar(MainWindow);
    161. menuBar->setObjectName(QStringLiteral("menuBar"));
    162. menuBar->setGeometry(QRect(0, 0, 1427, 21));
    163. menu_File = new QMenu(menuBar);
    164. menu_File->setObjectName(QStringLiteral("menu_File"));
    165. menuHelp = new QMenu(menuBar);
    166. menuHelp->setObjectName(QStringLiteral("menuHelp"));
    167. menuView = new QMenu(menuBar);
    168. menuView->setObjectName(QStringLiteral("menuView"));
    169. MainWindow->setMenuBar(menuBar);
    170. statusBar = new QStatusBar(MainWindow);
    171. statusBar->setObjectName(QStringLiteral("statusBar"));
    172. MainWindow->setStatusBar(statusBar);
    173.  
    174. menuBar->addAction(menu_File->menuAction());
    175. menuBar->addAction(menuHelp->menuAction());
    176. menuBar->addAction(menuView->menuAction());
    177. menu_File->addAction(actionOpen_File);
    178. menuHelp->addAction(actionAbout);
    179. menuHelp->addSeparator();
    180. menuHelp->addAction(actionAbout_Qt);
    181. menuView->addAction(actionZoom_In);
    182. menuView->addAction(actionZoom_Out);
    183. menuView->addSeparator();
    184.  
    185. retranslateUi(MainWindow);
    186.  
    187. QMetaObject::connectSlotsByName(MainWindow);
    188. } // setupUi
    189.  
    190. void retranslateUi(QMainWindow *MainWindow)
    191. {
    192. MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
    193. actionOpen_File->setText(QApplication::translate("MainWindow", "Open File", 0));
    194. #ifndef QT_NO_TOOLTIP
    195. actionOpen_File->setToolTip(QApplication::translate("MainWindow", "Open an image for viewing", 0));
    196. #endif // QT_NO_TOOLTIP
    197. actionAbout->setText(QApplication::translate("MainWindow", "About", 0));
    198. actionAbout_Qt->setText(QApplication::translate("MainWindow", "About Qt", 0));
    199. actionZoom_In->setText(QApplication::translate("MainWindow", "Zoom In", 0));
    200. actionZoom_In->setShortcut(QApplication::translate("MainWindow", "Ctrl++", 0));
    201. actionZoom_Out->setText(QApplication::translate("MainWindow", "Zoom Out", 0));
    202. actionZoom_Out->setShortcut(QApplication::translate("MainWindow", "Ctrl+-", 0));
    203. action_Normal_Size->setText(QApplication::translate("MainWindow", "&Normal Size", 0));
    204. action_Normal_Size->setShortcut(QApplication::translate("MainWindow", "Ctrl+S", 0));
    205. btnChange->setText(QApplication::translate("MainWindow", "Invert Image", 0));
    206. label->setText(QApplication::translate("MainWindow", "Scale", 0));
    207. xposition->setText(QString());
    208. lblImage->setText(QString());
    209. yposition->setText(QString());
    210. menu_File->setTitle(QApplication::translate("MainWindow", "File", 0));
    211. menuHelp->setTitle(QApplication::translate("MainWindow", "Help", 0));
    212. menuView->setTitle(QApplication::translate("MainWindow", "View", 0));
    213. } // retranslateUi
    214.  
    215. };
    216.  
    217. namespace Ui {
    218. class MainWindow: public Ui_MainWindow {};
    219. } // namespace Ui
    220.  
    221. QT_END_NAMESPACE
    222.  
    223. #endif // MAINWINDOWFX1656_H
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    The widget size is driven by the layout within the constraints imposed by the widget itself, the available screen space for the layout, stretch factors, and the other widgets in the layout. If your label still has the default size policies then it is resizable to make space for other widgets that might need it or to fit a default sized container window... and probably is. If your widget must have at least a certain size then set that size and set the size policy to something other than preferred. You may also need to allow for the possibility the widget cannot fit on the user's screen.

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    I don't think I quite understand all you just wrote. What constraints other than the code for the UI I posted are being imposed? I put a QLabel on a QmainWindow and performed a grid layout on the the central widget.

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    Designer is not a literal WYSIWYG editor with absolute placement and sizing (like the UI editor of Visual Basic 6 and similar vintage). Designer provides a mechanism for placing widgets into a layout. You see the general relationship of widgets in Designer but the layout code determines at run time what the end result will be. Each widget can have a minimum size, maximum size, a size hint (preferred size), and policies describing how a layout should treat their size hint. Layouts may also support minimum and maximum constraints that are used also. These are all set in Designer or derived from other characteristics of the widget (e.g. the amount text and font size in a label) and taken into consideration at run time by the layout code. When more space becomes available to a layout, i.e. its container is resized, then the new space is allocated to the contained widgets according to any stretch factors that are present (or evenly allocated otherwise).

    In your uic example, lblImage has neither minimum nor maximum size set, no explicitly set size policy (defaults to Preferred). The result is that this widget can be resized by the layout almost without restriction.

    Qt4 Layout Management
    Qt5 Layout Management

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    Quote Originally Posted by ChrisW67 View Post

    The result is that this widget can be resized by the layout almost without restriction.

    Qt4 Layout Management
    Qt5 Layout Management
    Fair enough. Does that mean I have no control over the dimensions? I don't think so. How then do I control the child widget's dimensions? What size policies do I impose? How do I get an image to display in the size that the QLabel appears in the designer?

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    Does that mean I have no control over the dimensions?
    No, and neither I nor the documentation I pointed you at says that.
    How then do I control the child widget's dimensions?
    By setting maximum, minimum, or preferred sizes (size hints in custom widgets) and size policies. Most out-of-box widgets like QLabel provide a default size hint based on their min/max size properties and their content. Have you tried anything for yourself?
    What size policies do I impose?
    You impose size policies that make sense for the application and widgets concerned. They are documented. QSizePolicy::Fixed, there's a hint in the name, but you need to read the docs to see what it is fixing.
    How do I get an image to display in the size that the QLabel appears in the designer?
    There's generally no direct connection between the size of something in Designer and the size at run time. If you fix the size of a widget Designer will generally show the widget at that size. At run time any non-fixed widget is likely to be a different size.

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    Quote Originally Posted by ChrisW67 View Post
    There's generally no direct connection between the size of something in Designer and the size at run time. If you fix the size of a widget Designer will generally show the widget at that size. At run time any non-fixed widget is likely to be a different size.
    OK, let me ask differently. How would you create a label that displays a pixmap to use the entire area of the label. For example, I colored the label background. At runtime (before loading any image), the label is displayed at full width and height. However the label reports 100 x 30.

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    Quote Originally Posted by astodolski View Post
    OK, let me ask differently. How would you create a label that displays a pixmap to use the entire area of the label. For example, I colored the label background. At runtime (before loading any image), the label is displayed at full width and height. However the label reports 100 x 30.
    I have no idea what you are asking for. The label is the "full width and height" of what? How is the label reporting "100 x 30"? In what way is that a problem?

    If the pixmap is smaller or larger than the geometry of the label then you will need to scale it in some way to fit the "entire area of the label". This is not automagical and there is more than one way to scale something to fit. Have a look at QLabel::scaledContents() for one option.

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    Quote Originally Posted by ChrisW67 View Post
    I have no idea what you are asking for. The label is the "full width and height" of what? How is the label reporting "100 x 30"? In what way is that a problem?

    If the pixmap is smaller or larger than the geometry of the label then you will need to scale it in some way to fit the "entire area of the label". This is not automagical and there is more than one way to scale something to fit. Have a look at QLabel::scaledContents() for one option.
    I may have to set up the label size after construction. I see that when the main window is shown and no image loaded I can re-size the client area so small that the image gets reduced to the 100 x 30. So at construction the label is at the smallest that is allowed by the layout (100 x 30).

    My comment earlier is that after construction and when the window is shown the label appears at the full width and height allowed by the layout. However, I am telling the image to be displayed in the size of the label as reported in the main window constructor. The image appears very small - not the size of the label when the window is shown

    Hope that's clearer.

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    Quote Originally Posted by astodolski View Post
    However, I am telling the image to be displayed in the size of the label as reported in the main window constructor. The image appears very small - not the size of the label when the window is shown
    So you are asking where to set the image size instead of in the constructor. My suggestion would be in showEvent().

    Cheers,
    _

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    Quote Originally Posted by anda_skoa View Post
    So you are asking where to set the image size instead of in the constructor. My suggestion would be in showEvent().

    Cheers,
    _
    Perhaps something like below?

    Qt Code:
    1. void MainWindow::showEvent(QShowEvent* e)
    2. {
    3. QMainWindow::showEvent(e);
    4.  
    5. if(e->spontaneous())
    6. return;
    7.  
    8. if(isInitialized)
    9. return;
    10.  
    11. MappedBuffer = new quint8[ui->lblImage->width() * ui->lblImage->height() * BITMAP_FORMAT];
    12. origImage = new QImage(MappedBuffer, ui->lblImage->width(), ui->lblImage->height(), BITMAP_FORMAT);
    13.  
    14.  
    15. isInitialized = true;
    16. }
    To copy to clipboard, switch view to plain text mode 

    As an alternate, why is showEvent() preferred over event()?
    Qt Code:
    1. bool MainWindow::event(QEvent* e)
    2. {
    3. int returnValue = QMainWindow::event(e);
    4.  
    5. if(e->type() == QEvent::Show)
    6. {
    7. widgetSize = ui->lblImage->size();
    8.  
    9. }
    10.  
    11. return returnValue;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by astodolski; 29th October 2013 at 14:40. Reason: updated contents

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

    Default Re: QLabel not sized from dimensions in designer during runtime

    You can also implement it in event().

    The base implementation of event() checks the type, performs a cast and then calls showEvent().
    It is just more code you have to write


    Cheers,
    _

Similar Threads

  1. Qt Creator Maybe I'm stupid or QLabel don't show image, at runtime
    By giancarlo.fir in forum Qt Tools
    Replies: 2
    Last Post: 20th March 2011, 08:52
  2. Replies: 1
    Last Post: 25th September 2010, 08:20
  3. Replies: 1
    Last Post: 29th September 2009, 19:44
  4. QLabel onto QTabWidget with Designer[Resolved it]
    By ksqt in forum Qt Programming
    Replies: 4
    Last Post: 23rd February 2008, 17:48
  5. (qt4 & xp) qlabel as background in designer
    By incapacitant in forum Newbie
    Replies: 5
    Last Post: 5th March 2006, 11:21

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.