Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFontDialog::getFont() doesn't use correct font style

QFontDialog::getFont() doesn't use correct font style

Scheduled Pinned Locked Moved Unsolved General and Desktop
29 Posts 3 Posters 10.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P pbe

    On 2nd glance, maybe it's really better to reply here.
    So here comes by better example:

    QFontDialog::getFont() doesn't use the correct font style when the font of the font parameter uses toString() and fromString().
    It always uses the first font style in the list instead of the style (normal) and weight (regular) of the QFont parameter.
    I made an example:

    bool ok;
    
    // select a font where `Regular` is not the first item in the `Font
    // style` selector (e.g. `Noto Sans`) and select `Regular`
    QFont font = QFontDialog::getFont(&ok);
    if (ok) {
        // looks like `QFont( "Noto Sans,11,-1,5,50,0,0,0,0,0" )`
        qDebug() << "'font': " << font;
    
        QFont font2;
        // I originally store the font to the settings and load it again, but
        // that's not even needed to reproduce this problem
        font2.fromString(font.toString());
        // still looks like `QFont( "Noto Sans,11,-1,5,50,0,0,0,0,0" )`
        qDebug() << "'font2': " << font2;
    
        // now `Bold Italic` is selected, because it's the first item
        QFont font3 = QFontDialog::getFont(&ok, font2, this);
    
        if (ok) {
            // would now look like `QFont( "Noto Sans,11,-1,5,75,1,0,0,0,0" )`
            qDebug() << "'font3': " << font3;
        }
    }
    

    I was testing under:

    Platform: linux
    Operating System: KDE neon User Edition 5.7
    Build architecture: x86_64
    Current architecture: x86_64
    Qt Version (build): 5.7.0
    Qt Version (runtime): 5.7.0

    (The problem was initially reported by a user of QOwnNotes at https://github.com/pbek/QOwnNotes/issues/239.)

    Am I doing something wrong?
    Thanks a lot for your help!

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #13

    @pbe
    Hi,
    There's a bug - QTBUG-52741 that appears to be what you're describing. I also run your example through the debugger after I was able to reproduce (Qt 5.7) and the problem is indeed in the dialog, here.

    The dialog matches the family only, and when it calls setCurrentItem it selects the very first item in the model (which has the default style), which in turn will overwrite the member that holds the style and you get what you described. If you use the font object directly you won't get the bug, i.e.:

    QFont font = QFontDialog::getFont(&ok);
    if (ok)  {
        // ...
        QFont font2 = QFontDialog::getFont(&ok, font, this); //< Works correctly, no bug
        // ...
    }
    

    Kind regards.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    1
    • P Offline
      P Offline
      pbe
      wrote on last edited by
      #14

      @kshegunov, I already found that bug and was wondering if that's what's affecting QOwnNotes. Thank you for making that clear!

      Yes, you are right. If you use the font directly it works, but it doesn't work with font2.fromString(font.toString()); that is needed to store and load it to/from the settings. Do you have any ideas to get around this?

      kshegunovK 1 Reply Last reply
      0
      • P pbe

        @kshegunov, I already found that bug and was wondering if that's what's affecting QOwnNotes. Thank you for making that clear!

        Yes, you are right. If you use the font directly it works, but it doesn't work with font2.fromString(font.toString()); that is needed to store and load it to/from the settings. Do you have any ideas to get around this?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #15

        @pbe said:

        Do you have any ideas to get around this?

        Sadly, no. The bug seems inherent to the QFontDialog class when the QFont's internal identifier isn't set (i.e. when the dialog needs to go and query about the font by its attributes). If I get some more time in the coming days, I'll try to run it again and hopefully will have something better.

        Kind regards.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • P Offline
          P Offline
          pbe
          wrote on last edited by
          #16

          @kshegunov, thank you very much! You are my hero!

          kshegunovK 1 Reply Last reply
          0
          • P pbe

            @kshegunov, thank you very much! You are my hero!

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #17

            @pbe
            I have a bug fix and I'll try to submit it this evening (I've been working in the dev branch, so some git magic is needed).

            Kind regards.

            Read and abide by the Qt Code of Conduct

            P 1 Reply Last reply
            1
            • kshegunovK kshegunov

              @pbe
              I have a bug fix and I'll try to submit it this evening (I've been working in the dev branch, so some git magic is needed).

              Kind regards.

              P Offline
              P Offline
              pbe
              wrote on last edited by
              #18

              @kshegunov, really? That's wonderful! I can't wait to see it!

              kshegunovK 1 Reply Last reply
              0
              • P pbe

                @kshegunov, really? That's wonderful! I can't wait to see it!

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #19

                @pbe
                https://codereview.qt-project.org/#/c/165958/

                We'll see if it gets approved.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  pbe
                  wrote on last edited by
                  #20

                  Thanks a lot, I will watch that request!

                  kshegunovK 1 Reply Last reply
                  0
                  • P pbe

                    Thanks a lot, I will watch that request!

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #21

                    @pbe
                    There's something else, while investigating your bug I stumbled upon this one ... which you may actually encounter, although it's somewhat unrelated to your problem with the dialog.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pbe
                      wrote on last edited by
                      #22

                      @kshegunov, that really is what I'm encountering... So actually two bugs are triggered here...

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pbe
                        wrote on last edited by
                        #23

                        Really thanks a lot for your help!

                        kshegunovK 1 Reply Last reply
                        0
                        • P pbe

                          Really thanks a lot for your help!

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #24

                          @pbe
                          QTBUG-54936's been merged and the patch will be incorporated in Qt 5.8

                          Kind regards.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            pbe
                            wrote on last edited by
                            #25

                            @kshegunov, great news! Thanks a lot!

                            kshegunovK 1 Reply Last reply
                            0
                            • P pbe

                              @kshegunov, great news! Thanks a lot!

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by
                              #26

                              @pbe
                              My pleasure. The dialog bug is low(er) priority and it may take some time for the proposed patch to get reviewed.

                              Kind regards.

                              Read and abide by the Qt Code of Conduct

                              P 1 Reply Last reply
                              0
                              • kshegunovK kshegunov

                                @pbe
                                My pleasure. The dialog bug is low(er) priority and it may take some time for the proposed patch to get reviewed.

                                Kind regards.

                                P Offline
                                P Offline
                                pbe
                                wrote on last edited by
                                #27

                                @kshegunov I wonder if it will get fixed at all...

                                kshegunovK 1 Reply Last reply
                                0
                                • P pbe

                                  @kshegunov I wonder if it will get fixed at all...

                                  kshegunovK Offline
                                  kshegunovK Offline
                                  kshegunov
                                  Moderators
                                  wrote on last edited by
                                  #28

                                  @pbe
                                  It's going to, it'ill just take some time. Now things a bit slow because most people are on vacation.

                                  Read and abide by the Qt Code of Conduct

                                  P 1 Reply Last reply
                                  0
                                  • kshegunovK kshegunov

                                    @pbe
                                    It's going to, it'ill just take some time. Now things a bit slow because most people are on vacation.

                                    P Offline
                                    P Offline
                                    pbe
                                    wrote on last edited by
                                    #29

                                    @kshegunov, humans... :)

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved