Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTabWidget size depending on current Tab



  • I have the same problem with the https://stackoverflow.com/questions/29128936/qtabwidget-size-depending-on-current-tab, so I tried Nejat's answer,

    PreferenceWidget::PreferenceWidget(QWidget* parent) : QWidget(parent)
    {
        setWindowTitle(tr("Preference"));
        setFixedWidth(600);
    
        m_tabWidget = new QTabWidget(this);
        connect(m_tabWidget, &QTabWidget::currentChanged, this, &PreferenceWidget::updateSizes);
        m_tabWidget->addTab(createGeneralWidget(), tr("General"));
        m_tabWidget->addTab(createBehaviorWidget(), tr("Behavior"));
    
        QHBoxLayout* hLayout = new QHBoxLayout(this);
        hLayout->addWidget(m_tabWidget);
    }
    
    void PreferenceWidget::updateSizes(int index)
    {
        for (int i = 0; i < m_tabWidget->count(); ++i)
        {
            if (i != index)
            {
                m_tabWidget->widget(i)->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
            }
        }
    
        m_tabWidget->widget(index)->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        m_tabWidget->widget(index)->resize(m_tabWidget->widget(index)->minimumSizeHint());
        m_tabWidget->widget(index)->adjustSize();
        resize(minimumSizeHint());
        adjustSize();
    }
    

    But it didn't work. What's wrong?


  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?
    On what OS ?
    Can you provide the full class so people can test it ?


Log in to reply