Can a background image be larger than the qlistview item? - qt

I'm working on an app in which I'm using a QStandardItemModel. Every item in QstandardItemModel is a thumbnail with text. These items are bound in a QListView which are in a layout.
Is it possible to set a background for each of the QListview items such that the background is larger than the item? Or is it possible to add a frame to the item?

Yes, you can add a frame to the item index of item, and you can set a background to each item, but I didn't understand your need to do that. I mean, if you background may be larger than item, why don't you set a whole QListView background instead?
Edit: How to add a frame to an item:
QFrame *frame = new QFrame(this); //or any other widget-type object.
ui->listView->setIndexWidget(ui->listView->model()->index(0,0), frame); //set to desired index.

Related

Set minimum row-height of QComboBox

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)

How to show QListWidgetItems in a grid without eliding the text?

I have a QListWidget. I set a grid size for QListWidget, to show items inside the list widget as a grid. But when font size of the dialog is increased, items that have long text are shown with an elide. How to remove this elide and show the full content of the items?
QListWidget *listWidget = new QListWidget;
listWidget->setFlow(QListView::LeftToRight);
listWidget->setResizeMode(QListView::Adjust);
listWidget->setGridSize(QSize(100, 50));
listWidget->setSpacing(someInt);
listWidget->setViewMode(QListView::IconMode);

Qt:Unable to set Background color for qComboBox properly

I have a QCombobox and I want to set a white background color.This is my code.
QComboBox *cBox = new QComboBox;
cBox->addItem("Text1");
cBox->setStyleSheet("background-color:white");
This combobox has a parent widget whose background is an image and is set as given below:
ui->centralWidget->setStyleSheet("border-image:url(./image.png)");
When I set the parent Widget[centralWidget] background as some other color,then the white BG works properly for the combobox.But when I set an image as the parent Widget background,the UI looks like this.
In the above pic,the black Bg is an image.Could someone highlight me what am I missing.Any help will be really helpful.
When you do not indicate to which widget you are going to apply some property, they will be applied to all your children, for this reason the same QComboBox background image is applied to the child of centralWidget.
In your case you want to apply only to the centralWidget, and by default Qt Designer uses the same name for the name of the variable that represents the widget and the objectName.
So if you want to apply to a widget we can use the objectName as selector:
QWidget#centralWidget{ border-image:url(./image.png)}

Cannot align items to top in scroll area Qt

In a Qt Widget Application, I'm trying to make the following:
My aim is to have a form with a scrollArea with a vertical layout and each item in the scroll area aligned at the top.
Unfortunately, it's only staying in the centre shown here:
I've tried selecting the scrollArea and chaninging vertical alignment from AlignVCenter to AlignTop, but this seems to have no effect.
How can I align all the content to the top of the scroll box?
You can use a QListWidget to hold a list of not only just text, but also QWidget's (such as push buttons, line edits, etc).
Simply add a QListWidget to your window, and in C++ add the widgets to the QListWidget as needed.
Here are some steps to insert a button into your QListWidget:
Create a QListWidget item into your application.
change selectionMode = NoSelection and focusPolocy = NoFocus
Put the following C++ code into your application:
// Create list item
QListWidgetItem *listWidgetItem = new QListWidgetItem();
// Create widget
QWidget *widget = new QWidget();
QVBoxLayout *verticalLayout = new QVBoxLayout();
verticalLayout->setSizeConstraint(QLayout::SetFixedSize);
widget->setLayout(verticalLayout);
// Add button to widget layout
QPushButton *pushButton = new QPushButton("Button!");
verticalLayout->addWidget(pushButton);
// Add list item to ul->listWidget
listWidgetItem->setSizeHint(widget->sizeHint()); // Set the size of the list item to the same as the widget
ui->listWidget->addItem(listWidgetItem);
ui->listWidget->setItemWidget(listWidgetItem, widget);
Old answer:
I found my answer here but I will re-post it for anyone else interested.
To align elements at the top of a scroll area, add a vertical layout as a child to the scroll area and put all your elements inside that vertical layout. Like so:
In your C++, add a spacer item to your vertical layout inside the scroll area by using addStretch():
ui->verticalLayout->addStretch();
All the items should now magically be aligned to the top.
Edit:
This seems to only work for the very first item, and all other items are not pushed up to the top.

QListWidget resize contents when splitter changes size

I have a QListWidget with some widgets inside:
QListWidgetItem* w = new QListWidgetItem(ui->listWidget);
ui->listWidget->addItem(w);
w->setSizeHint(widget->sizeHint());
ui->listWidget->setItemWidget(w, widget);
However, everything seems to work fine until I resize the the QListWidget (it is embedded into a QDockwidget). Do I have to iterate over all items and resize them manually or is there a simple trick?
It is just a list with items having a button on the left and a button on the right. If the list gets resized (QSize changes) than the buttons gets hidden and a scrollbar appears. I want to have the size of the item according to the list width.
Try setting the resizeMode property to Adjust. This will cause your QListWidget to automatically resize all of the items in the view to the size of the QListWidget anytime the widget is resized.
See the documentation here. Your code will be structured like so:
QListWidgetItem* w = new QListWidgetItem(ui->listWidget);
w->setSizeHint(widget->sizeHint());
ui->listWidget->setResizeMode(QListView::Adjust);
ui->listWidget->addItem(w);
ui->listWidget->setItemWidget(w, widget);
EDIT: Depending on the uniformity of your items, you may also benefit from setting the uniformItemSizes property. QListView can optimize the layout of your view if you set this property.

Resources