Warning: resurrected thread.
- - - - -
I just faced the same problem. I wanted to point out that instead of using QTimer::singleShot, you can simply call ensurePolished() right before querying any elements of the widget style that are set by the custom style sheet, like a custom font.
Like Lykurg guessed, it's exactly what happens the first time the widget is shown, it is polished.
Additionally, if you're interested in setting a 'minimum width' for a QLineEdit so that it always fits the placeholder text, you can use the following.
Subclass QLineEdit and declare a public function 'void setPlaceholderText( const QString& placeholder ).' This will shadow the original function from QLineEdit.
Then the code for the shadowing function is:
#include <QStyleOptionFrameV2>
void SubclassedQLineEdit::setPlaceholderText( const QString& placeholder )
{
if ( placeholder.isEmpty() )
return;
QLineEdit::setPlaceholderText( placeholder
);
// Original function.
// Set a minimum size if the widget has valid placeholder text, so
// the placeholder text isn't truncated at the end.
ensurePolished(); // Updates the widget from the default font to the style-sheet font.
initStyleOption( &frame );
const QSize contentsSize
( fontMetrics
().
width( text
), fontMetrics
().
height() );
const int width
= style
()->sizeFromContents
( QStyle::CT_LineEdit,
&frame, contentsSize,
this ).
width();
setMinimumWidth( width );
}
#include <QStyleOptionFrameV2>
void SubclassedQLineEdit::setPlaceholderText( const QString& placeholder )
{
if ( placeholder.isEmpty() )
return;
QLineEdit::setPlaceholderText( placeholder ); // Original function.
// Set a minimum size if the widget has valid placeholder text, so
// the placeholder text isn't truncated at the end.
ensurePolished(); // Updates the widget from the default font to the style-sheet font.
QStyleOptionFrameV2 frame;
initStyleOption( &frame );
const QSize contentsSize( fontMetrics().width( text ), fontMetrics().height() );
const int width = style()->sizeFromContents( QStyle::CT_LineEdit, &frame, contentsSize, this ).width();
setMinimumWidth( width );
}
To copy to clipboard, switch view to plain text mode
There's always some empty space left, but the line edit is guaranteed to display all the placeholder text without truncating it (as in, "placehold...").
Bookmarks