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.
Related
When i add splitters, it acts as a layout, but also allows to resize the widgets in runtime. So, for example, i managed to lay out my widgets in this way:
Therefore, i can resize my widgets in runtime. As i noticed, this function is also available in designer mode, but it doesn't work properly. I tried to hover over my splitter and drag it in designer mode, but it only replaces the entire widget.
That is how does my main window look like in QtDesigner. I haven't tried to code yet. The problem is, that even though i used to set a stretch factor, my widegt's look in designer mode and in runtime completely differ. They have another sizes.
So, what are the problems:
Firstly, i can't change my widgets sizes properly, using stretch factors. I don't know, i tried to change size policies, but i did't manage to see an effect. I have somehow changed size of the vertically oriented widgets, but when speak about horizontal orientation - stretch factor and size policy doesn't change anything at all.
Secondly, i can't move my splitter in designer mode. It's position is constant, by default, it's always somewhere in the middle.
Thirdly, i have bugs (i think so) with my widget sizes in designer mode. They differ with widget's sizes in runtime.
Question:
So, how can i change widget's sizes properly? Maybe there's a way of moving a splitter in designer mode - do newer versions of Qt have it? Currently i'm using Qt 5.9.9. Also, why these bugs, and are they bugs at all. Maybe i just should update my Qt to newer versions to get access to newer functionalities?
Comment: I'm not sure if stretch factors work with layout as they do with widgets. I'm using layouts exactly the same way i use widgets. My layout's wrong(maybe) use may have caused this problem. Anyways, i'm entirely new to Qt, and may not know something to understand it completely.
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.
I'm trying to create a QTableView that can be zoomed in and out like in Excel.
A similar question was asked here: Zooming function on a QWidget
However, I'm subclassing the QTableView in PyQt and not C so reimplementing the entire PaintEvent method is a bit evolved. The source code for that is a bit complex: https://qt.gitorious.org/qt/tiittane-qt/source/bdd4a9149789f60974603e1f7621d51378f0a108:src/gui/itemviews/qtableview.cpp#L1282
I'm looking to see if there are any other viable options to have a zoom able TableView. My first attempt was by setting the font size then realized each column and row widths would have to scale as well which can become slow. Then realized changing the font would change the print. It didn't seems like an elegant solution. Changing the scale of the painter before painting seems like the more elegant solution but would have to re-implement and translate quite a bit of code to python to do so. I'm wondering if there are any other hooks to get this done.
Thanks
If you can use QTableWidget instead then you can create a QGraphicsScene and add it to that. Then you easily control the scale of the widget within.
If you want vertical and horizontal headers always visible I think you will have to turn off the table's scrollbars (which would end up zoomed, probably not what you want anyways) and have the scrollbars part of the panel that contains the graphics scene (probably panel would be a QAbstractScrollArea with 4 cells in layout: one cell for scene, one for horiz scrollbar, one for vert scrollbar, and one for the corner maybe empty), and connect them to the table's scroll behavior.
There is no built-in method to zoom on a view.
The simplest way to separate the size of the font on the screen, versus the size of the font saved or printed, is to basically have two fonts. One to be displayed on the screen you can call 'zoom', versus the other to be saved/printed and call that 'font size'.
Note this answer is cut and pasted from the same question:
Zooming a view in PyQt?
After much digging I've realised there is no Qt-way to use 3 middle buttons in landscape mode in S50v5, however I don't like to waste this precious space. I've tried to place my widgets there but menu bar is on top and widgets aren't visible.
Is there any way to utilise this space without using native Symbian APIs?
Controlling the stock softkeys in Qt is a pain. The strategy I have used is to make the QMainWindow full-screen with showFullScreen() which allows you to use the softkey space yourself. You will have to make sure any widgets you create are ultimately parents of the main window, and be mindful of this bug when creating pop-ups.
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.