QListWidget rows overlaping each other - c++

First of all I am apologizing for my English in advance. I am trying to display a custom widget in a QListWidget, but when more than one row is added, they are displayed on the same spot.
(source: minespeed200.de)
Settings of the QListWidget
alternateingRowColors:true
movement:Static
isWrapping:false
resizeMode:adjust
layoutMode:singePass
spacing:1
viewMode:listMode
i've already tried all of the possible settings for this values. if there is another importent one i've missed (i am fairly new to Qt) please tell me. Also the row colors seem to overlap too (the picture of my application below has 2 rows added), what tells me, that not only the widget's but the row's are overlapping each other entirely.
(source: minespeed200.de)
(source: minespeed200.de)
I am adding the rows with this code:
item=new QListWidgetItem();
QSize size(this->width(),this->height());
item->setSizeHint(size);
list->addItem(item);
list->setItemWidget(item,this);
Inside of the constructor of my custom widget class.

The issue was that i left the gridSize of my QListWidget at it's default value and the y component had to be set to the height of my widget i was displaying.
Adding:
list->setGridSize(QSize(0,this->sizeHint().height()));
fixed it.

I think you need this method.
list->QListWidget::setFlow(QListWidget::LeftToRight)
check here: https://doc.qt.io/archives/qt-4.8/qlistview.html#flow-prop

Related

How can I overlap qwidgets while using the grid layout and positioning overlapping widgets a particular distance from the window border?

I am programming a game and I have a tab widget which takes up the majority of the window. I want to use the extra space in the tab bar for buttons. I have the tab widget in a grid layout. To accomplish this, I use the code below in order to remove and add back the button widgets to the desired areas (the solution to someone else's question).
ui->centralLayout->removeWidget(ui->exitButton);
ui->centralLayout->removeWidget(ui->ResizeButton);
ui->centralLayout->addWidget(ui->ResizeButton,0,4, Qt::AlignTop|Qt::AlignRight);
ui->centralLayout->addWidget(ui->exitButton,0,4, Qt::AlignTop|Qt::AlignRight);
This does not work for me; however, because I would like the second widget-- the resize button-- to be just to the left of the exit button. What is occurring is that it instead overlaps the exit button. I simply need to move it 21 pixels to the left and have no idea how!
I tried putting both buttons in a frame and then removing and adding the frame the way I did the buttons. Unfortunately the same functions I used do not exist for the qt frame object.
Here are some pictures of my window.
https://docs.google.com/document/d/17w5USWQcCtb6OdcRShdcYcRjXTcdVpmdrG5TWLX71y8/edit?usp=sharing
you are using void QGridLayout::addWidget(QWidget * widget, int row, int column, Qt::Alignment alignment = 0) overload.
2-nd and 3-rd parameters are row and column of a grid. And you put 2 widgets in the same cell so they are overlaping each other.
I solved my problem. Earlier when I was trying to add them to a frame and reposition it I could not but using a widget as the container for my buttons let me place them the way I was earlier attempting to individually place the buttons.

Columns auto-resize to size of QTableView

I am new to Qt and I have just managed to make a QTableView work with my model. It has fixed 3 columns. When I open a window, it look ok but when i resize the window, the QTableView itself gets resized but columns' width remains the same. Is there any build-in way to make it work? I want columns to resize to fit the edges of QTableView every the the window gets resized.
This code equally stretches each column so that they fit the table's width.
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
Docs:
QHeaderView::setSectionResizeMode
See resize modes here.
There is a header flag to ensure that the QTableView's last column fills up its parent if resized. You can set it like so:
table_view->horizontalHeader()->setStretchLastSection(true);
However, that does not resize the other columns proportionately. If you want to do that as well, you could handle it inside the resizeEvent of your parent thusly:
void QParent::resizeEvent(QResizeEvent *event) {
table_view->setColumnWidth(0, this->width()/3);
table_view->setColumnWidth(1, this->width()/3);
table_view->setColumnWidth(2, this->width()/3);
QMainWindow::resizeEvent(event);
}
QParent class is subclass of QMainWindow.
Widgets QTableView, QTreeView and their derived classes (such as QTableWidget) have this two usefull methods:
QHeaderView* horizontalHeader() const;
QHeaderView* verticalHeader() const;
If you open documentation for a class QHeaderView, you will find methods that set up appearance and behavior of row or column header for item views. You can resolve your problem by one of these methods:
void QHeaderView::stretchLastSection( bool stretch )
As Davy Jones mentioned.
Example:
QTableView *table = new QTableView();
table->horizontalHeader()->setStretchLastSection(true);
void QHeaderView::setResizeMode( ResizeMode mode )
As mode you can set QHeaderView::Stretch or QHeaderView::ResizeToContents.
Unfortunately this method have a drawback - after it's apply you will not be able to change size of columns (or rows) manually (in GUI) or programmatically.
Example:
QTableView *table = new QTableView();
table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
In PyQt5 you can achieve this in your table_widget by doing:
header = table_widget.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)

