How to set Qt sRGB gamma for stylesheets and labels? - qt

My QGLWidget has glEnable(GL_FRAMEBUFFER_SRGB) but my Qt widgets are not gamma corrected causing a disconnect between the Qt stylesheet colors and other colors (for color picking) and what is displayed in the QGL view.
Is there a Qt way to gamma correct in these cases?

Related

QMenu font size for different DPI for windows

I tried to make a custom QWidgetAction. That works fine.
However... on windows with different DPI the font in the menu changes in non-obvious ways.
100% -> pointSize = 8
125% -> pointSize = 9
150% -> pointSize = 9
(so I was not able to fit some linear function).
I am going through Qt sources to find where the font size is set, but no luck (so far).
Maybe someone can point me to the place where the font size is set depending on DPI? (So that I can use the same calculation for my custom widget.
Thanks, guys.
Add QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); in main.cpp and check again I think this will fix your problem.
also, this doc explains it more.
Edited:
For a setting font to QLabel you can do this in two ways :
From GUI, can change the font of all widgets in GUI from here :
create QFont object and add the same menu font to it then with the setFont function of QLabel add it.
QFont font;
font.setPointSize(9);
label->setFont(font);
Default font size for all widgets and QMenu in Qt is 11.

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.

qt creator theme pane background color

What variable in QT Creator theme file .creatortheme controls the background colors noted in the figure? I tried BackgroundColorNormal but it didn't work. .

Relative UI unit in QT Widgets QSS stylesheets

My current UI doesn't use QT widgets paint and it's mostly done with QSS markup. The current styles are defined in pixels and I could not find anything in the QT documentation that allows the styles to be defined as relative unit as opposed to pixels. I would like to know if it's possible to use something relative then tie that unit to a ROOT font-size. In case I need to adjust for a particular screen size like a battery charger IOT device that has a small screen, while my main UI was designed for very large screens (27 inch monitor). So instead of me styling a button just for the IOT device, I shrink the base unit (technique similar to web responsive like REM) then everything like fonts, padding, buttons shrink proportionally. Is that something that QT supports?
To my knowledge vanilla Qt Widgets don't support free scaling at all. Recently high-DPI display support was added, but it only scales in fixed ratios (e.g. 1:2). There are other options to get scaling in Qt applications:
Use QML (didn't try this yet myself, but it's developed with adaption to different displays in mind)
Use QGraphicsScene with QGraphicsWidget
Use QGraphicsScene with custom graphics objects that implement the behavior you need
Roll your own widgets
I'd love to stand corrected on this answer - maybe in a future Qt version...?

QSpinBox buttons size

Is there a way to obtain the size of the buttons/arrows of a Qt QSpinBox?
Note: This question has nothing to do with changing the style of a QSpinBox. On the contrary: The intent is to use the QSpinBox style as given and obtain at runtime the size of its buttons/arrows, which will be different for different platforms, for the purpose of fine tuning the layout constraints in which the QSpinBox is one component.
Also: The QStyle class does not appear to provide PixelMetric values for the button/arrows sizes.
Thnx.

Resources