Custom Layout in QListView - qt

I'm looking for a way to create a custom layout in the QListView.
The problem is the following: I have a number of images, with different widths (height is the same). In code, these images are stored in QAbstractItemModel. I want to lay them out similar to "justified" layout in text editors, i.e. in each row there's an image in the start and in the end of the row, images in between are placed uniformly. Spaces between images on the same row should be equal.
I didn't find this customization functionality in Qt docs nor in google.
How can I achieve custom layout like this using Qt?
Here's the image, showing an example of the layout I need:

I tried to do that a while back, the result is this library: longscroll-qt. It does not use QAbstractItemModel however, since its structure does not fit the problem.
It solves all the nasty stuff, like what to do when there image sizes + spacings do not add up. And it supports real custom widgets to display items, that you can interact with (like pressing a button), which is not really possible in QAbstractItemViews.

Related

Qt - Print widgets in specific layout

In my project I have some own widgets.
When I click on a toolbar-button to print, I want to print the widgets in a
specific layout (landscape-orientation).
I tried it with a new QWidget with a layout, and in this layout add my widgets.
But when I do this, my widget disappear from my mainwindow.
How can I print my widget without disappearing and my own layout?
I had asked something similar - Printing complex widgets
and basically ended up in some design, where I set up an invisible copy of the whole widget and caused it to resize on the best size for the print job - take portrait and landscape printing into consideration!
I had to add code to paint- and resize- related methods of my custom widgets, where depending if I was printing or painting to screen, I modified palettes, font sizes, style sheets etc. Modifiying palettes might be necessary if your printer is black/white only. Or if your application has a dark background, you might want to flip colours.
This was quite a tedious job, but you will notice, that customers often don't want a screen hardcopy but something better.

Custom shaped menu with shadow in Qt

I'd like to create a context menu looking similar to this one:
I read suggestions on the web that QWidget::setMask() should be used to create a shape. But how can it fit the variable number of items then? Moreover, the same menu item may take more or less screen space on different machines.
Another question is how to create a shadow around this custom shape? As far as I understand, the mask allows to crop the widget, but not to make it semi-transparent.
I don’t found an easy way to do that! But here goes a way!
Instead of using the Qt mask API, I've used a frame-less widget with transparency enabled!
To draw the shadow, I've used radial gradient!
You can change the size of the menu before opening it, however you can’t resize it after opened (for example resize with mouse).
It’s quite easy add or remove widgets, just respect the layout margin to not draw outside the bounds destined to widgets. To simplify your life I created an inherited class of QPushButton with colors you can easily customize with style sheet.
See the result:
You can browse the source
Hope that helps!

How can you achieve this style of layout using Bootstrap but then move to a different arrangement on desktops?

I am looking to recreate this type of view for a list of 'offers' as groupons have when you visit the site on mobile:
As we can see they seem to be using a new unordered list item for each offer box, and then the offer itself is contained within a single list item, which has a link, image, header, and a table for the details about the bottom. This looks good and seems to work nicely.
I want to recreate this using bootstrap - in such a way that when I then view the layout on my desktop, the site scales up and perhaps new fields / details which were not visible in the example above suddenly become visible.
here, they are using tables. But, It is the old way, you can redesign itself with bootstrap. The only thing you should know is bootstrap.
they have a great documentation of their classes and what they do.
for example:- if you need an element to disappear in mobiles and appear in tabs and machines, you can use .sm-hidden class.
Give it a try.

Qt5: how to resize two UI lists against each other

I would like to be able to resize two QListWidget that sit in a QVBoxLayout, by grabbing the edge in the middle of the two and sliding it up or down. One would shrink, the other would get bigger.
Here is a screenshot of a sample app:
I would like to be able to grab the bar between the two lists and resize them.
This is what it looks like in the Qt layout editor:
I'm not sure if the vertical layout is the good choice, here, there may be another vertical layout that provide this functionality maybe?
Is it even possible with Qt?
You need to use QSplitter. It's available in the form designer.
To add to Riateche's correct answer, you can also use qt-designer to add specify the type of layout (QSplitter) by first selecting the widgets and then right-click to layout->horizontal splitter etc.
Here's a simple tutorial I ran by a few years ago: http://www.youtube.com/watch?v=E7Ud6FonsR4

Qt: Same Widget Inside Two Different Layouts

What I'm trying to achieve is that a widget could exist in two different layouts, in this case in QHBoxLayout and in QVBoxLayout. I'm implementing a system which dynamically switches between the two layouts when a device's screen orientation changes.
Currently I'm creating, let's say a complex composite widget called MyWidget and adding it into a two different layouts:
MyWidget *wgt = new QWidget();
QVBoxLayout vlayout;
QHBoxLayout hlayout;
vlayout->addWidget(wgt);
hlayout->addWidget(wgt);
Now imagine that both layouts are hosted within a 'root' layout, and that this root layout can resize into a more wide than tall 'landscape' mode, and into a more tall than wide 'portrait' mode.
The MyWidget shows correctly in only the first layout it is added into, and when the layouts are switched, it shows all wrong or not at all.
I don't know if I'm making any sense here, but this is my problem. Maybe when the switch event is called all child widgets and layouts should be resized, so it would always look right. Only problem is that I don't know how.
This isn't a general solution for changing layouts, but an easy solution in your case: Just change the boxlayout's direction.
hlayout->setDirection(QBoxLayout::TopToBottom);
// now your hboxlayout works as vertical layout
hlayout->setDirection(QBoxLayout::LeftToRight);
// and now it is horizontal again
This isn't particularly easy to do, but is possible.
First of all, I'd recommend that you actually create two different widgets, one for the vertical and one for the horizontal, and manage things that way. If the source data is properly separated from the UI class, you should be able to do so without too much trouble, but by incurring some memory overhead.
One way to do as you desire would be to completely remove the widgets from one layout and add them to the other when you need to change the arrangement on the screen, and change the layout that is added to the widget. This should cause the same widgets to be drawn in a different way.
A different, more intricate way of handling this (although potentially more efficient) would be to write your own layout and have it handle rearranging widgets based on the orientation change.

Resources