PDA

View Full Version : Problem with resizing widget



alenn.masic
31st August 2012, 18:10
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?

d_stranz
31st August 2012, 22:10
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?

alenn.masic
2nd September 2012, 18:31
it's ok I've fixed it using this code:

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

amleto
3rd September 2012, 12:39
you should really be doing this with a QVBoxLayout and using a spacer to keep the bottom half empty

alenn.masic
3rd September 2012, 14:56
you should really be doing this with a 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.

d_stranz
3rd September 2012, 17:46
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.

alenn.masic
5th September 2012, 16:20
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.

d_stranz
5th September 2012, 16:29
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.

alenn.masic
5th September 2012, 16:58
here is UI file:


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>693</width>
<height>461</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>BPad</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="centralWidget">
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>534</x>
<y>193</y>
<width>129</width>
<height>26</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>150</width>
<height>16777215</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">border: 3px solid gray;
border-color: rgb(85, 170, 255);
border-radius: 9px;</string>
</property>
</widget>
<widget class="QFrame" name="frame">
<property name="geometry">
<rect>
<x>70</x>
<y>30</y>
<width>461</width>
<height>291</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-image: url(:/new/prefix1/21112009121.jpg);</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>120</x>
<y>20</y>
<width>256</width>
<height>192</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">background-image: url(:/new/prefix1/old-paper.jpg);
border: 4px solid gray;

</string>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>693</width>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="separator"/>
<addaction name="actionSave"/>
<addaction name="actionSave_as"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuEdit">
<property name="title">
<string>Edit</string>
</property>
<addaction name="actionCut"/>
<addaction name="actionCopy"/>
<addaction name="actionPaste"/>
<addaction name="separator"/>
<addaction name="actionUndo"/>
<addaction name="actionRedo"/>
<addaction name="separator"/>
<addaction name="actionFont"/>
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
<string>Help</string>
</property>
<addaction name="actionAbout"/>
</widget>
<widget class="QMenu" name="menuFind">
<property name="title">
<string>Find</string>
</property>
<addaction name="actionFind_and_replace"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
<addaction name="menuFind"/>
<addaction name="menuHelp"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionNew">
<property name="text">
<string>New</string>
</property>
<property name="shortcut">
<string>Ctrl+N</string>
</property>
</action>
<action name="actionOpen">
<property name="text">
<string>Open</string>
</property>
<property name="shortcut">
<string>Ctrl+O</string>
</property>
</action>
<action name="actionSave">
<property name="text">
<string>Save</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
<action name="actionSave_as">
<property name="text">
<string>Save as</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
<action name="actionCut">
<property name="text">
<string>Cut</string>
</property>
<property name="shortcut">
<string>Ctrl+X</string>
</property>
</action>
<action name="actionCopy">
<property name="text">
<string>Copy</string>
</property>
<property name="shortcut">
<string>Ctrl+C</string>
</property>
</action>
<action name="actionPaste">
<property name="text">
<string>Paste</string>
</property>
<property name="shortcut">
<string>Ctrl+V</string>
</property>
</action>
<action name="actionUndo">
<property name="text">
<string>Undo</string>
</property>
<property name="shortcut">
<string>Ctrl+Y</string>
</property>
</action>
<action name="actionRedo">
<property name="text">
<string>Redo</string>
</property>
<property name="shortcut">
<string>Ctrl+Z</string>
</property>
</action>
<action name="actionAbout">
<property name="text">
<string>About</string>
</property>
</action>
<action name="actionFind_and_replace">
<property name="text">
<string>Find and replace</string>
</property>
<property name="shortcut">
<string>Ctrl+F</string>
</property>
</action>
<action name="actionFont">
<property name="text">
<string>Font</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

d_stranz
5th September 2012, 17:41
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.

alenn.masic
5th September 2012, 17:54
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.

viulskiez
5th September 2012, 19:08
Ah, I think a few video tutorial about layout with Qt Designer will be good for you. Have you search it on youtube?

d_stranz
5th September 2012, 19:22
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.

8188

alenn.masic
5th September 2012, 21:34
really nice work man, thank you.

d_stranz
6th September 2012, 15:30
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.