qtqtabwidget

How to add a QLabel to the upper right corner of a QTabWidget?


I am creating a QTabWidget with some tabs inserted.

However, I have an additional requirement, which is to display a message on the upper right corner of the QTabWidget, as shown in the following snapshot:

Message on the top right corner

Is there any way to do this?


Solution

  • From Qt documentation of QTabWidget:

    void QTabWidget::setCornerWidget(QWidget *widget, Qt::Corner corner = Qt::TopRightCorner)

    Sets the given widget to be shown in the specified corner of the tab widget. The geometry of the widget is determined based on the widget's sizeHint() and the style().

    Only the horizontal element of the corner will be used.

    Passing nullptr shows no widget in the corner.

    Any previously set corner widget is hidden.

    All widgets set here will be deleted by the tab widget when it is destroyed unless you separately reparent the widget after setting some other corner widget (or nullptr).

    Note: Corner widgets are designed for North and South tab positions; other orientations are known to not work properly.

    See also cornerWidget() and setTabPosition().

    Here's an example that demonstrates that:

    QLabel *lbl = new QLabel("Some text here");
    ui->tabWidget->setCornerWidget(lbl);