Add widgets into a QTabWidget
Asked Answered
M

2

9

Can I add some widgets like QLabel and QPushButton into a QTabWidget?

Actually, I want to do something like this: enter image description here

I'm using C++ and Qt.

Thanks

Mcreynolds answered 6/6, 2016 at 3:33 Comment(0)
C
11

It's possible, just use QTabWidget::setCornerWidget.

Quick example:

        QWidget* pTabCornerWidget = new QWidget(this);

        QLabel* pLabelTime = new QLabel(pTabCornerWidget);
        pLabelTime->setText("10:22:20");

        QPushButton* pButton = new QPushButton(pTabCornerWidget);
        pButton->setText("?");
        pButton->setMaximumSize(QSize(25, 25));

        QHBoxLayout* pHLayout = new QHBoxLayout(pTabCornerWidget);
        pHLayout->addWidget(pLabelTime);
        pHLayout->addWidget(pButton);

        mUI.tabWidget->setCornerWidget(pTabCornerWidget, Qt::TopRightCorner);
Cheater answered 1/2, 2017 at 9:0 Comment(0)
A
10

If you want do this stuff, I recommend you use QTabBar instead of QTabWidget. For example, your code can be (remember, that it's just a very simple example):

// Here some first widget
QWidget *wid1 = new QWidget(this);
QHBoxLayout *wid1Lay = new QHBoxLayout(wid1);
wid1Lay->addWidget(new QLabel(tr("Widget1")));

// Here some second widget
QWidget *wid2 = new QWidget(this);
QHBoxLayout *wid2Lay = new QHBoxLayout(wid2);
wid2Lay->addWidget(new QLabel(tr("Widget2")));

// Here some third widget
QWidget *wid3 = new QWidget(this);
QHBoxLayout *wid3Lay = new QHBoxLayout(wid3);
wid3Lay->addWidget(new QLabel(tr("Widget3")));

// Here your Tab bar with only bars
QTabBar *bar = new QTabBar(this);
bar->addTab("One");
bar->addTab("Two");
bar->addTab("Three");

// Here some label (for example, current time) and button
QLabel *lab = new QLabel(tr("Some text"), this);
QPushButton *but = new QPushButton(tr("Push"), this);

// Main layouts
QVBoxLayout *vLay = new QVBoxLayout(ui->centralWidget);
QHBoxLayout *hLay = new QHBoxLayout();

vLay->addLayout(hLay);
hLay->addWidget(bar);
// Spacer for expanding left and right sides
hLay->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
hLay->addWidget(lab);
hLay->addWidget(but);

vLay->addWidget(wid1);
vLay->addWidget(wid2);
vLay->addWidget(wid3);

// Some simple connect with lambda for navigation
connect(bar, &QTabBar::currentChanged, [=] (int index) {

    wid1->setVisible(false);
    wid2->setVisible(false);
    wid3->setVisible(false);

    switch(index) {
    case 0: wid1->setVisible(true);
        break;
    case 1: wid2->setVisible(true);
        break;
    case 2: wid3->setVisible(true);
        break;
    default:{}
    }

});

emit bar->currentChanged(0);
Adage answered 6/6, 2016 at 6:10 Comment(2)
Actually, I don't think I can exchange it for a QTabBar, because my QTabWidget is already implemented (with a lot of features), but thanks for your answer, it was very helpful.Mcreynolds
@someoneinthebox, you can use a QStackedWidget for showing one widget a time instead of hiding and showing widgets yourself. you can just connect to its setCurrentIndex() slot directly.Exteroceptor

© 2022 - 2024 — McMap. All rights reserved.