QMenu font size for different DPI for windows - qt

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.

Related

How to change logical DPI for QML app?

I want to control the pointSize-to-pixel scaling of all Text elements in my Qt 5.2.1 QML/C++ app.
I have a QML singleton component with target display properties like width, height and dot pitch that calculates appropriate pixel sizes for common dimensions such as the recommended size in pixels for a touchscreen button. This works fine for controls I write, but the Text element has a perfectly good font.pointSize that I would like to use if I could just set the logical DPI used for text scaling.
I use this to simulate target devices with very different screen DPI while debugging on my 96 DPI development screen. For example, I would like to run my app that targets a 1280x720 133DPI display and has an element like:
Text { font.pointSize: 72; text: “Xy” }
display 133 pixels tall, not the 96 pixel tall text I get because the OS tells Qt that I have a 96 DPI monitor attached. I want to override the logical DPI scaling for my application.
I can see the logicalDotsPerInchX through the QGuiApplication QScreen list. There is tons of documentation on how to get the logical DPI. But I cannot find any information on how to change it.
How can I change the logical DPI for my Qt app?
I don't think you can simply change the DPI values in the QScreen class (there are only public getter methods).
Maybe there are some "hacks" for that problem if you modify the QScreen class and add a public setter or something yourself and then recompile Qt, but that might be some work and takes a lot of time...
Another solution might be to just add a scale factor to your font sizes? I don't know how many you have but that can be simply done in QML and I use something like that even for production setting to scale the fonts and other sizes properly to the device.
You could use Screen.pixelDensityor just define your custom QML property and multiply that to all font sizes? if you do that you can dynamically change the sizes while the app is running.

Why does FlowLayout behave differently in Qt 5.2, compared to Qt 4.8?

I've just ported my application from Qt 4.8.4 to Qt 5.2.1. I have an issue with the FlowLayout class as provided in the FlowLayout example code in the Qt docs.
I have a QMainWindow with a QDockWidget docked at the bottom of the central widget.
The QDockWidget has a FlowLayout with several child widgets. In Qt 4.8, this worked like a charm, the size of the child widgets fitted the standard size of the DockWidget. However, in Qt 5.2, the DockWidget tries to increase its size to the maximum (taking the place from the central widget). Changing its layout prevents this unwanted behavior. But of course, I use FlowLayout on purpose.
To illustrate the problem, I created a minimal example:
The constructor of the DockWidget:
QGroupBox *generalBox = new QGroupBox("");
generalBoxLayout = new FlowLayout;
generalBox->setLayout(generalBoxLayout);
for(int i=0; i<10; ++i)
{
QPushButton *button = new QPushButton("Test", this);
button->setMinimumWidth(100);
button->setMinimumHeight(100);
generalBoxLayout->addWidget(button);
}
this->setWidget(generalBox);
Does someone know what the problem is and how I can solve it?
Edit
I've created a new minimal working example and unfortunately cannot reproduce the discrepancy between Qt 4.8 and Qt 5.2. The same problem shows up in Qt 4.8, so I would still like to present it here:
Initial view showing the bottom-docked dockwidget taking the whole vertical space:
View after resizing the dockwidget with the mouse
View after resizing the mainwindow with the mouse
These screenshots show that the dockwidgets behaves as expected after changing the size of the dockwidget manually. However, on initialization, the widget takes all available space from central widget, which is not desired.
Does someone know of a solution / workaround?
This is the answer given by Marek R. I'm putting it here so this question gets an answer. For too long it has been masquerading as an unanswered question.
I see the problem now. I did some experiments and research. It looks
like some bug in Qt. FlowLayout::heightForWidth is always called
with same width value (in my case 103) independently on main window
size and this leads height bigger then desired (usually window is much
wider). Also it is not called when width of main window is changing
(it suppose to to update height of dock area).

QDialogs using layouts but with a fixed size (no size grip)

I have several QDialogs that I have made in Qt Designer that use layouts, so the dialog can size itself correctly depending on the size of its children. The size of its children is run-time dependent (variable text fields, system font size etc).
It does not make sense for my dialog to be user-resizeable, so I want the size grip disabled. As far as I can tell, the size grip is disabled by calling setFixedSize(). However, I cannot put in a pixel size here, so I have implemented resizeEvent() like so:
void cRemoteConnectionDialog::resizeEvent(QResizeEvent *)
{
setFixedSize(minimumSize());
}
This works fine on Windows, but the dialog is far to small on Mac OS X.
How can I achieve a program resizable but not user resizeable QDialog that takes up the minimum space it requires based on run-time state?
Try something like this on your dialog:
this->layout()->setSizeConstraint(QLayout::SetFixedSize);

Prevent Subwindow Resize

:D
I'm a Qt beginner and I have some problems.
Generally, all the languages and tools that have an GUI builder have for the window the "Resizeable" property.
Very well, I need to do the same in Qt:
No window resize, no resize pointer in hover the window's border, maximize button (in the window's title) disabled.
I'm tried to find something but unsuccessfully.
If someone help me, I'll be grateful.
Hug.
Sorry for my english. I'm brazilian. :)
In Qt Creator, I think you can create a non-resizable window by giving it the same minimum and maximum size.
Or you can do it by code using setFixedSize. In that case, just add this line in your window's constructor:
setFixedSize(size());
Use void QWidget::setWindowFlags ( Qt::WindowFlags type ). see enum Qt::WindowType for the list of flags you can play with.
For instance, setting Qt::Dialog | Qt::FramelessWindowHint should produce a windows without resize frame and no max/min button.

Differing DPI [font sizes] in QWebView compared to all other QWidgets?

I have a QWebView which displays some HTML content and I used CSS to style the text:
body { font-size: 10pt; }
The window with the QWebView also has a QTextEdit field, for which I have set the font like this:
QFont newFont;
newfont.setPointSize(10);
myEditField->setFont(newFont);
Unfortunately, the text displayed in the QWebView is slightly larger than the text displayed in the QTextEdit. I have a feeling that this has something to do with DPI settings being different in the QWebView.
Is there a way to get the same font sizes displayed for both the QWebView and the QTextEdit?
Thanks!
Explanation of this behavior has been given on the bugreports:
It appears to be true that WebKit assumes 96 dpi as a fixed resolution. If this is how web content is designed then we have a problem changing that, because there are other people that expect WebKit to render web content like in web browsers.
See also https://www.webkit.org/blog/57/css-units/
They suggested two solutions:
QWebView provides setZoomFactor and setTextMultiplier which I believe could be used to get the desired behaviour (matching the QWidget ).
You can calculate zoom factor and text multiplier using current DPI:
QWidget* window = QApplication::desktop()->screen();
const int horizontalDpi = window->logicalDpiX();
m_view->setZoomFactor(horizontalDpi / 96.0);
Using QWebSettings::ZoomTextOnly you can apply zooming to the text only.
This is a little tricky. I found a good way to get accurate DPI measurements is from QApplication, example:
QWidget* window = QApplication::desktop()->screen();
int horizontalDpi = window->logicalDpiX();
As far as font goes, you can use QWidget::fontMetrics() to get a good font metrics information.
I think a combination of both will give you some consistency between your web view and text edit.
Good luck.

Resources