Init QListWidget to show its all content (with no scroll bars) - qt

I was playing with the Config Dialog Example, all was fine. Then I changed something in the right part of the dialog.
Then I found the contentsWidget (QListWidget) in the left part of the dialog became smaller and showed the scroll bars (both Horizontal and Vertical).
I want the QListWidget to show all its content so that no scroll bars are needed.
All items are added at the beginning and fixed. No dynamic.
I guess there is a simply method to let the QListWidget expand to show all its content at the beginning.
Could anyone help me and tell me the magic word?
Here is the code:
contentsWidget = new QListWidget;
contentsWidget->setViewMode(QListView::IconMode);
contentsWidget->setIconSize(QSize(96, 84));
contentsWidget->setMovement(QListView::Static);
contentsWidget->setMaximumWidth(128);
contentsWidget->setSpacing(12);
//contentsWidget->setMinimumWidth(contentsWidget->sizeHintForColumn(0));
//contentsWidget->setMaximumWidth(contentsWidget->sizeHintForColumn(0));
//contentsWidget->adjustSize();
//qDebug()<<contentsWidget->sizeHintForColumn(0);
createIcons();
contentsWidget->setCurrentRow(0);
QHBoxLayout *horizontalLayout = new QHBoxLayout;
horizontalLayout->addWidget(contentsWidget);
horizontalLayout->addWidget(pagesWidget, 1);
I tried contentsWidget->sizeHintForColumn(0), but it didn't work. It was 0. I tried some other methods but nothing worked.

I think you should try:
contentsWidget->setMinimumWidth(128);
This will ensure that no matters what, the size of contentsWidget will always be at least 128, hence large enough to contains the icons.

Related

Qt Aligning contents of top item in combobox

I am working with Qt to create a GUI for a small game theory project I'm working on. I'm using a combobox as a selector for the team. I can align the contents in the dropdown window just fine, e.g.
for(int i = 0; i < ui.comboBox->count(); i++)
{
ui.comboBox->setItemData(i, Qt::AlignLeft, Qt::TextAlignmentRole);
}
I can't figure out how to align the top item that is selected, as you can see here. The contents is not aligned to the left and the name even goes off the screen. Does anyone know how I can align this similarly to the contents in the dropdown list?
I already searched online, but I'm having trouble phrasing the question correctly. Furthermore if I go through the options available to me by the autocompleter when typing ui.combobox-> I don't really see an option that can do what I want.
Not sure if this is the go to fix, but I solved it by setting an icon size for the combobox. What I think goes wrong is that there is probably some default size set, no matter the size of the icons when adding items. My icons were of size 50x15 and so I added the following lines:
QSize size(50, 15);
ui.comboBox->setIconSize(size);
Now it is done correctly and gives the correct view.

Qt QGraphicsProxyWidget hiding other QGraphicsProxyWidgets

I'm working on a Slotmachine with QGraphicsScene and -View and I'm close to the end of that project. But now I want to add a small rect to the winning-lines that shows the amount of won credits in that line. The rect is a QLabel in a QGraphicsProxyWidget. I don't know if thats the best way to solve this problem but I'm not able to find a better solution at the moment. I'm setting the proxyWidget to not visible in the constructor of the label but when I'm setting this proxyWidget to visible other proxyWidgets in the scene disappear. And I just dont understand why. It's not hiding every proxyWidgets.
As you can see on the picture there's a red rect in the middle of the first square of the winning line. This is causing the problem. It is hiding the lower white border which is a QGraphicsPixmapItem and the first of the normally three QGraphicsProxyWidgets at the bottom, stake(this is not visible), last gain(letzter Gewinn), credit(Guthaben). I don't know why its not hiding the other Widgets on the bottom because they are all equal. As you can see its not hiding the complete lower border. I just don't know why this happens and why it just happens to the lower border and the left widget on the bottom and not to any other elements. I just don't know how to fix a problem like that.
It's a bit long to read but I don't know how to really describe the problem or how I could show you in the Code. I hope someone could help me.
Solved it by using an QGraphicsTextItem. totally forgot that it is existing. Thanks to the QT Forum.

How can I exchange an item and a spacer?

