Results 1 to 10 of 10

Thread: QLineEdit setAlignment doesn't set the Vertical alignment

  1. #1

    Default QLineEdit setAlignment doesn't set the Vertical alignment

    This is my code below:
    Qt Code:
    1. MainWindow::MainWindow(CoreDictionary const& dict)
    2. : QMainWindow(nullptr), dict{dict}
    3. {
    4. ui.setupUi(this);
    5. ui.searchField->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
    6. }
    To copy to clipboard, switch view to plain text mode 

    the Qt::AlignRight is there not because I need it, but to prove the function is taking effect - the problem though is that Qt::AlignVCenter has no effect, I cannot get the text to align vertically, here's an image of the result:

    Immagine 2025-01-21 011540.png

    Before you ask, I'm sure I'm not accidentally changing the Vertical Alignment anywhere else in the code, this because is a new project and that is pretty much all my code...
    Last edited by Marcus Aseth; 21st January 2025 at 01:23.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,287
    Thanks
    310
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Your example is a bit misleading because it shows only lowercase letters, and only letters with no ascenders (like "h") or descenders (like "g"). The vertical center is almost certainly calculated using the ascent and descent limits for the entire font, not just the text on display. Think about that - if only the actual text in the QLineEdit was used to calculate vertical alignment, then the vertical location could jump up and down depending on what was on display. Probably not what you'd want.

    Look at the QFontMetrics, especially the QFontMetrics::ascent() and QFontMetrics::descent() values (or QFontMetrics::height(), which is the sum of these). You will probably find that half the height corresponds to the center of your QLineEdit, taking into account whatever margins or padding are used in the line edit.

    Alternatively, it is possible that the vertical alignment is set to the font's baseline, which corresponds to QFontMetrics::descent() above the bottom margin of the line edit. Either way, it will be fixed no matter what text is displayed.
    Last edited by d_stranz; 21st January 2025 at 03:41.
    <=== 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. #3

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Quote Originally Posted by d_stranz View Post
    Your example is a bit misleading because it shows only lowercase letters, and only letters with no ascenders (like "h") or descenders (like "g"). The vertical center is almost certainly calculated using the ascent and descent limits for the entire font, not just the text on display. Think about that - if only the actual text in the QLineEdit was used to calculate vertical alignment, then the vertical location could jump up and down depending on what was on display. Probably not what you'd want.

    Look at the QFontMetrics, especially the QFontMetrics::ascent() and QFontMetrics::descent() values (or QFontMetrics::height(), which is the sum of these). You will probably find that half the height corresponds to the center of your QLineEdit, taking into account whatever margins or padding are used in the line edit.

    Alternatively, it is possible that the vertical alignment is set to the font's baseline, which corresponds to QFontMetrics::descent() above the bottom margin of the line edit. Either way, it will be fixed no matter what text is displayed.
    I've attempted adding letters with ascenders, descenders, uppercase and zeroing out margins and padding, this was the code:
    Qt Code:
    1. MainWindow::MainWindow(CoreDictionary const& dict)
    2. : QMainWindow(nullptr), dict{dict}
    3. {
    4. ui.setupUi(this);
    5. ui.searchField->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
    6. ui.searchField->setText(QString("Type something here!"));
    7. ui.searchField->setTextMargins(QMargins(0, 0, 0, 0));
    8. ui.searchField->setStyleSheet("QLineEdit { padding: 0px 0px 0px 0px; }");
    9. }
    To copy to clipboard, switch view to plain text mode 
    And this the result:
    Immagine 1.png

    Is that really Qt::AlignVCenter working as intended?

    I've also tried changing the setTextMargin line with this:
    Qt Code:
    1. ui.searchField->setTextMargins(QMargins(0, 0, 0, 6));
    To copy to clipboard, switch view to plain text mode 
    And this is the result:
    Immagine 2.png

    So by ignoring the setAlignment and raising the bottom margin instead I can get the text reasonably centered on the QLineEdit, the problem now is that the cursor (to the right of the exclamation mark) gets raised as well, so now that is not centered... I need help finding a solution :\
    What I would like in the end is for the text and the cursor to be centered on the QLineEdit window
    Last edited by Marcus Aseth; 21st January 2025 at 06:39.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,287
    Thanks
    310
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Because you are using a dark mode, I can't really see what the boundaries of your line edit are. Try adding a 1px white border to your style sheet so you can tell what is actually happening.

    Qt Code:
    1. QLineEdit { border: 1px solid white }
    To copy to clipboard, switch view to plain text mode 
    <=== 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.

  5. #5

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Quote Originally Posted by d_stranz View Post
    Because you are using a dark mode, I can't really see what the boundaries of your line edit are. Try adding a 1px white border to your style sheet so you can tell what is actually happening.

    Qt Code:
    1. QLineEdit { border: 1px solid white }
    To copy to clipboard, switch view to plain text mode 
    Sure - to recap, the code now looks like this:
    Qt Code:
    1. MainWindow::MainWindow(CoreDictionary const& dict)
    2. : QMainWindow(nullptr), dict{ dict }
    3. {
    4. ui.setupUi(this);
    5. ui.searchField->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
    6. ui.searchField->setText(QString("Type something here!"));
    7. ui.searchField->setTextMargins(QMargins(0, 0, 0, 6));
    8. ui.searchField->setStyleSheet("QLineEdit { padding: 0px 0px 0px 0px; border: 1px solid white;}");
    9. }
    To copy to clipboard, switch view to plain text mode 

    And the result is this (side by side the version with bottom margin 0 and bottom margin 6):
    Attachment 13851

    Also a thing worth noticing is that the "g" bottom part is being clipped when the bottom margin is raised (right picture), with and without Qt::AlignVCenter which is another thing I find strange, because one would think the text has still room to be moved upward without clipping - but probably that issue is besides the point because probably I don't need to be messing with the margin to begin with (although I have no idea)

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,287
    Thanks
    310
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Your attachment link is not valid, so I can't see your screenshot. Next time you post, click "Go Advanced" so you can see what your post will look like and verify that any links work. Maybe you didn't actually upload your image?

    I don't need to be messing with the margin to begin with (although I have no idea)
    I guess I am really puzzled as to why the behavior of the line edit bothers you so much that you are spending all this time trying to "fix" it? If you really want complete control of how the line edit positions the text, then you should derive from it and rewrite the paintEvent() method to position the text rectangle where you want it.
    <=== 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.

  7. #7

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Quote Originally Posted by d_stranz View Post
    Your attachment link is not valid, so I can't see your screenshot.
    should work now?
    immagine 3.png

    Quote Originally Posted by d_stranz View Post
    I guess I am really puzzled as to why the behavior of the line edit bothers you so much that you are spending all this time trying to "fix" it?
    Mostly because for me it's an hobby, so I don't mind making slow or no progress at all, as long as the final result is what I've envisioned.
    If I can't even make it happen with Qt, I should probably pick up "Programming Windows - 5th Ed" and learn how to make everything from the ground up, I feel

    Quote Originally Posted by d_stranz View Post
    If you really want complete control of how the line edit positions the text, then you should derive from it and rewrite the paintEvent() method to position the text rectangle where you want it.
    I would like to, but I need to see how it was implemented in the first place and stepping through the drawing process inside of Visual Studio, the problem is that I cannot see those parts of the code.
    Or, if I right click and choose "go to definition" I get this type of error:
    Immagine 2025-01-23 024835.png
    Last edited by Marcus Aseth; 23rd January 2025 at 03:29.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,287
    Thanks
    310
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Or, if I right click and choose "go to definition" I get this type of error:
    Do you have the Qt VS Tools extension installed? I think that adds the paths to Qt sources to your Visual Studio configuration. When I right-click on "QString" in my source code, for example, and select "Peek Definition" it goes right to the qstring.h header file.
    <=== 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. #9

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Quote Originally Posted by d_stranz View Post
    Do you have the Qt VS Tools extension installed? I think that adds the paths to Qt sources to your Visual Studio configuration. When I right-click on "QString" in my source code, for example, and select "Peek Definition" it goes right to the qstring.h header file.
    Yes, and it does that for me too, but that's the declaration of most stuff, not the definition/implementation (the thing that usually resides in a .cpp file).
    You've suggested to rewrite the paintEvent() function, try and reach the body of that function through VS, you'll likely get my same error.

    Immagine 2025-01-24 042741.png
    Last edited by Marcus Aseth; 24th January 2025 at 04:29.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,287
    Thanks
    310
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLineEdit setAlignment doesn't set the Vertical alignment

    Yes, you're right. Even if I add the absolute path to the directory containing qstring.cpp to the VC++ Directories in project settings, it still goes only to the header file. I've asked online before and I don't think there is any way to add source code browsing unless the files are part of your project "solution".
    <=== 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.

Similar Threads

  1. QLineEdit doesn't recive any character
    By nemaider in forum Newbie
    Replies: 1
    Last Post: 2nd June 2021, 10:45
  2. Replies: 0
    Last Post: 5th September 2019, 18:01
  3. Replies: 2
    Last Post: 9th July 2016, 11:59
  4. Replies: 1
    Last Post: 11th March 2014, 12:20
  5. Replies: 0
    Last Post: 24th October 2011, 13:52

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.