Results 1 to 1 of 1

Thread: Shrinking / extending functionality a widget generated by Qt designer

  1. #1
    Join Date
    Jul 2015
    Posts
    20
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Shrinking / extending functionality a widget generated by Qt designer

    Dear all,

    I am trying to add some functionality to an already existing widget: some of the GUI elements should be shown or hidden depending on if the user wishes to see them or not. Also, the widget should extend or shrink in order to give just as much space as possible for the GUI elements. Basically, I am trying to do the same thing that is shown in this example:
    http://doc.qt.io/archives/qt-5.5/qtw...n-example.html

    However, since the widget I am working on already exists and was created by means of the Qt designer as a .ui file I can not just copy the code from the example.
    A minimal working example of what I am trying to do is the following:

    --------------- Start of MainWindow.ui ---------------
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>746</width>
    10. <height>494</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MainWindow</string>
    15. </property>
    16. <widget class="QWidget" name="centralwidget">
    17. <widget class="QWidget" name="gridLayoutWidget">
    18. <property name="geometry">
    19. <rect>
    20. <x>10</x>
    21. <y>10</y>
    22. <width>331</width>
    23. <height>181</height>
    24. </rect>
    25. </property>
    26. <layout class="QGridLayout" name="mainLayout">
    27. <property name="sizeConstraint">
    28. <enum>QLayout::SetMinimumSize</enum>
    29. </property>
    30. <item row="1" column="0">
    31. <widget class="QPushButton" name="pbtExtend">
    32. <property name="text">
    33. <string>extend</string>
    34. </property>
    35. <property name="checkable">
    36. <bool>true</bool>
    37. </property>
    38. </widget>
    39. </item>
    40. <item row="2" column="0">
    41. <widget class="QWidget" name="extensionWidget" native="true">
    42. <widget class="QWidget" name="gridLayoutWidget_2">
    43. <property name="geometry">
    44. <rect>
    45. <x>60</x>
    46. <y>30</y>
    47. <width>160</width>
    48. <height>81</height>
    49. </rect>
    50. </property>
    51. <layout class="QGridLayout" name="extensionLayout">
    52. <item row="1" column="0">
    53. <widget class="QLabel" name="lbl2">
    54. <property name="text">
    55. <string>TextLabel</string>
    56. </property>
    57. </widget>
    58. </item>
    59. <item row="0" column="0">
    60. <widget class="QLabel" name="lbl1">
    61. <property name="text">
    62. <string>label 1</string>
    63. </property>
    64. </widget>
    65. </item>
    66. <item row="0" column="1">
    67. <widget class="QDoubleSpinBox" name="dsb1"/>
    68. </item>
    69. <item row="1" column="1">
    70. <widget class="QDoubleSpinBox" name="dsb2"/>
    71. </item>
    72. </layout>
    73. </widget>
    74. </widget>
    75. </item>
    76. </layout>
    77. </widget>
    78. </widget>
    79. <widget class="QMenuBar" name="menubar">
    80. <property name="geometry">
    81. <rect>
    82. <x>0</x>
    83. <y>0</y>
    84. <width>746</width>
    85. <height>21</height>
    86. </rect>
    87. </property>
    88. </widget>
    89. <widget class="QStatusBar" name="statusbar"/>
    90. </widget>
    91. <resources/>
    92. <connections/>
    93. </ui>
    To copy to clipboard, switch view to plain text mode 
    --------------- end of MainWindow.ui ---------------


    --------------- Start of MainWindow.h ---------------
    Qt Code:
    1. #include <QtWidgets>
    2. #include <QMainWindow>
    3. #include <ui_MainWindow.h>
    4.  
    5. class MainWindow : public QMainWindow
    6. {
    7. Q_OBJECT
    8. public:
    9. MainWindow(QMainWindow *parent = 0);
    10. private:
    11. Ui::MainWindow * m_pMainWindow;
    12. };
    To copy to clipboard, switch view to plain text mode 
    --------------- end of MainWindow.h ---------------


    --------------- Start of MainWindow.cpp ---------------
    Qt Code:
    1. #include <iostream>
    2. #include "MainWindow.h"
    3.  
    4. MainWindow::MainWindow(QMainWindow *parent) :
    5. QMainWindow(parent)
    6. {
    7. m_pMainWindow = new Ui::MainWindow;
    8. m_pMainWindow->setupUi(this);
    9. m_pMainWindow->extensionWidget->setVisible(true);
    10. m_pMainWindow->mainLayout->setSizeConstraint(QLayout::SetFixedSize); //if this line is uncommented, then the extensionWidget is not visible at all
    11. QObject::connect(m_pMainWindow->pbtExtend, &QPushButton::toggled, m_pMainWindow->extensionWidget, &QWidget::setVisible);
    12. }
    To copy to clipboard, switch view to plain text mode 
    --------------- end of MainWindow.cpp ---------------


    In main.cpp I am calling it like:

    Qt Code:
    1. QApplication app(argc, argv);
    2. MainWindow w;
    3. w.show();
    4. app.exec();
    To copy to clipboard, switch view to plain text mode 

    In forums / posts I keep reading that I need to do sth. like
    m_pMainWindow->mainLayout->setSizeConstraint(QLayout::SetFixedSize);

    in order to achieve that the window size extends and shrinks as necessary, however, if I use this line, then the extensionWidget is not visible at all.
    I already tried to do the same thing in another widget that subclasses QDialog because I was suspecting that it maybe only works in a QDialog, but the result was the same.
    What am I doing wrong???

    Thanks in advance!
    Last edited by anda_skoa; 6th December 2016 at 07:49. Reason: missing [code] tags

Similar Threads

  1. extending properties of designer plugin
    By rashmi in forum Newbie
    Replies: 1
    Last Post: 9th February 2011, 18:59
  2. Replies: 2
    Last Post: 28th May 2010, 15:26
  3. Replies: 0
    Last Post: 3rd January 2009, 05:26
  4. Expanding/shrinking a widget
    By punkypogo in forum Qt Programming
    Replies: 6
    Last Post: 27th May 2008, 16:50
  5. Replies: 13
    Last Post: 15th December 2006, 11:52

Tags for this Thread

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.