PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] How to dynamically change tabs sizes in Qt?

 September 12, 2022     c++, cross-platform, qt, user-interface, windows     No comments   

Issue

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?


Solution

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!



Answered By - Elijah Antonov
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing