What makes a Qt widget and its layout behave properly (in regard to its size)? - c++

I'm having all sorts of size problems with Qt. I am creating my own widgets and using different layouts (generally, I need my own to make them work properly without spending hours on the "powerful" default layouts... which don't lay things out as intended.)
Once I'm done with a widget and its layout though, it doesn't work right. The size is never getting set properly unless I call widget->resize(1, 1); which finally forces a "resize" and makes the widget look correct (i.e. recompute the geometry.) Even the updateGeometry() call has no effect.
This is a dreadful problem when the resize() needs to be called on the parent widget (yuck!) and from what I'm reading should not be necessary were the layouts properly programmed.
Is there a sample that works and is not several thousand of lines long, or does Qt require several thousand lines to make anything work perfectly, even the simplest widget?
What are the minimal functions to be called to make a widget & its layout work at once?
Thank you.
Alexis
P.S. I tried to implement the sizeHint(), minimumSize(), maximumSize(), others that I'm missing? I was hoping that would be enough. Obviously, I also implement the setGeometry() on the layout to resize the children appropriately.
--- addition 1
There is a sample image with a layout that clearly isn't available as is in Qt. The positioning, functions, and colors of the different keys is XML driven and works for any keyboard in the world.
(note, this sample doesn't show the Enter key displayed on two rows and wider below than at the top; more or less, not doable at all with the regular layouts; of course, it works with my version.)
--- clarification
I'm not too sure how to describe the problem better. I was thinking to write a test widget next to see how I can reproduce the problem and then post that and eventually fix it. 8-)
The default layout function that the internal Qt layouts make use of require a lot of coding. I would like to avoid having to copy/paste all of that because for maintenance, it makes it close to impossible.
--- today's findings
As I needed to tweak one of the widgets, I decided to add a VBoxLayout and make it work.
I actually found the problem... One of the widgets in my tree is a QScrollArea and that sizeHint() returns (-1, -1). Not exactly what I'd expect but... whatever you put inside that widget has better know how to compute its width and height or else... it fails.
Looking at the code closely, I could actually compute the width by using the widest width found. Once I used that, the widget would appear (and it actually resizes itself as things change in the list, kinda cool.)
This being said, my earlier comment about having a tree of widgets that auto-resize themselves stands. From the root up to the parents of the leaves in your tree, all of those widgets will need a valid layout. Once I added one in the top widget it resized itself and its children properly (well... in my case up to the QScrollArea, the rest required a bottom to top resizing. Funny how that works!)
--- ah! ha! moment (or: what you find reading the implementation code!)
Today I bumped in another problem which just needed the correct call... I just couldn't find anything worth it in the documentation.
All the objects have a layout now, but a certain parent would not resize properly. Plain simple.
I had a call to the parent as following:
// changes to the children are changing the geometry
parentWidget()->updateGeometry();
Yeah. The docs says that's what you have to do. Nothing happens at all with that call. No idea what it's supposed to do, I did not look at that function. It never did anything for me anyway.
So... I looked at the layout to try to understand how it would send the info up/down. I did not see much except for one interesting comment:
// will trigger resize
This is said of the SetFixedSize mode. To reach that function you need to make the layout for update. Ah! Yes... the layout, not the parent widget... let's try that instead:
parentWidget()->layout()->update();
And voila! It resizes correctly in all cases I have. Quite incredible that the widget updateGeometry() doesn't trigger the same effect...

Although it's possible to do what you want it sounds like the problems you are having are because you're using Qt in a way that it's not meant to be used. Why do you need separate widgets for each key represented on the keyboard?
I see two options, both of which are better in some way:
Use QGraphicsScene and QGraphicsView.
A single custom widget that uses custom drawing to display the keyboard (and likely uses hover for hints).
The first option is probably better. Your keys could then be represented by QGraphicsSimpleTextItem's or even a QGraphicsSvgItem. It also provides a number of standard layouts or you could choose to write your own layout. By default you can use the keyPressEvent or mouseReleaseEvent to respond to user interactions.
I'd highly recommend you take a look at the QGraphicsView examples to get an idea what you can do.
If you go the second route you'll need to record the different key locations so you can respond accordingly as the user moves the mouse around, clicks, etc.

This won't help you with your immediate issue but I wanted to show you a keyboard I made using standard layouts and buttons. It's not perfect and it still won't help you with an enter key that spans two rows but it's not bad. It's resizable too by resizing the window, although I'm not sure if that will be apparent from the images below as SO may be scaling them. (you can view the actual images by opening them in their own tab)
Anyway, this was done using only Qt Designer with no manual coding. It consists of a top level vertical layout with 5 horizontal layouts in it. The buttons are then inserted into one of the 5 horizontal layouts. The size of the keys can be controlled by setting the horizontal and vertical size policies to "ignored" for most of the buttons and then horizontal "minimum" for buttons that you want to be wider. Things can be tweaked by setting min and max size restrictions to buttons. When resized, the buttons will not maintain their relative proportions though, that would probably take some custom programming.
The styling in your example could be approximated pretty well using css style sheets and background images. Still not a minor effort but you should be able to get most of the way there without custom layouts and buttons.

Related

QList view from bottom to top

I'm writing a messenger, and there is a message feed there. As you all know message feeds usually look like lists and go from bottom to top, the keep the scrolling positions anchored to the bottom when a new element is added, they grow upwards when resized and all kinds of fancy behavior like that.
So, I wanted to use QListView to make the feed, I have a model written elsewhere, it's absolutely under my control. But it doesn't seem like QListView supports this mode. There is a trick of how to do it with QML, but I don't have QML used anywhere else in my project and I'm not sure of how to interact with it, like how to set there my model, how to listen for signals or if it's going to behave well in my QGridLayout where I want it to place.
I can't believe no one ever faced the same situation. I'm trying to find the most standard solution, involving as little of my rookie code as possible, keeping in mind that Qt guys are much more experienced in layouting elements in list view and handling all corner cases. If I'm correct my options are
Use QML for the feed
Try to rotate QListView with QGraphicScene tricks
Reimplement QListView
Which of those you think is a better solution? Or, may be, someone knows some better way to do it?

qt on Mac: Form Layout fails to stretch horizontally

I'm still trying to do some pretty basic stuff with qt, and it's been a struggle. I'm specifically targeting for Mac. My current problem is getting the forms to not suck.
The simple problem. Create a new MainWindow app. Go into the Qt Creator (open Forms -> mainwindow.ui).
Drag 3 labels into place. Then I dragged 2 Line Edits and for fun, a Dial, but that part probably doesn't matter.
Click the main window and then tell it to use Form Layout. Within Qt Creator if I resize the window, my various line edits expand to fill the available space, exactly like I want.
Run the app. All the line edit fields are a fixed length, very short (if I don't override their minimum width), and do NOT resize as I resize the window.
Three quarters of the reason to use Form Layout is for resizing capability.
I've tried clicking on the central widget before setting to Form Layout: no change in behavior.
I do get reasonable behavior if I use a grid layout, although I have to add a vertical spacer at the bottom or my dial resizes crazily as I play with the window size.
So... Am I just doing something wrong with Form Layout? Or does it not work well on Mac and I should use Grid Layout instead?
I really miss Motif's XmFormLayout. It took time to set all the constraints, but I could make my forms do exactly what I wanted. Ah, but so 1990s.
On the panel to the right where you can see the objects, click the centralWidget. In the property panel, scroll down until you get to the layout properties of the form layout. Set the layoutFieldGrowthPolicy to ExpandingFieldsGrow.
By default, the line edits are not growing to fill the available space on macOS because they are not supposed to. This is some sort of macOS UI guideline. QFormLayout follows the platform conventions by default.
It would also be nice if I can figure out how to NOT necessarily expand everything to full width. I have a few Line Edit widgets that I'd like to keep short. They're going to hold TCP port numbers.
For those, set the Horizontal Policy property (it's grouped inside the sizePolicy property group in the properties panel) of the affected line edits to something suitable. In this case, probably to Fixed, but look up the documentation first of what each setting does.

Why a layout is required for multiple widgets recieving events?

I'm actually writing a module in qt (container) inerhiting from QWidget which is containing multiple plots presenting a graph where all of them inerhit from QWidget.
So it's given:
1 container can have :
n plots where each of them contains
1 graph.
I had yesterday a lot of time spent for figuring out a bug.
It happened that just the n plots which were (for debug purposes) added to the container in its constructor to catch the paint events.
All plots that were added by the same method while the container was constructed allready, were not able to receive any events.
later on, a coworker after I requested assistance explained me, that my container (which is located in the mainwindow-form) needs to get asigned a layout in the Qtdesigner. I gave it a try and was suprised that it did solve my problem. After adding the dynamicly generated plots to the container and aswell adding them to the layout, all widgets were receiving the events as expected.
But since I wasn't able to understand his explanation and don't want to bother him with it further, I'm asking it here.
So why dynmacially generated widgets which are child objects of other dynamically generated QWidgets require the parent which is a child of the mainwindow to have a layout asigned?
And if this is not just a exceptional case, why it isn't asigned by default and additional I wasn't able to find cases on the web, of tohers having such problems?
You're likely seeing the side effects of overlapping sibling widgets, or perhaps widgets having zero size. All that a layout does is resize them and ensure they don't overlap.
added to the container in its constructor to catch the paint events
What do you mean by "cathing" the paint events and what has that got to do with any constructors? You claim that all of your classes (the container, the plot, and the graph) derive from QWidget. They should all implement paintEvent, unless they are designed to have no content of their own other than a default background brush.
I imagine that you're somehow complicating things too much, but without self contained piece of code that we can compile to replicate your problem, we'll be going around in circles. Feel free to look around in my answers: they all consist of a single main.cpp, sometimes with quite a bit of functionality squeezed in. Almost universally, there's no code other than necessary to demonstrate a given technique. Your question should include similarly minimized, compileable example.

How to determine the content step value of scrollarea

I'm making my own UI from scratch using OpenGL that is why I'm asking this and please don't make any discouragement as this is just a hobby project.
Currently, I'm stuck implementing how this scrollbars really work. In my current implementation, the content scrolls at the wrong step value as well as the thumb, meaning, I set the value manually like 1px step for each of them.
The structure of my scrollbar implementation is describe as follows:
I draw scrollbars i.e the main rectangle where the 3 button lies.
Those 3 buttons are, thumb, buttonBack and buttonNext.
All of them do the basic logic of scrollbars i.e when I click each one of them, they moved. But the whole part(scrollbar) don't know how to scroll contents
So what I did is: I make another object and I call it scrollarea
It has two scrollbars, vertical and horizontal scrollbar.
I made a function called scrollToX and scrollToY which
does what I named to them.
But the step values I set to them are
manually set up.
I try to google some scrollbar, scrollarea, scrollview or whatever you call to that scrollable rectangle thing, but all I see are implementation and I cannot find any guides how to build your own. I have no choice but to look at their implementation. I try my best to comprehend what they did but their implementation of how their whole UI structure is very different to mine, and I cannot find anything useful there.
So I ask again here if anybody can explain me well how to make a properly functional scrollbar.
Most specific things I'm really concerned of are:
How do I determine the thumb step value?
How do I determine the content step value?
All of these depend on your content -
Is it just an image ? If so, you only need to change the offset depending on the size of the image.
Is it a list of values like in Windows explorer ? Then you need to create a data structure first that contains all of it, and shows the content that fits within the window as it scrolls.
OpenGL does not fit into this discussion.

Managing layouts in Qt with "empty" widgets for displaying images

I am designing a GUI in Qt that has placeholders for a number of images (images will be set and updated at run time) along with some input fields (QLineEdits with QLabels grouped into QGroupBoxes). I am using a technique similar to the answer here:
How do I make an image resize to scale in Qt?
for the image display widgets. Some are a class that inherits from QWidget that can draw an aspect-scaled image, others are a class that inherits from QLabel that can do the same and also display text. These are custom C++ classes.
The problem I am having is with the layout and getting things spaced correctly. I am using Qt Designer to layout the GUI. Since the image widgets are "empty" they don't exert a lot of "force" on the other widgets and the other widgets tend to dominate even though I don't need the other widgets to be that big. The QLineEdits especially like to be as wide as they can be. I want to GUI to be adaptable for different sizes (to accommodate different screen resolutions) so I don't really want to set a lot of sizes manually if I can help it.
I do not have a lot of experience in Qt and managing layouts. What I know I have mostly learned from playing around with it. I have tried searching for similar situations without much success. I can't figure out the terms to use for my specific situation. Does anyone have any suggestions for what I can do to better control the layout? Alternatively, are there any good resources for learning how to manage Qt's layouts? The Qt documentation I have looked at is fairly basic. There are a lot of options, most of which I am unsure exactly how they should work, and the trial-and-error approach is getting to be too cumbersome for more complex layouts.
I am using Qt 4.8.
Sorry to be vague. This is a work project and I would rather not post exactly what I have. And coming up with a generic example will take time. I think my questions are generic enough that it shouldn't matter. If a more specific example is needed, I can try to throw something together.
Edit:
Here is what the layout looks like in Designer right now. The black boxes are redacted labels. The orange boxes are where images will go. The one in the top-left corner is a particular problem. I currently have the top and bottom halves on the left in a splitter in an effort to make things fit better. I think that may be making it worse.
http://i.stack.imgur.com/nRRg3.png (long-time reader, first-time poster :-/)
Making the "What I want it to look like" picture is going to take a bit longer. It may be easy to see what I don't like. The input fields need to be much smaller to make room for the images.
Edit2:
And here's something closer to what I would like. The upper-left and right-hand images are larger and actually visible. I might like the boxes in the lower-middle to be even bigger, but there's only so much room. If I can get the layout right, I'm hoping that will optimize the size of everything for a given window size.
http://i.stack.imgur.com/yvLvi.png