Results 1 to 9 of 9

Thread: Width for QProgressDialog should be determined by Dialog's Window Title

  1. #1
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Width for QProgressDialog should be determined by Dialog's Window Title

    Hi there,

    I am just implementing a QProgressDialog. Everything works as expected except one strange behavior:

    I cannot manage to ensure that the dialog's horizontal size is big enough to show the complete text of the window title I defined. Here's my code:

    Qt Code:
    1. QProgressDialog progress(QString("Loading %1 Objects").arg(ids.size()), "Abort", 0, ids.size(), this);
    2. progress.setWindowModality(Qt::WindowModal);
    3. progress.setMinimumDuration(500);
    4. progress.setWindowTitle("My really somewhat long Window Title");
    To copy to clipboard, switch view to plain text mode 

    I tried to accomplish this using the setSizePolicy() method but without success. But there should be a simple way to accomplish this ...

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,821
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Width for QProgressDialog should be determined by Dialog's Window Title

    You have to retrieve the dialog's font metrics, determine the pixel length of the string in that font, then resize the dialog's width to be wide enough to contain that plus everything inside the dialog plus the control buttons and icons on the title bar. Otherwise, if the width is too narrow, the title will be elided ("My really somewhat long ...").

    The size policy applies only to the children within the dialog, not the title bar.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    apatwork (4th October 2018)

  4. #3
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Width for QProgressDialog should be determined by Dialog's Window Title

    Thanks a lot, d_stranz! I added these lines of code to the
    progress dialog configuration code shown in my first posting:

    Qt Code:
    1. const QString dlgTitle("My really somewhat long Window Title");
    2. progress.setWindowTitle(dlgTitle);
    3. QFont displayFont(font());
    4. QFontMetrics fm(displayFont);
    5. int widthInPixels(fm.width(dlgTitle) + 120);
    6. progress.setMinimumWidth(widthInPixels);
    To copy to clipboard, switch view to plain text mode 
    I added 120 pixels to ensure that the dialog is wide enough to contain
    the icon on the upper left corner as well as the X-button on the upper
    right corner.

    As you already mentioned I need to take into account the
    width of the contents of the dialog (this will always be narrower than the title
    string) as well as the widths of the icons present in the window
    title. I found this approach:

    Qt Code:
    1. QStyle *style = qApp->style();
    2. QIcon closeIcon(style->standardIcon(QStyle::SP_TitleBarCloseButton));
    To copy to clipboard, switch view to plain text mode 
    Unfortunately I couldn't figure out how to determine the actual width
    of such an icon.
    My assumption is that the icon on the upper left corner of the dialog
    window might have the same size as the X-button.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,821
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Width for QProgressDialog should be determined by Dialog's Window Title

    My assumption is that the icon on the upper left corner of the dialog
    window might have the same size as the X-button.
    That is probably not a bad assumption. The QIcon can tell you what the QSize is. Anyway if it is your dialog, you can add as much padding as you want to the title. By the way QWidget::windowIcon() will return the top-left corner icon if there is any.

    You may also have some problems if the OS has font magnification turned on. I use a big 4K monitor, and have the font magnification set to 125% so I can read it. I have noticed some problems with Qt apps where dialogs did not take this into account so the text was clipped. So you may need to temporarily change the font magnification for your screen to make sure your calculations still work.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    apatwork (4th October 2018)

  7. #5
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Width for QProgressDialog should be determined by Dialog's Window Title

    Phantastic! Thanks a lot once again, d_stranz!

    I already studied the QIcon class's APIs and encountered the method
    actualSize(). But I don't completely understand how to use it:


    Qt Code:
    1. QSize QIcon::actualSize(const QSize &size, QIcon::Mode mode = Normal, QIcon::State state = Off) const
    To copy to clipboard, switch view to plain text mode 
    The documentation says:

    Returns the actual size of the icon for the requested size, mode, and
    state. The result might be smaller than requested, but never
    larger. The returned size is in device-independent pixels (This is
    relevant for high-dpi pixmaps.)

    My problem with that description is that I don't know what is meant
    with "requested size".

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,821
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Width for QProgressDialog should be determined by Dialog's Window Title

    My problem with that description is that I don't know what is meant with "requested size".
    I see your problem. QIcon is designed to return a scaled pixmap for whatever size is requested. You can provide QIcon with several pixmaps to choose from, using QIcon::addPixmap(). If you ask for a size that is in-between the size of one of the pixmaps, it will choose the smaller one, or create one by interpolating or scaling down one of the ones it does have.

    There is another way to get at this, and that is to use QStyle::pixelMetrix() and one of the QStyle::PixelMetric enums: QStyle::PM_TitleBarHeight (and assume width == height for icons), QStyle::PM_TitleBarButtonIconSize, or QStyle::PM_TitleBarButtonSize. The last two are Qt >= 5.8.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. The following user says thank you to d_stranz for this useful post:

    apatwork (5th October 2018)

  10. #7
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Width for QProgressDialog should be determined by Dialog's Window Title

    Whow! Thank you very much for the explanation!

    I now tried the computation using the QStyle object. My code looks as follows (progress is an instance of type QProgressDialog):

    Qt Code:
    1. QStyle *style(qApp->style());
    2. int metricVal(style->pixelMetric(QStyle::PM_TitleBarHeight, &btn, &progress));
    3. QFont displayFont(font());
    4. QFontMetrics fm(displayFont);
    5. int widthInPixels(fm.width(dlgTitle) + 2 * metricVal);
    6. progress.setMinimumWidth(widthInPixels);
    To copy to clipboard, switch view to plain text mode 
    Surprisingly for me this did not lead to a completely visible window title. The resulting minimum width of the QProgressDialog window was still about 20% too small.

    I even tried the other QStyle values you mentioned but they led to an even smaller resulting width. The metricVal values returned were these:

    Qt Code:
    1. QStyle::PM_TitleBarButtonIconSize -- 16
    2. QStyle::PM_TitleBarHeight -- 24
    3. QStyle::PM_TitleBarButtonSize -- 19
    To copy to clipboard, switch view to plain text mode 
    I should mention that I'm developing my Qt application on RedHat Enterprise Linux v. 7.x.

  11. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,821
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Width for QProgressDialog should be determined by Dialog's Window Title

    I wonder if QDialog::font() is returning the default font for the dialog, and that it might be a different font (controlled by the OS) for the title bar? Try changing the default font in Qt Designer (or in your dialog constructor) and see if the title font changes. I would bet that it doesn't.

    I am searching for a Qt class / method that will allow you to retrieve the title bar font, but so far I'm not having any luck.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  12. The following user says thank you to d_stranz for this useful post:

    apatwork (9th October 2018)

  13. #9
    Join Date
    Oct 2017
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Width for QProgressDialog should be determined by Dialog's Window Title

    Hi,

    I agree completely with you. The size of the font being used in the window title is definitively larger than the font size used in the dialog window and it displays bold shape characters.

    I also tried to figure out how I could retrieve the font used in the window title using Qt APIs but until now without success. During my research I learned that the window title's properties are defined by the window manager in Linux and so possibly there might be no API in Qt that allows access to this information.

    Finally I ended up with this code:

    Qt Code:
    1. const QString dlgTitle("My really somewhat long Window Title");
    2. QStyle *style(qApp->style());
    3. int metricVal(style->pixelMetric(QStyle::PM_TitleBarHeight, &btn, &progress));
    4. QFont displayFont(font());
    5.  
    6. // Enlarge font size by one point and switch to bold display.
    7. displayFont.setPointSize(displayFont.pointSize() + 1);
    8. displayFont.setBold(true);
    9. QFontMetrics fm(displayFont);
    10.  
    11. // Add two times the title bar height to the width used up by the text.
    12. int widthInPixels(fm.width(dlgTitle) + 2 * metricVal);
    13.  
    14. progress.setMinimumWidth(widthInPixels);
    To copy to clipboard, switch view to plain text mode 
    I'm not really happy with this solution but at least it works on Linux.

Similar Threads

  1. Replies: 2
    Last Post: 14th October 2016, 11:55
  2. Setting font for window/dialog title
    By rohitkk in forum Qt Programming
    Replies: 0
    Last Post: 21st March 2014, 06:18
  3. Force dialog to have WhatsThis title bar button
    By hvengel in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2013, 23:39
  4. Getting rid of Dialog Title Bar
    By skepticalgeek in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2010, 00:09
  5. QMessageBox - no dialog title on Mac OS?
    By will49 in forum Qt Programming
    Replies: 1
    Last Post: 8th October 2007, 15:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.