Qt Designer mainwindow size anomaly - qt

I'm trying to get a Designer fixed size mainwindow to display as it looks at its minimum size in Designer. If I set its sizePolicy to 'Expanding, Expanding', and adjust the size as small as it will go, the geometry is shown as [(0,0), 1378 x 756]. When I run the program it displays bigger than it needs to be, but I can resize it to its smallest and it looks good enough (picture on the left), although the bottom of the Random frame doesn't perfectly align with the label to its left.
If I use QSettings; the size from the .config file is 'size=#Size(1368 727)', but I can't set it to that in Designer. If I set the sizePolicy to 'Fixed, Fixed' at 1378 x 756 then it looks wrong when I run it and I obviously can't resize it (picture on the right).
two images as described above
What could be causing it and/or how can I fix it?
(I tried adding QApplication::setStyle(QStyleFactory::create("Fusion")); and its #include, from the first answer here, but no change)
Qt Creator 7.0.0 Based on Qt 6.2.3, Linux Mint 21 Cinnamon (ubuntu 20.04).
Eta: Typically; a bit more effort on my part has got it right. Mainly I think the label was too big for the space available for it. All looking perfectly aligned now and no more discrepancy between Designer's size (of the main window) and the .conf file.

Related

Pixelated gif in QT

I'm trying to show a gif file in QT app, using the approach provided in the link: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/widgets/movie?h=5.15
Approach makes use of QMovie object set in a QLabel.
The example works well and fine.
But if I enable High DPI scaling for the app, the gif becomes all pixelated. Please see the screenshots below.
This is the line that I add to enable High DPI scaling.
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
Any ideas to get this fixed ?
I have tried the following fixes already:
setScaledSize for the QMovie object
setScaledContents(true) for the QLabel
QT version I'm using is 5.15.2 and platform is Windows.
A GIF picture cannot have more than 256 unique colours. When you load such an image into Qt, it is internally represented in that exact format with the palette (of 256 colours) from the GIF representation, even if your hardware might be able to display many more colours.
This also means that when you scale such an image, Qt is not allowed to extend the colour space to render in-between colours - This means that scaled GIF pictures generally have to look much worse than scaled high-colour images.
The solution to this is either to transform the QImage you created from a GIF picture into a format with a larger colour space before scaling it (with QImage::convertToFormat) or, better still, don't use GIF images at all. After all, GIF is a format developed 30 years ago and has never really been updated to adapt to modern hardware, and using it, you artificially limit your programs to the capabilities of that format.
It seems at this moment there is no way to render sharp gifs when scaled using QMovie and QLabel. I have filed a bug for the same in QT bug tracker.
Meanwhile I have found a workaround that works fine. It is done making use of QML in the QWidget system, using QQuickWidget.
Let me add the full steps here, so that it is helpful to anyone else who run into this problem:
First we need to add support for Qml and QuickWidgets. I use CMake and Visual Studio. Hence I add the below lines in the CMakeLists.txt file. Equivalent changes needs to be made in the .pro file, if QT Creator is used instead.
# I'm only adding the relevant lines for brevity
find_package(Qt5 COMPONENTS Qml QuickWidgets)
target_link_libraries(${APP_TARGET_NAME}
Qt5::Qml
Qt5::QuickWidgets)
# Note the --qmldir switch
add_custom_command (TARGET ${APP_TARGET_NAME} POST_BUILD
COMMAND ${QTDIR}/bin/windeployqt
--qmldir ${CMAKE_CURRENT_SOURCE_DIR}/qml
${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${APP_TARGET_NAME}.exe)
Then create a spinner.qml file inside a folder named qml:
import QtQuick 2.15
Rectangle {
width: 12
height: 12
AnimatedImage {
y: 5
width: 12
height: 12
id: spinner
source: "img/spinner.gif"
speed: 1.0
}
}
And then load the qml file using QQuickWidget and add the QQuickWidget instance to the QWidget layout.
QHBoxLayout *main_layout = new QHBoxLayout();
auto *spinner_gif = new QQuickWidget(QUrl::fromLocalFile(":/spinner.qml"));
main_layout->addWidget(spinner_gif);
this->setLayout(main_layout);
The gif that shows up won't be pixelated even when scaled and we can see the GPU in use, in the Task Manager, as expected for a QQuickWidget.

JavaFXML app shows zoomed resolution in NetBeans

In SceneBuilder I have set my root pane(Anchor Pane) to 1280 x 720 and it shows the same thing in preview, however when I run my code in NetBeans my program is shown bigger than my full HD screen (1920 x 1080).
To resolve this I lowered my set resolution in SceneBuilder to something less and now it shows a smaller window when ran from NetBeans but it looks zoomed in and the size is still bigger than what it should be.
What can I do in NetBeans to show accurate size of stage(window) i.e size that I have created in SceneBuilder.
You can use many methods and add them to anchorPane.
For Example:
anchorPane.setPrefSize(double prefWidth, double prefHeight)
In Netbeans. Here is link: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html#setPrefsize-double-
And you can find other methods in that documentation. For example: setMinWidth(double), setMinHeight(double), etc...
If you want to be 100% sure that your window is always 1280 x 720 then set:
anchorPane.setMinWidth(1280);
anchorPane.setMaxWidth(1280);
anchorPane.setMinHeight(720);
anchorPane.setMaxHeight(720);
where "anchorPane" can be Fx Id of your Anchor Pane.
EDIT: You can set them in initialize method.
Maybe you want to set size of Stage, then use stage instead of anchorPane.

How to avoid adding excess 1 pixel spacing by Qt Designer

I'm working on application for embedded and we have 256x64 grayscale screen. Qt 5.3 perfectly renders on that screen with -platform linuxfb option. Obviously, we save every pixel of space, so I faced with trouble: Qt Designer adds excess 1 pixel spacing for every layout element deeper in hierarchy. So they accumulate for the most deep widgets. More precisely, for some reason child element of layout components gets coordinates (1,1) relative to parent. So, it's true for every widget except for root widget. Picture below demonstrates accumulated spacings (thin and thick red lines), and (1,1) coordinates of the very first child widget.
I believe it's Qt behavior itself, not just Qt Designer issue (not tested yet). But I can't work further even if it's shown in Designer only: I need to have pixel-exact view while designing.
Of course, every spacing and margin of every component in form set to 0.
Manual coordinates assigning (from code) eliminates the problem of course, but I need to generate code by uic.
So, my question is: how to avoid such spacings? Fixing Qt core sources can be (hard) option too, since anyway we recompile Qt for the project.
Mirror post on Qt forums
Thanks.
Ilia.
If you select Form > View Code, you can see that the geometry is not actually used for widgets which are inside a layout. So the numbers you see in the Property Editor are purely informational and have no relevance to the eventual code that is generated from the ui file (which is why they are greyed out).
The one pixel offset is there because Qt Designer needs space to draw the red boxes around layouts. They have to be be represented somehow, so I don't see how this can be avoided given the way Qt Designer currently works. If you want a more accurate reprentation of the final results, I suppose you will have to show a preview.
There is a facility in Settings > Preferences > Embedded Design that allows you to specify device profiles (which determine things like style, font, and screen resolution). This will add a new entry to the Preview In menu, which should allow you to refine the accuracy of the previews even further.

JavaFX Screen size?

I'm using a PiTFT on my pi and running a JavaFX program. I got the output going to the PiTFT by running fbcp in the background.
I had to set disable_overscan=1 in /boot/config.txt to get it to show the whole image. That all works fine now.
My problem is that the touch screen calibration doesn't seem to effect JavaFX apps. The X and Y axis are swapped. Does anybody know how to calibrate the touch screen for JavaFX apps?
I don't know if this is a problem with the touchscreen driver for the PiTFT or a JavaFX configuration problem, so I'm going to post the same question to the adafruit forums and if I get an answer in one place, I'll copy it to the other.
I've struggled with the same problem today, and I managed to figure it out.
The PiTFT is by default a portrait display with a resolution of 240x320. The driver conf is originally made to rotate the display. The touchscreen setting for X are aware of that and have the rotate option set accordingly. And yes, this will not affect JavaFX since it doesn't run in X.
What I did:
in /boot/config.txt
disable_overscan=1
framebuffer_width=480
framebuffer_height=640
#set specific CVT mode
hdmi_cvt 480 640 60 1 0 0 0
#set CVT as default
hdmi_group=2
hdmi_mode=87
hdmi_force_hotplug=1
With this, the main display resolution is set to 480x640, and overscan is disabled.
Next, in /etc/modprobe.d/adafruit.conf, change the rotate option to be 0.
Save everything and reboot. Then fbcp & java Main, and it should be working OK.
Btw, be aware that I used the pre-built raspbian image from adafruit.

Cant get widget to keep its aspect ratio

I am making a program with Qtcreator (using qt designer too).
I made some QSvgWidget buttons (What i wanted was a scalable image based button). They work but don't keep the aspect ratio of the image (aspect ratio needs to be 1:1).
I haven't been able to come up with a solution for this myself even after hours of trying.
I have tried:
QSizePolicy qsp(QSizePolicy::Preferred,QSizePolicy::Preferred);
qsp.setHeightForWidth(true);
ui->Widgetname->setSizePolicy(qsp);
But that doesn't work.
Some images of the problem:
Correct:
Incorrect:
I am new to working with Qt and coding.
What am I doing wrong?

Resources