Setting QTableView width depends on model columns - qt

I have a custom QAbstractTableModel for my data and the model currently contains fixed number of columns (12). I also have a custom QTableView to display this model. When I added this widget to my dialog, it always clapped the last few columns like this
I use standard layouts (QFormLayout, QVBoxLayout) for adding widgets to the dialog and I haven't specify minimumSize() for my widgets, hoping the layout engine to calculate the best for me.
So, how do I setup the model class / QTableview class so that it will automatically expand to show all the columns? Or how do I make the minimumSize of my tableView depends on the width of table columns?
(I don't want to hardcode the pixel values for the windows, as whenever the columns changes, I will have to adjust the values again manually)

As you can see, scroll bars are inside your table, not outside of it. QTableView extends QAbstractScrollArea, which creates them, when content does not fit into viewport. Minimal size of viewport is controlled by method QSize QAbstractScrollArea::maximumViewportSize () const (which is not virtual, by the way).
I think, the best way would be to save QWidget::saveGeometry() (is it QMainMindow?) and QTableView::horizontalHeader()->saveState() in QSettings in your widget's destructor, and resotre them in constructor.

Related

Resizing Layout equal to MainWindow

When I run my program it will display all content properly, and when I resizing the main window, the layout along with all associated widgets remain fixed, rather than resizing with the main window. I used to increase my all widget and listWidget respect to window computer resolution size but still this not one work properly.
I used this one code finding the system height and width.
QWidget widget;
widget.resize(widget.width(), widget.minimumHeight());
QRect rec = QApplication::desktop()->screenGeometry();
int h = rec.height();
int w = rec.width();
// Increasing the listwidget size
ui->listWidget->setFixedHeight(h);
ui->listWidget->setFixedWidth(w);
//increasing the button size
ui->pushButton->setFixedHeight(h0.2);
ui->pushButton->setFixedWidth(w0.2);
At this link you will find two screenshots that illustrate my problem.
Please resolve to solve my problem. Thanks very much in advance.
When defining the layout of your windows and forms in Qt Designer you have to define each element of your form in advance, in order to have a working layout.
This solution is based on the screenshots provided in the comments to the question. Follow these steps:
Add an empty widget to the central area of your form, if there is nothing there. It will be used as a placeholder for the controls you will add later, and of course you can replace it with whatever widget you want. But you need it there to define a proper layout.
In the property panel, set the horizontal QSizePolicy of this widget to MinimumExpanding.
Add an horizontal spacer to the left side of your progress bar.
Define a minimum/maximum width for the white widget on the left (I guess it's a text area). As an example set the maximum width to 200
pixels.
Make the same for the QTabWidget on the right.
Give a minimum height to the Groupbox on top.
Then give the grid layout to the MainWindow.
You should get something similar in the designer view (I use a dark theme, yours will have different colors of course):
If you complete all steps you should have a nicely resizing window.
For the future: remember to integrally define your layouts, also using placeholder widgets when needed, read carefully the documentation about the widgets size policies (there are several, you need to play with them to fully understand each one) and keep in mind that Qt uses a container based approach which is different, as an example from those used by the .Net framework that relies on the concept of anchors.
EDIT : to answer questions in the comments
You will need to add a layout to any widget that contains other widgets, e.g. adding controls to your groupbox will require to give it a grid, horizontal or vertical layout in order to scale nicely on resize. Again use spacers and size policies to make it look the way you want. If you need to add or remove controls, or change their positions, you may need to brake the layout, rearrange and then set it again.
You can also select groups of widgets and give them a layout e.g. vertical, than another group and set them horizontal and so on... then give a grid layout to the container widget to build a compound layout.
There are endless possibilities, you just need to practice and go through trial and error as for everything else...
You can also do it all programmatically, check the Qt widgets documentation for this. But for complex layouts I would not go that way: it's a lot of code... and you have to compile and run to test every modification.
Using the QtCreator, within the designer you can simply right-click on the parent-widget and add a Grid-Layout.
This one resizes it's children to it's dimensions.

Qt layout not expanding

I have a Qt Widget which has a frame, containg the rest of the widgets.
I only want one of the widgets to expand on maximazing the window, so I've set a max value for the others.
But it will only expand if I set a grid layout to the frame, which messes up the place of the widgets.
How can I solve it?
Do you use any kind of layout ?
Size policy only works if the parent has a layout set.
You can use a combination of different layouts to obtain the desired look, otherwise you will have to calculate the size of the 'expanding widget' yourself in the resize event method of the parent.

Resizing QListWidget's dimensions to fit its contents

I have a subclass of QListWidget, the widget holds text items in a single column and multiple rows (the usual kind). I want the widget to resize itself to the minimum size where the text items will still be visible. I've tried using the resize() method with the contentsSize() argument, this will resize the widget's height to fit the text contents, however the width stays the same.
Here's a snippet of an overriden method that I'm testing this with:
override void mousePressEvent(QMouseEvent event)
{
this.resize(this.contentsSize());
}
Note: This is in the D language, and I'm using the QtD wrapper library. Unless I'm doing something wrong it might even be a QtD bug (but I doubt it).
If you're content to switch to a QTableView or QTreeView, you can call resizeColumnsToContents(), and resize your widget based on the resulting width. Otherwise you'll have to iterate over your QListWidget contents and get the maximum of the widths of the items.

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.

QTreeWidget set height of each row depending on content

I want to make editable cells with multi-lines content in QTreeWidget and I use for this purpose QPlainTextEdit as a delegate. I need to set proper size to all rows that switching between editing and displaying went smooth, without any visible changes.
rect = textEdit.blockBoundingRect(textEdit.firstVisibleBlock())
With this I can find out the height I need to set for the row, but I missing the place where I can do it.
How can I set proper height to QTreeWidget's rows on initialization stage and how to handle it's changes?
You need to reimplement delegate's sizeHint(). It will automatically handle row's height and width.
And note, that QTreeWidget::uniformRowHeight property must be false in this case, though it will slow tree element rendering if it contains many rows.

Resources