Qtableview resize - qt

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.

Related

Why the layout in qt avoid to show full image of background image for pushbutton

I have problem with layout. I put image for QPushButton background but when I add my button to each layout, it avoid to show full image.
Use size Policy and minimum Height-width. for example you want to expand the button. first select the button and after that choose size Policy to change the size. select Expanding for each property(vertical|horizontal Policy) you want to change. Also you can set the vertical|horizontal stretch. if you want to set limit for it's size you can set minimum and maximum size for your button.
Just look at your object properties, Also look at here. :)

How to delete the space between QLabel and QLineEdit

I have a QDialog window like this and I want to delete the space between 'Length', 'n', 'm' and corresponding QLineEdit input boxes. How can I achieve that ?
If you use gridlayout, I am not sure why your output looks like that. Generally Qt will not leave huge empty space like that, There are three possibility I can think of:
You have many SPACE after Length:, M: or N:
The layoutHorizontalSpacing is too large in your grid manager.
The layoutColumnStretch was set to in favor of label in your grid manager, should be "0,0", not "1,0". I mean, stretch of Label should not be higher than lineedit.
Still, I would use a simple form layout in your application.
All you need to do is reset the alignment of the labels:
label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlighVCenter)
This can also be done in via the Property Editor in Qt Designer.
You have to decide what to do with the space you want to remove :
Reduce the whole widget : it depends on the surrounding layout, adding an horizontal layout on right or left of it will put the space out of your widget.
Space on the left of your labels : you can align the label text to the right as ekhumoro suggested, or directly reduce and align to the right the whole frame by adding an horizontal spacer on its left (in the surrounding layout).
Space on the right of your line-edits : like above you can add horizontal spacers or reduce and align the frame.
Expand the line-edits : remove their fixed width (default horizontal policy is expanding) or set a bigger one.
The point is : the space has to be somewhere except if you reduce the parent widget or enlarge some the inner widgets. Size-policy is useful to tell which widget should take the available space, spacers are useful to let empty space between widgets.

QTableWidget auto stretch last field just like QTreeWidget does

Is there anyway to let QTableWidget's header items stretch to full size just like QTreeWidget does ?
From the QTableView documentation:
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.
You should give that a try.
QTableWidget *tw = ...;
tw->horizontalHeader()->setStretchLastSection(true);
The stretchLastSection documentation has:
Note: The horizontal headers provided by QTreeView are configured with this property set to true, ensuring that the view does not waste any of the space assigned to it for its header.
So that's how the tree views do it.

Enable QLabel to shrink even if it truncates text

How can I get a QLabel to be resized even if it means truncating its containing text? I have a QLabel stretching the whole horizontal space of a Widget. When setting its text I make sure it is correctly truncated, ie getting its FontMetrics and Width and using metrics.elidedText().
But when the user resizes the widget the Label doesn't allow it to shrink any further since it would truncate its text.
Any ideas how to solve this? The simplest solution I think would be to somehow tell the QLabel to always shrink and then catch the resize event and correctly format the text - I just have no idea how to do the first part (different size policies don't help)
Although you mention that setting size policies didn't help, setting the QLabel's horizontal size policy to QSizePolicy::Ignored should tell the containing layout manager to ignore any minimum size hint from the label. An alternative would be to set the QLabel's minimum horizontal size to a non-zero value, like 1. If neither of these work then there is something else interfering.

How to set default height on QTableWidget

I have a widget which I'm putting in a QVBoxLayout. This widget's layout is a QVBoxLayout which includes a QTableWidget. When I display this everything is fine but the QTableWidget only shows a few rows. How can I set the height of the table to a decent value (like 20 rows) while still allowing the table to resize?
I've tried calling table.setMinimumHeight(200) but then the table can NEVER be smaller than 200. I've also tried setting the container widget height using setMinimumHeight but this has the same problem.
Check the sizePolicy for the QTableWidget. This will have horizontal and vertical size policies which, if I understand you correctly, should be set to "expanding" or "minimum expanding". There are a number of options and combinations for these and sometimes it can be tricky getting the right combination for all the widgets in your layout to get what you are looking for.
The size policies will work in combination with your min/max height/width settings and those of the other widgets in the layout.

Resources