1 Attachment(s)
QLineEdit setAlignment doesn't set the Vertical alignment
This is my code below:
Code:
MainWindow::MainWindow(CoreDictionary const& dict)
{
ui.setupUi(this);
ui.searchField->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
}
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:
Attachment 13848
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...
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.
2 Attachment(s)
Re: QLineEdit setAlignment doesn't set the Vertical alignment
Quote:
Originally Posted by
d_stranz
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:
Code:
MainWindow::MainWindow(CoreDictionary const& dict)
{
ui.setupUi(this);
ui.searchField->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
ui.
searchField->setText
(QString("Type something here!"));
ui.searchField->setTextMargins(QMargins(0, 0, 0, 0));
ui.searchField->setStyleSheet("QLineEdit { padding: 0px 0px 0px 0px; }");
}
And this the result:
Attachment 13849
Is that really Qt::AlignVCenter working as intended?
I've also tried changing the setTextMargin line with this:
Code:
ui.searchField->setTextMargins(QMargins(0, 0, 0, 6));
And this is the result:
Attachment 13850
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
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.
Re: QLineEdit setAlignment doesn't set the Vertical alignment
Quote:
Originally Posted by
d_stranz
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.
Sure - to recap, the code now looks like this:
Code:
MainWindow::MainWindow(CoreDictionary const& dict)
{
ui.setupUi(this);
ui.searchField->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
ui.
searchField->setText
(QString("Type something here!"));
ui.searchField->setTextMargins(QMargins(0, 0, 0, 6));
ui.searchField->setStyleSheet("QLineEdit { padding: 0px 0px 0px 0px; border: 1px solid white;}");
}
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)
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?
Quote:
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.
2 Attachment(s)
Re: QLineEdit setAlignment doesn't set the Vertical alignment
Quote:
Originally Posted by
d_stranz
Your attachment link is not valid, so I can't see your screenshot.
should work now?
Attachment 13852
Quote:
Originally Posted by
d_stranz
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
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:
Attachment 13853
Re: QLineEdit setAlignment doesn't set the Vertical alignment
Quote:
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.
1 Attachment(s)
Re: QLineEdit setAlignment doesn't set the Vertical alignment
Quote:
Originally Posted by
d_stranz
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.
Attachment 13854
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".