Results 1 to 7 of 7

Thread: Unwanted window title elision

  1. #1
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Unwanted window title elision

    When I create a QDialog, the title text of the widget is elided (as in, "Supercalifragi...").

    Is there a flag or some other way to request an ideal dialog size for the title text to not be elided? The contents may be expanded because of this, but this wouldn't be an issue.

    Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unwanted window title elision

    I don't think that is possible since in a lot of windowing systems drawing of window decorations is not in control of the application and the windowing system's decoration handler does not provide the feedback to react to this.

    You might be able to do this in a platform specific way, e.g. using some native API to query which font is used for window titles and then use QFontMetrics to calculate the size of your title text.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Unwanted window title elision

    Hello anda_skoa. I was hoping that there would be something already made, it would save a lot of time, but alas.
    I did what you proposed, estimating the title bar width with the OS API.

    The QDialog created, as it is. The width is given by its QLayout. If the caption text exceeds that, Windows puts an ellipsis at the end:



    The QDialog with a 'minimumWidth' set as the estimated title bar width. The title seems to fit:



    The main reference was this:
    - http://stackoverflow.com/questions/1...ext-in-c-sharp

    But it's written in C#, so I had to use GDI32 and User32 functions.
    The meat of the functionality is this:

    Qt Code:
    1. #include <windows.h>
    2. #include <QString>
    3.  
    4.  
    5. int getRecommendedWidthWin32( const QString* titleText, int totalSystemButtons )
    6. {
    7. // const QString* titleText = The desired caption text for the title bar.
    8. // int totalSystemButtons = The amount of system buttons in the title bar (i.e close button, minimise button etc.).
    9.  
    10. // Query the operating system metrics.
    11.  
    12. int finalWidth = 0;
    13.  
    14. NONCLIENTMETRICS metrics;
    15. ZeroMemory( &metrics, sizeof( NONCLIENTMETRICS ) );
    16. metrics.cbSize = sizeof( NONCLIENTMETRICS );
    17.  
    18. if ( SystemParametersInfo( SPI_GETNONCLIENTMETRICS, 0, &metrics, 0 ) != 0 )
    19. {
    20. // Add the width of caption buttons.
    21. // Buttons have at least this total width or less.
    22.  
    23. finalWidth += metrics.iCaptionWidth * totalSystemButtons;
    24.  
    25. // Add the width of the frame border (left and right, so twice).
    26. // This is commented out as it seems redundant.
    27.  
    28. //finalWidth += metrics.iBorderWidth * 2;
    29.  
    30. // Add the 'estimated' width of the caption text.
    31.  
    32. HFONT tempFont = CreateFontIndirect( &metrics.lfCaptionFont );
    33. HDC hDC = GetDC( NULL ); // Device context for the whole screen.
    34.  
    35. SelectObject( hDC, tempFont );
    36.  
    37. SIZE textPixelSize;
    38. GetTextExtentPoint32( hDC, reinterpret_cast< LPCWSTR >( titleText->utf16() ), titleText->size(), &textPixelSize );
    39.  
    40. ReleaseDC( NULL, hDC );
    41. DeleteObject( tempFont );
    42.  
    43. finalWidth += textPixelSize.cx;
    44. }
    45.  
    46. return finalWidth;
    47. }
    To copy to clipboard, switch view to plain text mode 
    For that dialog, the call was this:

    int minimumWidth = getRecommendedWidthWin32( "Supercalifragilisticexpialidocious", 3 ) // Minimise, maximise and close buttons.

    It's important to set the minimum width of your dialog to the 'qMax' between the layout sizehint and that estimated title bar width, so you use the biggest minimum width possible:

    setMinimumWidth( qMax( minimumWidth, layout->sizeHint().width() ) );
    Last edited by Kryzon; 27th September 2015 at 06:09.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unwanted window title elision

    You could probably also use a spacer item to "add" the title width constraints to the usual layouting mechanism.

    Depending on the size policy this could allow uses to make the window smaller if they wanted to, while still having the fully expanded dialog by default.

    Btw, it is usually not custom to pass a QString by pointer, i.e. by value or const reference are what you will find in most cases.
    Avoids either having to rely on the caller or having checks inside the callee.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Unwanted window title elision

    I set it as a pointer to avoid having to #include QString in the header file, but now that you mentioned it, I realise it's unnecessary.

    QObject includes the QString header. So any widget code that will make use of that helper function will have it included already.
    I will change it to a const reference.
    Regards.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unwanted window title elision

    Forward declaration also works if the argument is a const reference :-)

    Cheers,
    _

  7. #7
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: Unwanted window title elision

    I did not know that. It appears that you can use references to incomplete types as members and parameters.
    http://stackoverflow.com/a/553869/4206247

    I suppose I can improve some unrelated code based on that.

    - - - - - -

    I noticed that on Windows 8.1 when you drag-resize the dialog beyond a certain point, the caption text becomes centered. In my opinion it looks more pleasant with the text centered.
    So I added the following to detect if the OS version is above 8.0 and add just enough space for the caption text to be centered (it's twice the button sizes).

    Qt Code:
    1. // Returns 'true' if this is Windows 8 and above, 'false' if not.
    2.  
    3. static bool setIsWindows8()
    4. {
    5. OSVERSIONINFOEX osInfoEx;
    6. osInfoEx.dwOSVersionInfoSize = sizeof( OSVERSIONINFOEX );
    7. GetVersionEx( reinterpret_cast< LPOSVERSIONINFOW >( &osInfoEx ) );
    8.  
    9. return ( osInfoEx.dwMajorVersion >= 6
    10. && ( osInfoEx.dwMinorVersion >= 2 || osInfoEx.dwMajorVersion > 6 )
    11. && osInfoEx.wProductType == VER_NT_WORKSTATION );
    12. }
    13.  
    14.  
    15. int getRecommendedWidthOS( const QString& titleText, int totalSystemButtons )
    16. {
    17. // Query the operating system metrics.
    18.  
    19. int finalWidth = 0;
    20.  
    21. NONCLIENTMETRICS metrics;
    22. ZeroMemory( &metrics, sizeof( NONCLIENTMETRICS ) );
    23. metrics.cbSize = sizeof( NONCLIENTMETRICS );
    24.  
    25. if ( SystemParametersInfo( SPI_GETNONCLIENTMETRICS, 0, &metrics, 0 ) != 0 )
    26. {
    27. // Add the width of caption buttons.
    28. // Buttons have this total width or less.
    29.  
    30. finalWidth += metrics.iCaptionWidth * totalSystemButtons;
    31.  
    32. // Determine if the Windows version is at least 8, so we
    33. // can centre the caption text.
    34.  
    35. static bool isWindows8 = setIsWindows8();
    36. if ( isWindows8 )
    37. finalWidth *= 2; // Double the button width to make the title centered.
    38.  
    39. // ...
    To copy to clipboard, switch view to plain text mode 
    The rest of the code remains the same.

Similar Threads

  1. How to set the window title color?
    By bibbinator in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2015, 14:37
  2. Window Title
    By Archa4 in forum Newbie
    Replies: 5
    Last Post: 22nd February 2011, 08:13
  3. how to remove the window title bar?
    By tsuibin in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2009, 03:17
  4. Remove window title
    By marcel in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2007, 05:06
  5. window title bar
    By moowy in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2006, 16:56

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.