I have a QHBoxLayout horizontal layout with a lot of list widgets added to it.
And although I call setMaximumWidth(300) and setMinimumWidth(300) for the list widgets, once they don't fit on the window, they start to shrink.
I would like to have a scroll bar instead. Is this possible?
Yes, if you put the Layout inside a parent widget, and that parent widget inside a QScrollArea.
QScrollArea Documentation
Related
I have a QDialog like this:
I have added a QScrollArea to this QDialog like this:
What I want is When the user expands QDialog , the QScrollArea should also expand with it.
I have tried several properties of QScrollArea like verticalScrollBarPolicy
horizontalScrollBarPolicy, sizeAdjustPolicy, but nothing seems to work.
Can someone suggest how to accomplish this?
For the dialog to resize its child widgets, it must have a layout. Set a layout on the dialog. Do not change any of QScrollArea's properties: they only affect the area itself and its children, and have nothing to do with how the parent widget (the dialog) might manage the scroll area's geometry().
I have a simple qt application with a QTabWidget inside the main window. I also have a few QPushButton(s) and QRadioButton(s).
What I want is that when I resize the window either manually or by maximizing/minimizing it should resize the containers in the same way.
In other words, what I want is equivalent of DockStyle.Fill in qt C++
How can I do that ?
In Qt you have to use Layouts:
The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that
they make good use of the available space.
In short, all components in a layout will be relocated to new places after the window, to which the layout belongs, is resized.
If you are using deisgner:
1. Click the empty space of a widget to select itself(or a main Window, I use just a base widget here for demonstration), and the layout option will be hightlighted:
2. Choose a desired layout
Here is what object monitor looks like after a QVBoxLayout is used:
If your widget doesn't use layout, it will look like this:
What we have done here is to make the base widget/mainWindow equip a main layout. You can see that the buttons are automatically aligned, when you resize the widget, those component will be relocated according to the layout:
Perhaps you will find it nettlesome of those expanding space, so the next move is to add a Spacer to the layout; so when layout is resized, only the spacer will stretch.
(Another option is to make your widgets expandable, see ** at the end of this post)
3. Besides, you can add a layout into another to create a nested layout
For example, first I choose A and B (by pressing Ctrl) and use QVBoxLayout. This additional layout is not base layout and hence highlighted by red rectangle.
Then I choose C and the layout which contains A & B, and use QHBoxLayout on them,
Finally I use another QVBoxLayout as my main layout on the base widget, just like what we did previously.
And the object monitor:
If you like the special feeling of hitting keyboard and always handcraft the code:
For the last example:
QWidget *Form = new QWidget;
QPushButton *pushButton_A = new QPushButton("A");
QPushButton *pushButton_B = new QPushButton("B");
QPushButton *pushButton_C = new QPushButton("C");
QVBoxLayout *verticalLayout = new QVBoxLayout;
QHBoxLayout *horizontalLayout = new QHBoxLayout;
QVBoxLayout *mainLayout = new QVBoxLayout;
verticalLayout->addWidget(pushButton_A);
verticalLayout->addWidget(pushButton_B);
horizontalLayout->addWidget(pushButton_C);
horizontalLayout->addLayout(verticalLayout);
mainLayout->addLayout(horizontalLayout);
Form->setLayout(mainLayout);
Form->show();
In your case
Here is an example of layout:
Notice that QMainWidget has a centralwidget as a base widget. Besides, each tab of QTabWidget has it's own base widget (tab and tab_2 in the picture) which adopts another base layout.
*Don't forget to add Spacer in layouts to shape them as you like.
** You can set size policy on each widget (QTabWidget, QPushButton etc) to make them horizontally/vertically expandable or fixed, this cooperates with the layout strategy. For example, in the very begin example if we set
button A to be vertically fixed, horizontally expanding
button B to be vertically expanding, horizontally expanding
button C to be vertically expanding, horizontally fixed
It will look like this when resizing:
you need to look into how to use layouts in your application
http://qt-project.org/doc/qt-4.8/layout.html
As a quick and simple first try, in the Designer you can right-click on the main window, and choose "layout" from the drop-down menu. Here you can pick the grid layout, for instance.
Is there a way to draw Widgets on QGraphicsView instead of QGraphicsScene so that the widget stays in position when the scene is moved?
I want to create some dialogs that are dockable inside the workspace like this:
http://www.thebandfrom.com/wp-content/uploads/photoshop-ui.png
You can use the addWidget function of QGraphicsScene, and then set the QGraphicsItem::ItemIgnoresTransformations flag to the added QGraphicsProxyWidget.
QGraphicsProxyWidget* proxyWidget = scene->addWidget(myWidget);
proxyWidget->setFlag(QGraphicsItem::ItemIgnoresTransformations);
You can add widgets onto the QGraphicsView directly by setting the QGraphicsView as their parent. You could also add a layout so that when the QGraphicsView is resized, your widgets arrange themselves appropriately.
I am new to QT and I am creating a widget that has a gridlayout. The gridlayout contains a matrix of QLineEdit widgets. The window resizes to fit the layout but when layout is large it goes off screen. When I maximize the screen, the QLineEdit widgets are resized to fit the screen and for large layouts they become extremely small.
I want to be able to resize the window without resizing the QLineEdit widgets and add scroll bars to navigate.
I tried the following with no luck:
Window->resize(QSize(500,500));
QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidget(Window);
where window is the widget containing the layout. Also, the window closes when after executing "scrollArea->setWidget(Window);" and I dont why.
If someone can help me out I would really appreciate it.
Thank You!
For disabling the vertical resize on the widgets, why don't you just use the setFixedHeight() method on the widgets?
For the menu bar, why don't you take it out of the widget that is scrollable. You can have a layout for the window that contains the menu bar and then the widget that contains everything else (scrollable part). Is that what you are looking for?
I fixed my problem by creating a QMainWindow with the menu bar. Then created a widget which includes the layout, set the Scroll Area to the widget. Finally set the central widget of the main widow to the scroll area.
I have a button followed by a QGridLayout full of widgets.
I want to show/hide QGridLayout at every button click, but reading documentation of QGridLayout I see there's no show()/hide() implementation, also no setVisible() method available.
How do I achieve this?
Layouts only affect the size/position of the widgets added to them - for visibility (and anything else - event handling, focus, enable+disable) you care about the parent widget, as mentioned above. QLayout::parentWidget() gives you the widget which owns the layout, which you can then show and hide.
You didn't mention which version of Qt you're using. (I'm looking at the 4.4 documentation.)
I haven't tried this, but here are two ideas:
QGridLayout inherits the function QLayoutItem::widget(). If your layout is a widget, this will return a QWidget* on which you can call show() or hide().
If your QGridLayout is not a QWidget, you can nest it within a QWidget, and you can show() / hide() that widget instead.
I assume you have multiple QGridLayout instances, only one should be visible based on the button that has been clicked. You can use a QStackedWidget for this:
The QStackedWidget class provides a
stack of widgets where only one widget
is visible at a time.
Then, for each widget in the QStackedWidget you should associate a separate QGridLayout.
See the Qt documentation for more details