I am trying to set the minimum height of the rows in the QComboBox dropdown menu without changing their width or the size of the QComboBox itself.
By default the width and height of the row items are calculated by its data. To make the dropdown list resize to the width of the data I am calling
view()->setMinimumWidth(view()->sizeHintForColumn(0);
in the combobox's ::showPopup().
However, since I have different font sizes in rows, I would like to enforce a minimum height on each row.
I tried to use setSizeHint on each QStandardItem that is added in the first column to the rows but without success. I tried:
item->setSizeHint(QSize(item->sizeHint().width(), minHeight));
(https://stackoverflow.com/a/10749345/1981832)
But this lead to the dropdown menu not resizing to the data's width.
So, I tried to leave the width "unset". However, both
item->setSizeHint(QSize(QSize().width(), minHeight));
and
item->setSizeHint(QSize(0, minHeight));
did not work. Does anyone have an idea how I can enforce a minimum height on the rows of the QComboBox without messing up the automatic width calculation?
Just use this lines:
QComboBox combo;
QListView *view = new QListView(&combo);
view->setStyleSheet("QListView::item{height: 100px}");
combo.setView(view);
Or write this code in qss file:
QListView::item {
height: 30px;
}
After that use:
QComboBox::setView(QAbstractItemView *itemView)
Related
I am using a QTableWidget with 4-5 columns in a dialog. The dialog is resizable, I want table widget columns to resize according to dialog size i.e. if I increase dialog width, columns which are initially set with large width should expand more than the columns which were set with less width.
In short, I want relative resizing like column1 should occupy 20%, column2 occupy 50% of my table width (which increases with dialog width) and so on.
How this can be achieved for QTableWidget in Qt ?
Any solution, pointers or hints would be very helpful.
It should just be a matter of updating the column widths whenever your dialog resizes.
MyDialog::resizeEvent(QResizeEvent *event) {
int width = ui->tableWidget->size().width();
ui->tableWidget->setColumnWidth(0, width * .2);
ui->tableWidget->setColumnWidth(1, width * .5);
...
}
You could also subclass QTableWidget directly and do this same thing.
I have a QGridLayout and a QScrollArea inside one of the columns. Also, I have a QGroupBox inside the QScrollArea, where I list a number of combo boxes. Basically, what I want to do is avoid having horizontal scroll bars in the QScrollArea, and only have the vertical bars if the number of combo boxes is large. This can be done by fixing the width of the QGroupBox.
However, I don't have the size hardcoded, and rather allow the QScrollArea to grow as much as the column allows. Once the elements are drawn, they're fixed (no resizing).
So, basically, how can I find the size of a column in QGridLayout? Once I find that, I can limit the widths accordingly.
Thanks!
If all you want is to avoid displaying horizontal scroll bars within a QScrollArea, simply call
QScrollArea::setHorizontalScrollBarPolicy()
with Qt::ScrollBarAlwaysOff. To get the width and height of a QGridLayout cell, use the following code:
QSize getLayoutCellSize(QGridLayout *layout, int row, int column)
{
QLayoutItem *item = layout->itemAtPosition(row, column);
if (item)
return (item->sizeHint());
return (QSize());
}
You may can also use QLayoutItem::geometry() instead of QLayoutItem::sizeHint().
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.
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.
I have use the mx:Grid layout component to layout some form elements in a manner very similar to an HTML table. However, the result does not stretch out horizontally when you resize the app like an HTML table would. How can I make it do that?
Try setting the width and height of the Grid element to a percentage value.
I'm using a Grid on one of my main applications, and I've set the width/height to 100% and it resizes just fine.
You'll likely need to set the width and height of the rows and items (GridRow and GridItem) as well, depending on how you want the rows/columns to resize.
Edit if you're creating the Grid programmatically (e.g. in a .as file), you'll need to set these values as percentages as follows:
var grid:Grid = new Grid();
grid.percentWidth = 100;
grid.percentHeight = 100;