Results 1 to 15 of 15

Thread: Problem with resizing widget

  1. #1
    Join Date
    Aug 2012
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Problem with resizing widget

    Hi all

    I have QTextEdit in mainWindow. I'm trying to resize QTextEdit so height = mainwindow.height and width = height/2. That should change when I change the size of mainWindow during runtime.

    How can I do that?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Problem with resizing widget

    If you mean "QMainWindow" and you have called QMainWindow::setCentralWidget( textEdit ), then the main window will automatically resize the text edit to be the same size as its central area. Are you saying that you want the text edit to be a different size from the main window?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2012
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Problem with resizing widget

    it's ok I've fixed it using this code:

    void MainWindow::resizeEvent(QResizeEvent *)
    {
    ui->textEdit->resize(width()/2,height());
    }

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Problem with resizing widget

    you should really be doing this with a http://doc.qt.io/qt-4.8/QVBoxLayout and using a spacer to keep the bottom half empty
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Aug 2012
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Problem with resizing widget

    Quote Originally Posted by amleto View Post
    you should really be doing this with a http://doc.qt.io/qt-4.8/QVBoxLayout and using a spacer to keep the bottom half empty
    that's not the point, compare my code with yours and you'll see what I'm talking about.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Problem with resizing widget

    that's not the point, compare my code with yours and you'll see what I'm talking about.
    Since you haven't posted any code other than a resize event, it is pretty tough to compare, isn't it?

    The point is, if you are using a QMainWindow and you aren't using a central widget with a layout to control the relative sizes and positions of the child widgets inside it, then you are doing it wrong. If you are simply making child widgets and manually positioning and sizing them inside a QMainWindow parent, then you are doing it wrong. The comments you have made seem to imply that this is exactly what your code does.

    The purpose of this forum is to help people solve Qt problems and become better Qt programmers. If you argue with the people who post answers and continue to do things wrong, you won't get better.
    Last edited by d_stranz; 3rd September 2012 at 18:51.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Aug 2012
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Problem with resizing widget

    I'm sorry if I insulted you, that was not my intention. How would you solve this problem. I'm using this code:

    void MainWindow::resizeEvent(QResizeEvent *)
    {
    ui->textEdit->resize(width()/2,height());
    }

    and it works perfectly. If there is better way for doing this, then I would like to know about it.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Problem with resizing widget

    It may work, but from what you have previously said, your implementation of the main window content and layout does not appear to be correct. Show the code for your main window constructor, and we'll be happy to tell you what is wrong with it, if anything.

    Read the documentation on QMainWindow (especially the section titled Qt Main Window Framework) and the QMainWindow::setCentralWidget() method. If you aren't following these instructions, then you are doing it wrong and even though your resize code might work, the fact that your layout is wrong is likely causing other problems inside the QMainWindow machinery.

    So post the UI file for your main window class, and show what you are doing in the constructor.

  9. #9
    Join Date
    Aug 2012
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Problem with resizing widget

    here is UI file:

    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>693</width>
    10. <height>461</height>
    11. </rect>
    12. </property>
    13. <property name="sizePolicy">
    14. <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    15. <horstretch>0</horstretch>
    16. <verstretch>0</verstretch>
    17. </sizepolicy>
    18. </property>
    19. <property name="windowTitle">
    20. <string>BPad</string>
    21. </property>
    22. <property name="styleSheet">
    23. <string notr="true"/>
    24. </property>
    25. <widget class="QWidget" name="centralWidget">
    26. <property name="contextMenuPolicy">
    27. <enum>Qt::NoContextMenu</enum>
    28. </property>
    29. <widget class="QLineEdit" name="lineEdit">
    30. <property name="geometry">
    31. <rect>
    32. <x>534</x>
    33. <y>193</y>
    34. <width>129</width>
    35. <height>26</height>
    36. </rect>
    37. </property>
    38. <property name="maximumSize">
    39. <size>
    40. <width>150</width>
    41. <height>16777215</height>
    42. </size>
    43. </property>
    44. <property name="styleSheet">
    45. <string notr="true">border: 3px solid gray;
    46. border-color: rgb(85, 170, 255);
    47. border-radius: 9px;</string>
    48. </property>
    49. </widget>
    50. <widget class="QFrame" name="frame">
    51. <property name="geometry">
    52. <rect>
    53. <x>70</x>
    54. <y>30</y>
    55. <width>461</width>
    56. <height>291</height>
    57. </rect>
    58. </property>
    59. <property name="styleSheet">
    60. <string notr="true">background-image: url(:/new/prefix1/21112009121.jpg);</string>
    61. </property>
    62. <property name="frameShape">
    63. <enum>QFrame::StyledPanel</enum>
    64. </property>
    65. <property name="frameShadow">
    66. <enum>QFrame::Raised</enum>
    67. </property>
    68. <widget class="QTextEdit" name="textEdit">
    69. <property name="geometry">
    70. <rect>
    71. <x>120</x>
    72. <y>20</y>
    73. <width>256</width>
    74. <height>192</height>
    75. </rect>
    76. </property>
    77. <property name="sizePolicy">
    78. <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    79. <horstretch>0</horstretch>
    80. <verstretch>0</verstretch>
    81. </sizepolicy>
    82. </property>
    83. <property name="styleSheet">
    84. <string notr="true">background-image: url(:/new/prefix1/old-paper.jpg);
    85. border: 4px solid gray;
    86.  
    87. </string>
    88. </property>
    89. </widget>
    90. </widget>
    91. </widget>
    92. <widget class="QMenuBar" name="menuBar">
    93. <property name="geometry">
    94. <rect>
    95. <x>0</x>
    96. <y>0</y>
    97. <width>693</width>
    98. <height>26</height>
    99. </rect>
    100. </property>
    101. <widget class="QMenu" name="menuFile">
    102. <property name="title">
    103. <string>File</string>
    104. </property>
    105. <addaction name="actionNew"/>
    106. <addaction name="actionOpen"/>
    107. <addaction name="separator"/>
    108. <addaction name="actionSave"/>
    109. <addaction name="actionSave_as"/>
    110. <addaction name="separator"/>
    111. <addaction name="actionExit"/>
    112. </widget>
    113. <widget class="QMenu" name="menuEdit">
    114. <property name="title">
    115. <string>Edit</string>
    116. </property>
    117. <addaction name="actionCut"/>
    118. <addaction name="actionCopy"/>
    119. <addaction name="actionPaste"/>
    120. <addaction name="separator"/>
    121. <addaction name="actionUndo"/>
    122. <addaction name="actionRedo"/>
    123. <addaction name="separator"/>
    124. <addaction name="actionFont"/>
    125. </widget>
    126. <widget class="QMenu" name="menuHelp">
    127. <property name="title">
    128. <string>Help</string>
    129. </property>
    130. <addaction name="actionAbout"/>
    131. </widget>
    132. <widget class="QMenu" name="menuFind">
    133. <property name="title">
    134. <string>Find</string>
    135. </property>
    136. <addaction name="actionFind_and_replace"/>
    137. </widget>
    138. <addaction name="menuFile"/>
    139. <addaction name="menuEdit"/>
    140. <addaction name="menuFind"/>
    141. <addaction name="menuHelp"/>
    142. </widget>
    143. <widget class="QStatusBar" name="statusBar"/>
    144. <action name="actionNew">
    145. <property name="text">
    146. <string>New</string>
    147. </property>
    148. <property name="shortcut">
    149. <string>Ctrl+N</string>
    150. </property>
    151. </action>
    152. <action name="actionOpen">
    153. <property name="text">
    154. <string>Open</string>
    155. </property>
    156. <property name="shortcut">
    157. <string>Ctrl+O</string>
    158. </property>
    159. </action>
    160. <action name="actionSave">
    161. <property name="text">
    162. <string>Save</string>
    163. </property>
    164. <property name="shortcut">
    165. <string>Ctrl+S</string>
    166. </property>
    167. </action>
    168. <action name="actionSave_as">
    169. <property name="text">
    170. <string>Save as</string>
    171. </property>
    172. </action>
    173. <action name="actionExit">
    174. <property name="text">
    175. <string>Exit</string>
    176. </property>
    177. <property name="shortcut">
    178. <string>Ctrl+Q</string>
    179. </property>
    180. </action>
    181. <action name="actionCut">
    182. <property name="text">
    183. <string>Cut</string>
    184. </property>
    185. <property name="shortcut">
    186. <string>Ctrl+X</string>
    187. </property>
    188. </action>
    189. <action name="actionCopy">
    190. <property name="text">
    191. <string>Copy</string>
    192. </property>
    193. <property name="shortcut">
    194. <string>Ctrl+C</string>
    195. </property>
    196. </action>
    197. <action name="actionPaste">
    198. <property name="text">
    199. <string>Paste</string>
    200. </property>
    201. <property name="shortcut">
    202. <string>Ctrl+V</string>
    203. </property>
    204. </action>
    205. <action name="actionUndo">
    206. <property name="text">
    207. <string>Undo</string>
    208. </property>
    209. <property name="shortcut">
    210. <string>Ctrl+Y</string>
    211. </property>
    212. </action>
    213. <action name="actionRedo">
    214. <property name="text">
    215. <string>Redo</string>
    216. </property>
    217. <property name="shortcut">
    218. <string>Ctrl+Z</string>
    219. </property>
    220. </action>
    221. <action name="actionAbout">
    222. <property name="text">
    223. <string>About</string>
    224. </property>
    225. </action>
    226. <action name="actionFind_and_replace">
    227. <property name="text">
    228. <string>Find and replace</string>
    229. </property>
    230. <property name="shortcut">
    231. <string>Ctrl+F</string>
    232. </property>
    233. </action>
    234. <action name="actionFont">
    235. <property name="text">
    236. <string>Font</string>
    237. </property>
    238. </action>
    239. </widget>
    240. <layoutdefault spacing="6" margin="11"/>
    241. <resources/>
    242. <connections/>
    243. </ui>
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Problem with resizing widget

    No, it looks like you aren't doing it right. Even though you are creating a QWidget called "centralWidget", it isn't what the QMainWindow expects as the central widget. You aren't using layouts anywhere, which would allow the main window to move and resize the widgets inside it as it resizes, and you would not have to do anything in a resize event. Your code creates a lot of extra work for you - every time something changes in your GUI, you might have to rewrite all of the resize code instead of letting the main window and layouts manage all that for you.

    So technically, you can do what you're doing, but from an efficiency point of view, you aren't letting Qt do one of the things it does best.

    If you are making the transition from the Windows / MFC world into Qt, this is understandable, because MFC has no concept of layouts (at least not at the time I stopped using it 5 years ago), and you did have to manage all of the GUI resizing yourself.

  11. #11
    Join Date
    Aug 2012
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Problem with resizing widget

    I tried to achieve this using layout but I coudn't arrange things to meet my needs. Can you tell me how to achieve this using layout.

  12. #12
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo
    Thanked 17 Times in 17 Posts

    Default Re: Problem with resizing widget

    Ah, I think a few video tutorial about layout with Qt Designer will be good for you. Have you search it on youtube?
    ~ We are nothing in this universe ~

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Problem with resizing widget

    OK, here's your GUI with layouts. If you resize your original UI inside of Qt Designer, none of the widgets move, so I have specified that most of the spacers have fixed geometry. This keeps the QFrame in the same location no matter what the size of the QMainWindow. If this isn't what you want, then you can change their size policy from Fixed to something else. You can also look at setting the stretch factors for the widgets and spacers so that resize occurs in the proportions you want. Be sure to comment out your own resize code so you can see what the layout itself is doing.

    alenn.ui

  14. #14
    Join Date
    Aug 2012
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Problem with resizing widget

    really nice work man, thank you.

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,343
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Problem with resizing widget

    No problem. Sorry if I sounded a bit hard in some of the earlier posts, but I have found from lots of bashing my head on the wall that if you learn how to do things right, Qt will be really good to you and will save you a lot of work. The Qt trolls have spent a lot of effort to make GUI design and use easy because all the hard stuff is being done internally.

  16. The following user says thank you to d_stranz for this useful post:

    alenn.masic (6th September 2012)

Similar Threads

  1. Widget is not so smooth when resizing?
    By MorrisLiang in forum Newbie
    Replies: 2
    Last Post: 11th May 2010, 02:44
  2. QDock Widget resizing
    By vajindarladdad in forum Qt Programming
    Replies: 0
    Last Post: 9th December 2008, 11:12
  3. Resizing back the widget
    By gruszczy in forum Qt Programming
    Replies: 6
    Last Post: 23rd November 2008, 11:14
  4. Resizing child Widget
    By Pragya in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2007, 07:58
  5. Widget resizing problem
    By fwohlfert in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2006, 09:16

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.