Display icon&text on QLabel
-
@opengpu I will assume that your question is whether QLabel can have text and pixmap at the same time as you actually did not ask any question.
The documentation answers your question and you can easily check this with a small test app. Short answer: no, QLabel cannot show both at the same time. -
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
-
@Pradeep-P-N worked. but the dockButton and closeButton disapear....i want the buttons
-
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~~");