Emanuele
Emanuele

Reputation: 2384

How to add a QToolBar inside a QTabWidget

I have been trying to add a QToolBar inside a QTabWidget in order to achieve something like the image below, so that everytime I add a new QTabWidget I have also a related QToolBar inside it.

tab

Everything seems to work fine, I create a QAction to link it to the QTabWidget and according to this post it seems to be possible to do that but the problem is that when I compile nothing shows up as shows below:

tab2

Below is what I have done so far:

mainwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void onChangeTab(int index);
    void newTab();
    void closeTab(const int &index);
private slots:
    void on_addTabBtn_clicked();
    void on_tabWidget_tabCloseRequested(int index);
private:
    Ui::MainWindow *ui;
    QAction *addTab1;
    QToolBar *mToolBar1;
    QAction *addIconToolBar1;
};

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tabWidget->clear();
    ui->tabWidget->setTabsClosable(true);
    ui->tabWidget->addTab(new QLabel("Add"), QString("Add"));
    ui->toolBar->setContextMenuPolicy(Qt::ActionsContextMenu);

    mToolBar1 = new QToolBar;
    addIconToolBar1 = new QAction;
    addIconToolBar1->setIcon(QIcon("qrc:/cardio.png"));
    ui->toolBar->addWidget(mToolBar1);
    ui->toolBar->addAction(addIconToolBar1);

    connect(ui->addTabBtn, &QPushButton::clicked, this, [&] { ui->tabWidget->addTab(new QLabel("Add"), QString("Add")); });
    connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_addTabBtn_clicked()
{
    int index = 0;
    if(index == this->ui->tabWidget->count() - 1) {
        newTab();
    }
}

void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    ui->tabWidget->removeTab(index);
}

I tried to solve the problem in many ways and researched what the cause might be. I came across several references such as this one, which is the most important I found as the user seems to be doing it but there is no reference to documentation or no example to code to understand/study through.

Thanks for pointing to the right direction to solve this issue.

Upvotes: 0

Views: 1364

Answers (2)

Robert.K
Robert.K

Reputation: 126

I don't see where you are trying to add Toolbar to your TabWidget... You must define Layout, add your toolbar to that layout and finally set layout to your tabWidget.

Try to do something like this, in your mainwindow constructor.

QHBoxLayout* tabWidgetLayout = new QHBoxLayout;
tabWidgetLayout->addWidget( your toolbar);
tabwidget->setLayout(tabWidgetLayout);

Also don't forget to include QHBoxLayout's header.

Even if other answers may seem to work, this is actually the right way to do what you asked for.

Upvotes: 1

Skykey
Skykey

Reputation: 46

You can simply do something like this, and it really works.

QToolBar *toolbar=new QToolBar("toolbar",ui->tab);
toolbar->addAction("action1");
toolbar->addAction("action2");

enter image description here

Upvotes: 3

Related Questions