Let's consider we have QWidget that contains QTableWidget (only). So we want to resize the table by resizing the widget, and we dont want to have an indet between the QWidget border and cells of the table. What kind of property is making posible for QTableWidget to be aligned with the borders of it parent widget?
Thanks.
First, you want to make sure the QTableWidget is placed inside a layout. For instance,
QTableWidget* tw = new QTableWidget(parent_widget);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(tw);
parent_widget->setLayout(layout);
assuming parent_widget is already pointing to the widget containing the QTableWidget. This will ensure that the table resizes when the parent widget does. To have the table fill the entire space of the widget, just set the margin to zero on the layout. Try one of these:
layout->setMargin(0);
or
layout->setContentsMargins(0,0,0,0);
Related
In a custom QWidget (say MyWidget) I include a layout to manage children widgets:
MyWidget window;
QPushButton button;
QVBoxLayout layout(window);
layout.addWidget(button);
window.show();
I would like the layout to be in a specific position and size, by default, QWidget set it to the whole geometry.
How can I set the geometry the layout will consider for managing his space?
As an indirect question:
Which function the layout use to set children geometries?
QPushButton *button = new QPushButton;
verticalLayout->addSpacing(300);
verticalLayout->addWidget(button);
button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
button->setMinimumSize(500, 200);
button->setMaximumSize(500, 200);
If you need both vertical and horizontal spacing - use QGridLayout.
The QLayout use as margins the summary of both:
The parent widget contents margins.
The layout contents margins.
The solution is to set the contents margins to the required value in the custom widget constructor. And when creating the layout, to set his contents margins to 0.
this->setContentsMargins(left, top, right, bottom);
// ...
layout->setContentsMargins(0,0,0,0);
The Layout setGeometry is called by QWidget resize event, and set children widgets size according with it. As all functions are not virtual, it is not possible (or at least difficult) to modify in this behavior.
I have a QWidget that I want to include within a scroll-area so that when the designated QWidget size is exceeded vertically, the user can scroll up and down to see more.
QWidget renameWidget;
QScrollArea scrollarea.
How do I go about doing this? I set the widget inside the scroll-area on the UI editor but it didn't work.
Any ideas?
Thanks.
Think of QScrollArea as another layout. Add the scroll area to your main widget and put everything else inside it with setWidget().
QScrollArea is QWidget, so you can even use it as a top level widget:
QScrollArea *scrollArea = new QScrollArea();
scrollArea->resize(250, 250);
QWidget *widget = new QWidget(scrollArea);
widget->setBackgroundRole(QPalette::Dark);
widget->resize(200, 200);
scrollArea->setWidget(widget);
scrollArea->show();
QScrollArea provides a scrolling view onto another widget. It is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed.
An example:
QScrollArea *scrollArea = new QScrollArea(this);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(renameWidget);
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.
I have a MainWindow with a QToolbar, QWidget and a QTabWidget. The layout is "Grid". However, my window is resizeable and since I have a layout it works well. But there is one problem, in my QTabWidget I have a QTableWidget with two columns (layout is also "Grid"). If I resize my whole window the QTableWidget resizes but not the columns.
For example Whenever I resize my window, my QTabWidget resizes and the QTableWidget in it too. Only the columns in my QTableWidget won't.
So... how can I resize them if my QTableWidget resizes?
Change the ResizeMode of the QHeaderView. For example, use:
horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
to make the first column resize so the QTableWidget is always full.
Override the resizeEvent and set the widths of each column yourself when the QTableWidget has been resized.
To stretch last column:
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
To stretch column #n:
ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
The best solution for this, in Qt5 you have to use setSectionResizeMode instead of setResizeMode
tabv = QTableView()
tabv.horizontalHeader().setSectionResizeMode(QHeaderView::Stretch)
Also you can specify the Stretch mode when resizing
tabv.horizontalHeader().resizeSections(QHeaderView::Stretch)
ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
If you want to resize just the last column:
ui->tableWidget->horizontalHeader()->setStretchLastSection(1);
You can change the "resize mode" of your columns or rows with the QHeaderView and the method QHeaderView::setResizeMode().
http://qt-project.org/doc/qt-4.8/qheaderview.html#setResizeMode
http://qt-project.org/doc/qt-4.8/qtableview.html#verticalHeader
http://qt-project.org/doc/qt-4.8/qtableview.html#horizontalHeader
In Qt5 you have to use setSectionResizeMode instead of setResizeMode
QTableWidget* myTable = new QTableWidet;
QHeaderView* header = myTable->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);
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