Elijah Antonov
Elijah Antonov

Reputation: 21

How to dynamically change tabs sizes in Qt?

I'm a student working on my first Qt program and I need some help. I'm developing a musical instruments simulator. I have a mainwindow and 2 classes: DrumsWindow and KeysWindow simulating drums and keys and having different sizes (drums are 798x532, keys are 953x306). I have created a tabWidget in the main window and inserted my DrumsWindow and KeysWindow into it:

ui->tabWidget->insertTab(0, &dw, "Drums");
ui->tabWidget->insertTab(1, &kw, "Synth");

if (ui->tabWidget->currentIndex() == 0){
    this->resize(798, 532);
    ui->tabWidget->resize(798, 532);
}

if (ui->tabWidget->currentIndex() == 1){
    this->resize(953, 306);
    ui->tabWidget->resize(953, 306);
}

This code is from the MainWindow constructor. It works, there are two tabs in the main window showing drums and keys. However, those "if" statements make only the first opened tab of the proper size. When I click on the Synth tab, window size remains the same (while I need it to be changed). So, this is what I made to solve the problem. First, I created new slots in the MainWindow class:

void MainWindow::drumsTabClicked()
{
    ui->tabWidget->setCurrentIndex(0);
    this->resize(798, 532);
    ui->tabWidget->resize(798, 532);
}
void MainWindow::keysTabClicked()
{
    ui->tabWidget->setCurrentIndex(1);
    this->resize(953, 306);
    ui->tabWidget->resize(953, 306);
}

And then, I connected them to signals:

connect(ui->tabWidget, SIGNAL(tabBarClicked(0)), this, SLOT(drumsTabClicked()));
connect(ui->tabWidget, SIGNAL(tabBarClicked(1)), this, SLOT(keysTabClicked()));

But still, it doesn't work. Could you, please, explain how to resize the main window when the user clicks on a tab?

Upvotes: 0

Views: 567

Answers (2)

Elijah Antonov
Elijah Antonov

Reputation: 21

Thank you guys, you helped me a lot. Here's what I did.

mainwindow.h: added a slot

private slots:
    void onTabBarClicked(int);

mainwindow.cpp: added the realization of the slot

void MainWindow::onTabBarClicked(int index){
    if (index == 0){
        this->resize(798, 532);
        ui->tabWidget->resize(798, 532);
    }
    if (index == 1){
        this->resize(953, 306);
        ui->tabWidget->resize(953, 306);
    }
}

And connected the slot and the signal using Qt5 syntax:

connect(ui->tabWidget, &QTabWidget::tabBarClicked, this, &MainWindow::onTabBarClicked);

So, now it works!

Upvotes: 0

Farshid616
Farshid616

Reputation: 1484

You should connect it like this:

connect(ui->tabWidget, SIGNAL(tabBarClicked(int)), this, SLOT(onTabBarClicked(int)));

Then do your stuff in this slot and seperate them with if:

void MainWindow::tabBarClicked(int index)
{
    if(!index)
    {
        this->resize(798, 532);
        ui->tabWidget->resize(798, 532);
    }
    else
    {
        this->resize(953, 306);
        ui->tabWidget->resize(953, 306);
    }
}

Also you can create this connection automatically with right click on UI form tab bar and select go to slot tabBarClicked.

Upvotes: 1

Related Questions