I have a Qt Widgets application using Qt styleSheet for the app look and feel. We want to add support for high-DPI displays, as on these, the app looks very small. The first step was thus to call:
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
This does most of the job and the app window is big enough. The only remaining issue is the resolution of the images used in style sheet.
Say that we have:
QCheckBox::indicator::unchecked {
image: url(:/res/checkbox_off.png);
}
MainWindow QFrame {
background-image: url(:/res/background.png);
}
When using high DPI scaling, these images get upscaled accordingly which is a problem. For high DPI, I would prefer to use higher resolution images to make them look as crisp as possible.
As expected, a naive approach of simply providing the images in higher resolution does not work - it makes images and controls twice as large (on both low and high DPI screens).
Qt documentation on QImageReader states that
The high resolution version is marked by the suffix #2x on the base name. The image read will have its device pixel ratio set to a value of 2.
However, I have provided these resources with twice the resolution, added them to qrc file, but the images displayed are still the same on device with pixel_ratio of 3.
Are there any other steps needed to make this automatic image loading work with stylesheets?
Related
How do we have QT Quick QML application render properly on different screen sizes?
For example screen size 800x600 and 1280x720.
So how should I build the screen so that it looks the same on both the screens?
I know we have to use Anchors and avoid X, Y fixed location.
But we need to provide Height and Width for the components and that is causing the issue for displaying the same component on two different screens.
I have read the below links:
https://doc.qt.io/qt-5/scalability.html
https://doc.qt.io/archives/qt-4.8/scalability.html
QT recommends, building two separate parent layouts sounds illogical.
Is the scaling approach is the way to go?
QML fit screen on all resolutions
Normally, when I deal with scalability it's in a smartphone environment which has much more variableness to it than your situation but I think the same principles hold.
Generally speaking, I don't think you should try to scale the logical pixel ratio of the entire user interface. I think it should stay at 1:1 where one logical screen unit (pixel) in QML equals one logical screen unit (pixel) outside of QML.
The main reason for this is you are normally going to have at least two different aspect ratios: in your case 800x600 is 4:3 and 1280x720 is 16:9. When the aspect ratios do not match, you either have to clip content or have black bars on the sides if your UI itself chooses one or the other of those aspect ratios and tries to display full screen on both (assuming you don't want to stretch the image of the UI in either direction - which almost never makes sense for a UI).
So, what I normally do is use RowLayout and ColumnLayout with fillWidth and fillHeight set appropriately on the UI itself so that appropriate interior areas of the UI stretch to accommodate greater or lesser space as the screen size changes. It's the same principle used in web apps to accommodate various window sizes.
One great way of doing this is to build the app to your desktop environment (Mac or Windows) and just resize the window and see how your layout adapts to different screen sizes. Once you've got it laying out rationally for the range of sizes you have to deal with, then you can build for your target environment and it should run fine.
I am developing a Qt QML based application that runs on both Desktop and Mobile operating systems. I am having problems with proper fonts and components scaling- what does look good on large, desktop monitor is barely visible on a mobile phone, even though the scaling is the same.
I was wondering, what is the proper approach for this problem? I would like to run the same code on all platforms. For example, is there a way for a font to stay the same size (in mm or inches), no matter the screen resolution and size?
In QML I am always setting the font.pointSize property. It is scaled evenly, but because of that, the font are barely visible on mobile devices.
Have you tried font.pixelSize property yet?
I think this will be good for you.
"Sets the font size in pixels.
Using this function makes the font device dependent. Use pointSize to set the size of the font in a device independent manner."
https://doc.qt.io/qt-5/qml-qtquick-text.html#font.pixelSize-prop
I am currently developing an application. And my development computer resolution is 1366*768, I know how to make the application size auto resizable according to resolution. But the problem is in the labels used in application. If the resolution of the monitor is less than 1366*768, the labels becomes cramped and unable to see text, if i keep the font low, and if i use higher resolution monitor the text in those labels looks very tiny. I know one solution that is to check the resolution of monitor during the application start and assign appropriate font to the labels dynamically. But this method takes more time as i have to first create a list of resolution with appropriate font by using trial and error method. And this also requires lot of code. I was wondering if there is any feature or function in qt that i am unaware of to auto scale the font as per resolution. Thanks!
I'm creating the splash images for my app but I've got some questions:
Can I create of all them at once? I've seen some repos for this but
there are not updated with new sizes (e.g 2048x1536)
Would be possible to use the same image for different sizes?
My phone is 1920x1080 but there is no splash screen size for that so it takes one and applies a disproportionate image. If I've got
text on it it looks really badly.
What happens with the rest of images when I build? Meteor removes the rest? Are located in resources/splash.
Could be possible to use a html with css page?
Thanks in advance!
Next time you should consider splitting your questions into independent posts.
Creating splash images: if your image is simple enough, indeed you have many scripts that can generate the different sizes automatically. I am sure you can customize them to fit your sizes.
Use the same image for different sizes: in general, that would mean your image will be stretched by the device to fill the screen. On Android, you can define a 9-patch PNG that will tell the device which pixels can be stretched, so that some part(s) of your image is not deformed.
Deformed image for 1920x1080 screen: depending on the pixel density, there should very probably be a placeholder for that.
Storage of image versions: by default, all versions (i.e. sizes) are packaged within your APK / APP, so that whatever the device needs will be available once user has downloaded the app.
Using an HTML+CSS page for splash screen: in general, no, but it depends on what you want to do with your splash screen. Meteor calls it "launch screen", because it is the first thing it displays while the app is loading / "launching". In particular, the WebView and local server may not be ready yet, and cannot serve any HTML/CSS. That is why you have to use a plain image, which is passed to a very simple activity while the app is loading. But some people also use a "waiting screen" between some parts of their app. In that case, your WebView and local server are already loaded, and you can simply use whatever you want.
Update:
For Android, if 9-patch PNG does not fit your need, you can also try to request Cordova's splashscreen plugin to maintain the aspect ratio of your image:
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-splashscreen/#preferences
<preference name="SplashMaintainAspectRatio" value="true" />
"SplashMaintainAspectRatio" preference is optional. If set to true, splash screen drawable is not stretched to fit screen, but instead simply "covers" the screen, like CSS "background-size:cover". This is very useful when splash screen images cannot be distorted in any way, for example when they contain scenery or text. This setting works best with images that have large margins (safe areas) that can be safely cropped on screens with different aspect ratios.
In Meteor, you would use App.setPreference in mobile-config.js:
App.setPreference("SplashMaintainAspectRatio", true, "android");
I have created a qt project that take screen shot of the desktop and compress it .As far as I have taken the screen shot and scaled it.The original screen shot is about 230kb size and while scaling it reduced up-to 60kb.My problem is I need to reduce the saved jpg to around 5-10kb size without loosing it's clarity of the image.I have searched for the libraries and add-on in Qt but couldn't find any one that could give the possible result
Is there any algorithms ,examples or compression techniques that could reduce the size drastically without loosing its clarity?
OS:Ubuntu 14.04,
qt-5.2.1
.Here given below code I have done so far:
int widthToScale= (QApplication::desktop()->screenGeometry().width()*60)/100;
int heightToScale=(QApplication::desktop()->screenGeometry().width()*60)/100;
QPixmap p=QPixmap::grabWindow(QApplication::desktop()->winId(),0,0,
QApplication::desktop()->screenGeometry().width(),
QApplication::desktop()->screenGeometry().height()).scaled(widthToScale,heightToScale,Qt::KeepAspectRatio,Qt::SmoothTransformation);
p.save(localpath,0,65);