c++ - Align to the right text from QLabel and QLineEdit -
i have qlabel
below qlineedit
same size , alignment properties:
qlineedit *lineedit = new qlineedit("999"); lineedit->setfixedwidth(100); lineedit->setalignment(qt::alignright); // qlabel *label = new qlabel("999"); label->setfixedwidth(100); label->setalignment(qt::alignright); // qlayout *layout = new qvboxlayout; layout->addwidget(lineedit); layout->addwidget(label);
here how rendered:
how can have text of bottom label
right-aligned text of lineedit
?
full award if find solution works on platforms, , works when font sizes different in lineedit
, label
.
unfortunately may not possible, @ least not out of box, right margin not work, 0 when text offset left. reason this offset not determined margins, depends on combination of platform gui style , particular font's metrics that's being used, , value "conveniently" not available in class public interface, there no way it.
you can font metrics easily, can't qstyleoptionframe
method required protected, accessing require subclass qlineedit
. however, if lucky, value zero, go simple this:
qvboxlayout *layout = new qvboxlayout; qlineedit *lineedit = new qlineedit("999"); lineedit->setalignment(qt::alignright); qlabel *label = new qlabel("999"); label->setalignment(qt::alignright); int offsetvalue = lineedit->fontmetrics().averagecharwidth(); label->setindent(offsetvalue); setlayout(layout); layout->addwidget(lineedit); layout->addwidget(label);
if doesn't work correctly you, have no other choice subclass qlineedit
, examine paint event, determine offset being calculated, , store value in public member can accessed outside used offset label.
i got lucky code:
Comments
Post a Comment