How does QWidget size when used as a window? - qt

Several examples on trolltech use QWidget rather than a QMainWindow as the main window for simple Qt applications.
Now I need just a simple window with a single content widget (QGlWidget) that covers the entire client area.
However, when I show a QWidget window with a single content window it automatically resizes itself to be tiny.
If I create the QWidget parent window without a child It is a nice large default size.
I don't want to resort to using Layouts for a single child window :/

What I understand is that you use a QWidget to display your QGIWidget. Try calling the show method of your QGIWidget directly (if your QGIWidget class inherits QWidget), Qt will create the window decoration for you.
Otherwise if you really need your widget to be inside one another, and fit its size, you'll have to use a layout.

Either follow gregseth's advice or you can simply resize the widget yourself.
(though this way you'll loose nice auto-resizing which is provided by Qt when you use layouts)
In your case you can basically do something like:
QGlWidget* myWidget = new QGlWidget;
myWidget->resize(QApplication::desktopWidget()->availableGeometry().size());
// or maybe instead of resizing show it in fullscreen:
myWidget->showFullScreen();
(actually I don't remember if showFullScreen() will do resizing for you, maybe you'll need both resize+showFullScreen :))
Cheers :)
P.S. Using layout is actually an option. It's not expensive and it's flexible. All it gets: "layout = new QVBoxLayout(myWidget); layout->addWidget(myWidget);" and you're done :)

Not always.
I've found that a QMainWindow will not work as the parent widget when using the QVBoxLayout and QHBoxLayout to arrange child widgets.
If you create a QWidget and use that in place of the QMainWindow then the layouts will work correctly.
QWidget* centralWidget = new QWidget( MainWindow );
MainWindow->setCentralWidget( centralWidget );
If you use QtCreator and look at the code it creates you can see it creating a 'hidden' widget if you try to use the layouts directly at the top level.
It's not obvious, intuitive, or documented anywhere that I've found.

Related

How can overlap QPushButton without applying layout on QGridLayout?

How to place independently QPushButton without being affected by QGridLayout using Qt Designer if possible?
You can always remove a widget from layout. But you have to take care of its geometry..
use removeWidget(QWidget *widget).
Refer
http://doc.qt.io/qt-5/qlayout.html#removeWidget

QT: two layouts add the same widget

In Qt: I create a widget-ui class, and I want to make the widget appear in two different layouts in two separate base widget(or window). So I want to:
widget_based_class* inside = new widget_based_class(base_widget1);
QHBoxLayout *lay1=new QHBoxLayout(base_widget1);
base_widget->setLayout(lay1);
lay1->addWidget(inside);
base_widget1.show();
-------------------------------
base_widget1.hide();
QHBoxLayout *lay2=new QHBoxLayout(base_widget2);
base_widget->setLayout(lay2);
lay2->addWidget(inside);
base_widget2.show();
How cound I achieve this? (My program is more complicated, and I didn't see the code work.)
Tank you.
A QWidget has only one parent widget and only one geometry (position and size) in that parent. Every call of QLayout::addWidget() will reparent that widget to the widget, the layout is installed on.
Your second call of setLayout won't work as expected, because you have to delete the the existing layout manager before setting the new one:
delete base_widget->layout();
base_widget->setLayout(lay2);
If base_widget hasn't already got a layout manager, the layout manager lay2 would simply be reparented.
If the widget will never be displayed twice on the screen, I don't see why you can't reparent it by addWidget/removeWidget.
In the OP, the parent widget/window is always hidden before the other one is shown. addWidget is called on the fly. We should also call removeWidget on the fly. It should be possible to move the widget around.
widget_based_class* inside = new widget_based_class(base_widget1);
QHBoxLayout *lay1=new QHBoxLayout(base_widget1);
base_widget->setLayout(lay1);
lay2->removeWidget(inside); // remove widget from other layout
lay1->addWidget(inside); // add widget to this layout
base_widget1.show();
-------------------------------
base_widget1.hide();
QHBoxLayout *lay2=new QHBoxLayout(base_widget2);
base_widget->setLayout(lay2);
lay1->removeWidget(inside); //remove widget from other layout
lay2->addWidget(inside); // add widget to this layout
base_widget2.show();
Make one widget and use a pointer in each layout?

Resizing a QDialog after adding components to a child widget

I'm a bit new to QT but have to work on existing code. Here's the case:
I have a class extending QDialog. the constructor sets a QGridLayout then adding three other widgets to it. One of the widgets is a QScrollArea containing a QGroupBox. this QGroupBox has a QVBoxLayout and there I'm adding a list of widgets at runtime. The size of the scroll area should grow until a given limit is reached before showing the scrollbars so that they are only used when the dialog would grow too high. I've found that the sizeHint of the outer layout doesn't update when the sizeHint of the scroll area updates. How can I refresh this, or is there a better way to resize the parent dialog?
What about using widgetResizable property of QScrollArea? It should try to resize view to avoid using scorllbars.

Qt QLayout inside QDesktopWidget

I want to do universal method to set position of my widget.
All I wanna get it is set right coordinates for my widget wich must always be in right bottom corner of desktop. My widget can change his height (or maybe width) but it must have adjusted size by both ordinates... (too many words)
My idea is using QDesktopWidget as basic widget to put into my QLayout with stratch items (to align inner (my) widget to right and to bottom sides)
my code prototype is here:
QDesktopWidget * desktopWidget = QApplication::desktop();
MyWidget * myWidget = new MyWidget(desktopWidget);
QVBoxLayout * vlayout = new QVBoxLayout;
vlayout->addStretch();
vlayout->addWidget(myWidget);
QHBoxLayout * hlayout = new QHBoxLayout(desktopWidget);
hlayout->addStretch();
hlayout->addLayout(vlayout);
but it doesn't work...
Help me please implement my idea if you know how.
At this moment I know only one work way to do it - it is manually set pos of widget and handle many events (resize etc.) - but this is not good... (because i do it bad of cause ;-)
)
PS: idea with qlayout inside other widget is working for example with QTextBrowser with sandclock at certer of view, etc.
A QDesktopWidget isn't intended to be used like a typical widget (at least as far as I'm aware, I'm surprised the documentation isn't more explicit about that). So you shouldn't try to parent widgets to it or try to assign it a layout. You call its methods to obtain information about the desktop environment or connect to its signals to be informed of changes.
Using this information, you would then set the geometry of your own application widgets so that they appear on the correct screen and position you want.
This page shows some basic functionality.

How to anchor QGraphicsWidget/Item (make them static) on QGraphicsView

I want to make something like a hud. I have a QGraphicsScene with a huge QPixmap, that I added to a much smaller QGraphicsView. Now I need to add some control elements, like QLabel, QPushButton and QGraphicsEllipseItem. That's, I think, is not a problem. I found this helpful page Graphics View Classes. But how do I anchor this control elements to the View or maybe better said how do I not anchor them? When I drag the QPixmap, then I don't want that the hud is moving with the QPixmap out of the QGraphicsView.
What do I need to do, to solve this problem. Hope someone can help me.
You should just have to create your widgets and specify the QGraphicsView in the constructor like this:
QPushButton *button1 = new QPushButton(view);

Resources