How to add a widget to a qt Tool bar

I have the following problem:
I have added a spinner to my qt Tool Bar, which is located from top to buttom. This works fine. But now I want to order some buttons in a special order, but with mainToolBar->addWidget(button_name) it would be among one other. So how can I solve it? I tried to make a new widget "widget_1" and added some buttons to this widget, but when I write mainToolBar->addWidget(widget_1) nothing appears, only the one slider I have already added. Can anyone help me?
Thanks a lot :)
Well, just forget something.
select the widget and then layout in a form or whatever layouting you prefer.
And that's it. now, the widget will be shown in toolbar. it's because of size
and child widgets position. by layouting, everything will be resized and position
correctly.

Qt splitter layout resize behaviour using Qt Designer

I have an issue with size in my view made in Qt with drag and drop.
Let me start with an image to help me explain
This is the mainwindow for my form.
What happens is:
We have 4 tab widgets. the left tab widget has a horizontal splitter to the 2 mid widgets.
The 2 mid widgets have a vertical splitter, and a horizontal splitter on the left and right side.
The right widget has a vertical splitter on its left hand.
So all views are connected using splitters.
Lastly the mainform sticks every thing together in a resizable way using horizontal layout.
The problem is, the width of the leftmost and rightmost widgets are fixed (in designer).
I want them to be smaller in width. Something similar to:
You can see the widgets are resized. I was able to do this running the application, and manually adjusting the splitters. Is there a way in QtDesigner to do this? I tried playing with policies. I didn't really get any further however. Does this indicate a lack of knowledge of my part about policies? Perhaps layouts in general?
What options should I use to achieve the desired layout using QtDesigner. I want to avoid using code.
Hope I will be able to solve this soon. It must be overlooking something simple..
You can play with the "Horizontal Stretch" and "Vertical Stretch" properties to change the position of the split.
For example with both the vertical stretch of the top central QTabWidget and the horizontal stretch of the central QSplitter at 1 and all the other values kept at 0, you'll get the result you want.
When you have multiple non-zero stretch values, the result of the ratio (e.g.: vertical strech at 2 and 1 for the 2 central QTabWidgets => 2/3 and 1/3) is not visible in the designer but seems to be working when you run the application.
PS: You can also achieve the same result with tabbified QDockWidgets but the dock tabbification is not possible through the designer only.
I set start position that:
QList<int> list= ui->splitter->sizes();
list.replace(0,this->height()/0.3);
list.replace(1,this->height()/0.7);
ui->splitter->setSizes(list);
and remember to minimum size child widget

QListWidget that resizes instead of scrolls

How do you change the behavior of a QListWidget so that it resizes its height instead of choosing a (seemingly arbitrary) height and adding scrollbars? See screenshot:
The QListView's should fill up as much space horizontally as they can (creating as many "columns," if you will.) Then they wrap and make as many rows as necessary to fit all the items. These calculations should be adjusted as the window is resized. This is all working fine.
However, what I want to happen is that instead of the height staying the same, the QListView should grow or shrink vertically and never need any scrollbars. The scrolling, if necessary, will be handled on the parent QWidget that hosts all of the labels and lists. It seems like once the height of the QListWidget is established (not sure where its default is coming from), it never changes. It is too big in some cases (see second "Test" list above) and too small in others (see first "blank maps" list above.)
The layout above is nothing surprising: two QLabel's and two QListWidget's in a QVBoxLayout. Here are the properties I have set on the QListWidget's:
setMovement(QListView::Static);
setResizeMode(QListView::Adjust);
setViewMode(QListView::IconMode);
setIconSize(QSize(128, 128));
(I already tried setting the horizontal and vertical scrollbar policies, but that just turns the scrollbars off, clipping the content. Not what I want.)
Maybe you could this without using QListWidget. The Qt's examples contain a new layout class, QFlowLayout, which could be useful. With the following kind of widget hierarchy you could get multiple groups with labels and they all would be inside one QScrollArea.
QScrollBox
QVBoxLayout
QLabel "Blank maps"
QWidget
QFlowLayout
your own widgets showing map images and labels
QLabel "Text"
QWidget
QFlowLayout
your own widgets
The problem is that this kind of solution would create much more widgets than QListWidget based solution. So if you have hundreds of items in your list, this might not be the best solution.
There is a protected member function called contentsSize() in QListView. It is used to calculate the required minimum(), maximum(), and pageStep() for the scrollbars (as mentioned here).
Can you subclass the QListView class and make use of that information? I suggest you recalculate the size of your widget in the same function where you add contents to it. While somewhat lacking elegance, this appears to be a pretty reliable solution.