-1

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.

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
Kapitan
  • 73
  • 8

1 Answers1

0

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 :

  1. From GUI, can change the font of all widgets in GUI from here :

enter image description here

  1. 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.

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
  • 2
    Thanks, I was surprised it was not already used in the project. But that does not seem to solve the issue. As I need to know what font size will be used in menu, when creating QLabel. The custom widget in menu has, amongst others, QLabel. I need the QLabel to have the same font as the menu. How to do that? – Kapitan Oct 18 '21 at 10:57
  • After edit: Sure, those are quite obvious things. But how to know the font that QMenu will use while painting the (context) menu while creating QLabel dynamicaly? What you suggested is pretty obvious and not answering the question. – Kapitan Oct 18 '21 at 12:58
  • so I didn't understand what you exactly want. add your project or create an example. – Parisa.H.R Oct 18 '21 at 14:27
  • I want to know what font size QMenu will use before its paint event. – Kapitan Oct 18 '21 at 19:35
  • @Kapitan ,it's 11. – Parisa.H.R Oct 18 '21 at 20:03
  • if you didn't change the default font , it's 11 . but this can be changed by stylesheet and QFont . if your program use specific `.qss` you should check it too. – Parisa.H.R Oct 18 '21 at 20:34
  • All this is "guessing" or setting it. What if you have a QMenu that someone has set before you? And you need to find it out programmatically? Because it can differ for win, max, linux. For different style. What you write is guessing, not finding out. – Kapitan Oct 19 '21 at 05:18
  • If you add widget inside ui and click on it or if you click on menu in your ui and go to font section like picture that I added you can see the number of font that is default sets.but if you have specific stylesheet , this can be changed . – Parisa.H.R Oct 19 '21 at 07:25
  • Seems like you do not understand me. No UI. Or at least it does not matter. You have instance ov QMenu in code. You do not care where it came from. You write the code in python, C++, whatever. The menu might originate in UI, it might be created programmatically. YOu should not care. YOu have just the instance of QMenu. How you find out the font that will be used? You can call menu.font() but that returns default font before painting the element. – Kapitan Oct 19 '21 at 09:23
  • No UI. For this question just forget about any UI file. Just code. Lets say someone just wrote the code. It can be created programmatically, treat it as if it was. (Which is the case. But you seem to be stuck on design tools.) – Kapitan Oct 19 '21 at 09:24
  • 1
    You should be able to get the font associated with a menu action using [`QAction::font`](https://doc.qt.io/qt-5/qaction.html#font-prop). – G.M. Oct 19 '21 at 09:57