I have QListView with custom implementation of QItemDelegate. MyItemDelegate reimplements createEditor() to show custom widget. Size of widget is dependant on content.
By default, each row height is about 20px (one row), but my editor has bigger height.
I was trying to override QItemDelegate::sizeHint() method, but is doesn't contains reference to editor, so I couldn't calculate correct size.
How can I make QListView resize rows to actual size of editor?
You should emit layoutChanged after creating an editor, if you could not override sizeHint correctly. But it should be enough to override sizeHint.
Related
I have a simple QStackedWidget with 3 different QWidgets in it. The minimum sizes of the QWidgets are (350x200), (200x100), and (450x450).
So the problem I'm having is when I resize the QStackedWidget, it has a minimum size of the largest QWidget within it. So if I switch to the second QWidget, (which is the QWidget with the size of (200x100)), the QStackedWidget will only size down (450x450) because of the largest QWidget inside of it. I would like it to size down to fit the current QWidget being displayed, and remove that minimum size.
I think that the most straightforward solution is to subclass QStackedWidget and override sizeHint and minimumSizeHint to return the hint for the current widget instead of the maximum of all widgets. For example:
class StackedWidget : public QStackedWidget
{
QSize sizeHint() const override
{
return currentWidget()->sizeHint();
}
QSize minimumSizeHint() const override
{
return currentWidget()->minimumSizeHint();
}
};
Good question. Unfortunately Qt doesnt provide automatic mechanisms for sizing down depending on child widgets (By automatic I mean you don't have to do anything). Most of the focus is on expanding (see the size policies)
You have two options :
Use the signal and slots mechanism in the class which create those widgets. You will need to listen to the signal void QStackedWidget ::currentChanged ( int index ) and resize the stackedwidget to the size of the widget at index. This is quite fast to code.
Decorate QStackedWidget and define the size properties. Basically both sizeHint() and minimumSizeHint() should return the size of the current widget. Addwidget(QWidget*) also need to be modified. Useful if you are using stacked widgets everywhere.
step 1: overload method:
void resizeEvent(QResizeEvent*)
step 2: call 'resize' and 'select page' :
QRect rect;
rect.setBottomRight(stackedWidget->geometry().bottomRight());
currentWidget->setGeometry(rect);
An alternate solution is to not use stacked widget at all and instead make one Widget for each page and then use the setVisible(bool) to show and hide the pages. That way you get the resizing behavior without having to make custom widgets.
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.
I'm a bit new to QT but have to work on existing code. Here's the case:
I have a class extending QDialog. the constructor sets a QGridLayout then adding three other widgets to it. One of the widgets is a QScrollArea containing a QGroupBox. this QGroupBox has a QVBoxLayout and there I'm adding a list of widgets at runtime. The size of the scroll area should grow until a given limit is reached before showing the scrollbars so that they are only used when the dialog would grow too high. I've found that the sizeHint of the outer layout doesn't update when the sizeHint of the scroll area updates. How can I refresh this, or is there a better way to resize the parent dialog?
What about using widgetResizable property of QScrollArea? It should try to resize view to avoid using scorllbars.
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.
I have a QDialog I'm working with. It is made somewhat like a QMessageBox. I noticed that the size of the QMessageBox (and the size of its label) depends on the size of the message displayed.
How would I make the size of my QDialog adjust automatically like a QMessageBox? Presently my QDialog contains a button box and a label, and the QDialog is layout Vertical.
(I know I could just use the message box directly but eventually I will have more complex dialogs.)
Automatic solution:
Use layouts and set size policies to QSizePolicy::Expanding. In QtDesigner, once all your children are placed on your QDialog, then click on the Adjust Size button next layout ones. Your QDialog will be automatically resized at runtime.
Manual solution:
The QWidget class has a method adjustSize that resize the QWidget to fit its content. Just call it when all children are set.
Set your dialog to be expanding, and very small. Then, be sure to set your message before showing the dialog. When shown, it will try to find its proper size, based on the size of the objects it contains. (This happens recursively, so if the dialog isn't the direct parent of the label in which you show your message, make sure everything between the label and the dialog is set to use layouts.)
A TIP : if you try to use "adjustSize()" function when dialog is hidden, it may not be works fine. It would be better to use it after the "show()" function.