QTableWidget grid doesn't fill the widget - qt

I have created a simple QTableWidget, set StretchLastSection to true on the horizontal header, which works fine.
It however looks like this:
As you can see there is a little spacing after the last cell so the grid border doesn't line up with the header.
How do I fix this?

Related

How to make a QTreeView fit in QHBoxLayout perfectly?

I am trying to have a QTreeView fit into a horizontal layout perfectly.
My layout setup looks like this:
I want the horizontal width of Vertical Layout 1 to be defined by the required widths of the QTreeView Column Headers. In other words the Vertical Layout 1 should stretch horizontally to make sure its width always exactly matches the width required by QTreeView column headers. Therefore the layout stretch factors of the topmost horizontal layout are set to (0,1) so that the Vertical Layout 1 does not stretch and Vertical Layout 2 does.
This is what it looks like in action:
The good news is that when QTreeView items are expanded, the headers also expand and in turn the Vertical Layout 1 stretches accordingly.
The problem is however highlighted on this picture:
For some reason there is redundant unused space within QTreeView itself, which has constant width in both collapsed and expanded states.
All the headers within the QTreeView are set to Resize to Contents, the Size policy of the QTreeView is set to 'AdjustToContents' and last header section stretch is set to False, which I think is correct:
self.accountsTree.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.SizeAdjustPolicy.AdjustToContents)
self.accountsTree.header().setStretchLastSection(False)
self.accountsTree.header().setSectionResizeMode(
AccountTreeColumns.COLUMN_NAME,
QHeaderView.ResizeMode.ResizeToContents,
)
self.accountsTree.header().setSectionResizeMode(
AccountTreeColumns.COLUMN_BALANCE,
QHeaderView.ResizeMode.ResizeToContents,
)
self.accountsTree.header().setSectionResizeMode(
AccountTreeColumns.COLUMN_BALANCE_BASE,
QHeaderView.ResizeMode.ResizeToContents,
)
self.accountsTree.header().setSectionResizeMode(
AccountTreeColumns.COLUMN_SHOW,
QHeaderView.ResizeMode.ResizeToContents,
)
I am using PyQt 6.4 and Qt Designer.
I tried playing around with stretch factors, layout constraints and different header section resize modes, but everything I tried only led to worse results. I don't understand where the extra space comes from, other than that the setup works exactly how I want it to. I must be missing some setting somewhere but I can't seem to figure it out.
Thanks for any tips

How to show several QTableViews(without scrollbar) in QListWidget(or other similar widgets)?

I have a problem with showing multiple tables(without their own scrollbars) under one scrollbar. Is there any workaround or a good way to resolve this issue in Qt?
I've tried to do what you ask, and found this. So, here is a solution:
add QScrollArea to a form
set the property widgetResizable to true
put QWidget to scroll area
right click on widget -> Set ancestor -> [your scroll area]
add vertical layout to a widget
scroll area will collapse, epand it with a mouse
insert into the widget as many tables as you want
set vertical size policy for each table to Minimum and set minimal vertical size.
Here is how it looks:

How can I exchange an item and a spacer?

In qt, I have a form that contains among other things, a group with
A combo box
a checkbox
a spacer
a button
Based on some logic, I want sometimes to show another combo box... Where the spacer is, but smaller.
When I add it though, everything resizes automatically
I don't see a way to make it invisible, and yet keep items of the same size when I make it visible again.
I tried making it fixed size... But unless I use fixed sizes and positioning for everything, which I think is a bad idea, the items still move around when I change visibility.
It seems silly... But how can I make my little combo box show up instead of the spacer not next to it ? Spacers don't seem to have a name...
I would do
combo.setVisible(condition);
Spacer.setVisible(!condition);
Very easy... Except how do I access the spacer from code ?
My suggestion is to use a container QWidget instead of the spacer. Here is how it will look:
A combo box
a checkbox
a widget-container
a button
Widget-container is a QWidget with fixed size. Put your combo-box there and it will maintain it's size when you show/hide the combo-box.
Regarding your question (You will not need it but just to know in the future):
how do I access the spacer from code
You can create a spacer from code like this:
QSpacerItem* spacer = new QSpacerItem(0, 15, QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addItem(spacer);
...
Also you can get it from a layout if you know its index:
QLayoutItem* item = layout->itemAt(index);
But there is no such method as show/hide for layout items.

Qtableview resize

I have several qdockwidget and within these a qtableview, as seen in the attached picture, my problem is I want the tables are the same size of the dock.
I tried to do this and it does not work:
table-> resize (dock-> size());
By default, the cells in a table do not expand to fill the available
space. You can make the cells fill the available space by stretching
the last header section. Access the relevant header using
horizontalHeader() or verticalHeader() and set the header's
stretchLastSection property.
Source: QTableView Documentation
I hope that helps you. Cheers.

qt unexpandable layout?

Ok, here is my problem:
I have a vertical layout which contains a QPlainTextEdit and a horizontal layout (containing 2 QPushButtons) below the text edit.
The vertical layout is just a part of GUI, and gets resized depending on screen resolution. Btw. it is a mobile app, so I don't have a lot of space on screen.
Push buttons have some text which is dynamically set, I don't know it from the beginning to code it manually.
My problem occurs when the text in push buttons is big, and my whole vertical layout is expanded to fit the buttons.
How can I make the vertical layout unexpandable? note, that this is different from "fixed" because of different screen resoulutions.
I'd just like the clip the buttons if they do not fit, but keep the layout width untouched.
Anyway to do this?
You'll need to set the maximum width for the buttons, not the layout, which is only widening to fit the wider buttons. Check out the docs on QPushButton and look for QWidget inherited functions called setMaximumSize or setMaximumWidth.
You can always GetWidth() on the button when it is an appropriate size, then setMaximumWidth using that value since you wouldn't ordinarily know this. Pick an appropriate default text size/val and use that to create your "dynamic" default since this is going on screens of varying size.

Resources