In qt, I have a form that contains among other things, a group with
A combo box
a checkbox
a spacer
a button
Based on some logic, I want sometimes to show another combo box... Where the spacer is, but smaller.
When I add it though, everything resizes automatically
I don't see a way to make it invisible, and yet keep items of the same size when I make it visible again.
I tried making it fixed size... But unless I use fixed sizes and positioning for everything, which I think is a bad idea, the items still move around when I change visibility.
It seems silly... But how can I make my little combo box show up instead of the spacer not next to it ? Spacers don't seem to have a name...
I would do
combo.setVisible(condition);
Spacer.setVisible(!condition);
Very easy... Except how do I access the spacer from code ?
My suggestion is to use a container QWidget instead of the spacer. Here is how it will look:
A combo box
a checkbox
a widget-container
a button
Widget-container is a QWidget with fixed size. Put your combo-box there and it will maintain it's size when you show/hide the combo-box.
Regarding your question (You will not need it but just to know in the future):
how do I access the spacer from code
You can create a spacer from code like this:
QSpacerItem* spacer = new QSpacerItem(0, 15, QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addItem(spacer);
...
Also you can get it from a layout if you know its index:
QLayoutItem* item = layout->itemAt(index);
But there is no such method as show/hide for layout items.

How do i resize the contents of a QScrollArea as more widgets are placed inside

I have a QScrollArea Widget, which starts empty;
It has a vertical layout, with a QGridLayout, and a vertical spacer to keep it at the top, and prevent it from stretching over the whole scroll area;
Elsewhere in the program, there is a QTextEdit, which when changed, has its contents scanned for "species" elements, and then they are added to the QGridLayout. Any species elements which have been removed are removed too. This bit works;
I have turned the vertical scrollbar on all the time, so that when it appears it does not sit on top of the other stuff in there. Note that the scroll bar is larger than the scroll box already though, despite not needing to be.
This is the problem. The scroll area seems to be preset, and i cannot change it. If i add more rows to the QGridLayout, the scroll area doesn't increase in size.
Instead, it stays the same size, and squeezes the QGridLayout, making it look ugly (at first);
And then after adding even more it becomes unusable;
Note that again, the scroll bar is still the same size as in previous images. The first two images are from Qt Designer, the subsequent 3 are from the program running.
If I resize the window so that the QScrollArea grows, then I see this:
Indicating that there's some layout inside the scroll area that is not resizing properly.
My question is; what do I need to do to make the scrollable area of the widget resize dynamically as I add and remove from the QGridLayout?
If you're coming here from Google and not having luck with the accepted answer, that's because you're missing the other secret invocation: QScrollArea::setWidget. You must create and explicitly identify a single widget which is to be scrolled. It's not enough to just add the item as a child! Adding multiple items directly to the ScrollArea will also not work.
This script demonstrates a simple working example of QScrollArea:
from PySide.QtGui import *
app = QApplication([])
scroll = QScrollArea()
scroll.setWidgetResizable(True) # CRITICAL
inner = QFrame(scroll)
inner.setLayout(QVBoxLayout())
scroll.setWidget(inner) # CRITICAL
for i in range(40):
b = QPushButton(inner)
b.setText(str(i))
inner.layout().addWidget(b)
scroll.show()
app.exec_()
The documentation provide an answer :
widgetResizable : bool
This property holds whether the scroll area should resize the view widget.
If this property is set to false (the default), the scroll area honors the size of its widget.
Set it to true.
Why don't you use a QListView for your rows, it will manage all the issues for you? Just make sure that after you add it you click on the Class (top right window of designer) and assign a layout or it wont expand properly.
I use a QLIstWidget inside a QScrollArea to make a scrollable image list
Try this for adding other objects to the list, this is how I add an image to the list.
QImage& qim = myclass.getQTImage();
QImage iconImage = copyImageToSquareRegion(qim, ui->display_image->palette().color(QWidget::backgroundRole()));
QListWidgetItem* pItem = new QListWidgetItem(QIcon(QPixmap::fromImage(iconImage)), NULL);
pItem->setData(Qt::UserRole, "thumb" + QString::number(ui->ImageThumbList->count())); // probably not necessary for you
QString strTooltip = "a tooltip"
pItem->setToolTip(strTooltip);
ui->ImageThumbList->addItem(pItem);
Update on Artfunkel's answer:
Here's a PySide6 demo that uses a "Populate" button to run the for loop adding items to the scroll area. Each button will also delete itself when clicked.
from PySide6.QtWidgets import *
app = QApplication([])
scroll = QScrollArea()
scroll.setWidgetResizable(True) # CRITICAL
inner = QFrame(scroll)
inner.setLayout(QVBoxLayout())
scroll.setWidget(inner) # CRITICAL
def on_remove_widget(button):
button.deleteLater()
def populate():
for i in range(40):
b = QPushButton(inner)
b.setText(str(i))
b.clicked.connect(b.deleteLater)
inner.layout().addWidget(b)
b = QPushButton(inner)
b.setText("Populate")
b.clicked.connect(populate)
inner.layout().addWidget(b)
scroll.show()
app.exec()

Getting rid of unnecessary scrollbar in QScrollArea

I'm working on a Configuration QDialog. It has several categories (General, Appearance, etc.) that get loaded when the user clicks on them. Each category has its own page. These pages are separate classes in their own right (each has its own ui, cpp, and h). When the Preferences window loads, the pages get instantiated and are loaded into a QStackedWidget. The Stackedwidget is then placed into a QScrollArea so it can scroll if necessary.
However, there is a problem. Ever since I added the QStackedWidget, the QScrollArea always has a vertical scrollbar even when the current page is short enough not to need one: (picture shows the shortest page)
The ScrollArea vertical scroll policy is set to Qt::ScrollBarAsNeeded so logically it should only show a bar if the page is larger than the viewable area.
Here's what I already tried to fix this:
Setting the scroll policy to Qt::ScrollBarAlwaysOff. While this gets rid of the scrollbar, its unacceptable since it doesn't let the user know they need to scroll on long pages.
Setting the Minimum/Maximum heights for the QStackedwidget. This makes the scrollbar go away if I set it for a low enough value, but it is unacceptable since it causes some of the widgets to have a squished appearance.
I know the problem has something to do with the QStackedWidget but since this is the first time I've used QStackedWidget I’m not sure what it is. I've also noticed the scroll is always for the same amount; i.e. the scrollable area is always the same size no matter how large/small the page widget is. For some reason, it's slightly larger than the longest page. At first I thought the vertical spacers I put at at the bottom of each page to tighten up the layout were causing this, but taking them out didn't fix it.
Update: Here's the code that controls the Stackedwidget:
void Newconfig::on_Categories_currentItemChanged(QTreeWidgetItem *current)
{
QModelIndex index=ui->Categories->currentIndex();
int idx=index.row();
QString category=current->text(0);
this->setWindowTitle("Preferences -- " + category);
if (stack->currentWidget() != 0) {
stack->currentWidget()->setSizePolicy(QSizePolicy::Ignored,
QSizePolicy::Ignored);
}
stack->setCurrentIndex(idx);
stack->currentWidget()->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);
adjustSize();
}
The QStackedWidget takes the size of the largest widget added to it. In your case, the largest page in your preferences dialog is what is influencing the size of the QStackedWidget and is thus forcing the scroll area to show its scroll bar, even when it doesn't appear to be necessary.
To get around this, you can create a slot that is triggered right before you change the current widget in the QStackedWidget that sets the size policy of the page you are leaving to QSizePolicy::Ignored and the size policy of the page you are about to show to whatever you desire for that page -- QSizePolicy::Expanding for instance. An example of this technique is detailed on this page. There's a lot of code in the example, but the important part is here:
void changeCurrent(int idx)
{
if (stackWidget->currentWidget() !=0) {
stackWidget->currentWidget()->setSizePolicy(QSizePolicy::Ignored,
QSizePolicy::Ignored);
}
stackWidget->setCurrentIndex(idx);
stackWidget->currentWidget()->setSizePolicy(QSizePolicy::Expanding,
QSizePolicy::Expanding);
adjustSize();
}
This function is called to change the current index on the QStackedWidget. It causes the page that was just being viewed to have a size policy that has no influence on the overall size of the QStackedWidget and the page that is about to be viewed to have a size policy of consequence.

Resources