Solved Display icon&text on QLabel
-
Hi @opengpu
One workaround would be using 2 QLable (1 for image & 1 for text) in Layout and use it.
Need to be tested -
@Pradeep-P-N @opengpu Agree, use a widget with layout and put these two labels in this widget.
-
There are at least 2 more options as well.
- Subclassing and overriding paintEvent to draw an image and a text
- setting a stylesheet with the target image as background, than text should still be applicable in the normal way
-
thank you.
if i want to set a color-border to the container-Widget, should i use stylesheet? -
@Pradeep-P-N worked. but the dockButton and closeButton disapear....i want the buttons
-
@opengpu
Yes you can try setting the StyleSheet for the styles/design needed.All the best.
-
Did you try setting the Qt::WindowFlags ?
And is it possible to share the sample code ? So i can check it.All the best.
-
@Pradeep-P-N
setTitleBarWidget with a QWidget which include a HLayout of two QLabel. -
Hi @opengpu
Can you please once check the QDockWidget::setTitleBarWidget(QWidget *widget) documentation there some tips for implementing custom title bars.- If a title bar widget is set, QDockWidget will not use native window decorations when it is floated.
-
Or you can use your own QProxyStyle to make it:
class DrawIconDockWidgetStyle: public QProxyStyle
{
Q_OBJECTpublic:
DrawIconDockWidgetStyle(const QIcon& icon,
QStyle* style = nullptr)
: QProxyStyle(style)
, mIcon(icon){}
virtual ~DrawIconDockWidgetStyle(){}virtual void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = nullptr) const { if(QStyle::CE_DockWidgetTitle == element) { int IconWidth = pixelMetric(QStyle::PM_ToolBarIconSize)-3; int Margin = baseStyle()->pixelMetric(QStyle::PM_DockWidgetTitleMargin); QPoint IconPoint(Margin + option->rect.left(), Margin + option->rect.center().y() - IconWidth/2); painter->drawPixmap(IconPoint, mIcon.pixmap(IconWidth, IconWidth)); const_cast<QStyleOption*>(option)->rect = option->rect.adjusted(IconWidth, 0, 0, 0); } QProxyStyle::drawControl(element, option, painter, widget); }
private:
QIcon mIcon;
};
in .cpp:
ui->dockWidget_3->setStyle(new DrawIconDockWidgetStyle( QIcon(":/1_toolbar_normal.png"), ui->dockWidget_3->style()));
ui->dockWidget_3->setWindowTitle("hey~~");