1
votes

I'm trying to create a QScrollArea in a QTabWidget.

Versions :

  • Qt 5.15.0
  • Qt creator 4.12.4
  • MSVC2019 64 bits

First of all, I've created the QTabWidget :

tabWidget = new QTabWidget(this);
tabWidget->setGeometry(10, 15, 1200, 665);
tabWidget->setStyleSheet("font-size : 15px");

tab1Content = new QWidget(tabWidget); tabWidget->addTab(tab1Content, "tab1");
tab2Content = new QWidget(tabWidget); tabWidget->addTab(tab2Content, "tab2");
tab3Content = new QWidget(tabWidget); tabWidget->addTab(tab3Content, "tab3");
tab4Content = new QWidget(tabWidget); tabWidget->addTab(tab4Content, "tab4");

I can add

tabWidget->setEnable(true);

And for all tabs, 0 <= i < tabWidget.count

tabWidget->setTabEnabled(i, true);

Click to change tab doesn't work : https://i.stack.imgur.com/8r1Jg.png

Strange thing : color looks like enabled but i can only change tabs with ← → and when i lost tabWidget focus by clicking on an other thing outside the tabWidget, i can't regain focus.

So i've created temporary button to change tabs and linked to tabWidget like that :

connect(changeTab, &QPushButton::clicked, [&]() {onChangeTab();});
void MainWindow::onChangeTab() {
    tabWidget->setCurrentIndex(tabWidget->currentIndex() >= tabWidget->count() - 1 ? 0 : tabWidget->currentIndex() + 1);
}

It works well.

Thus, I've start to create the QScrollArea : First, it doesn't work, so I've tried to found sth on internet : QScrollArea not working as expected with QWidget and QVBoxLayout

My result : https://i.stack.imgur.com/jvVol.png

I cannot click on a single button and i can't scroll... And if i try to force scroll like this, it doesn't scroll

scrollArea->scroll(0, 50);

Last thing, there isn't infinite loop or dead lock things because all things around this cursed tabWidget and scroll Area work perfectly.

I don't know why these objects "don't answer" if somedoby had this kind of experiment could you help me please ?

Thank you very much in advance.

1

1 Answers

0
votes

try this code

#include "widget.h"
#include<QTabWidget>
#include<QLabel>
#include<QVBoxLayout>
#include<QScrollArea>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QTabWidget *tabWidget = new QTabWidget(this);
    tabWidget->setGeometry(10, 15, 1200, 665);
    tabWidget->setStyleSheet("font-size : 15px");
    QWidget * tab1Content = new QWidget;

    //preparing tab1content ( e.g.)
     QVBoxLayout * verticalLayout = new QVBoxLayout;
         // adding items to vertical layout
            for(int i=0;i<100;i++)
                 verticalLayout->addWidget(new QLabel(QString::number(i)));

    // set this vertical layout inside tab1content
       tab1Content->setLayout(verticalLayout);

    // create new scroll area  ...
     QScrollArea * scroll = new QScrollArea;
          // ... and add tab1content in scroll area
          scroll->setWidget(tab1Content);

  // and finally add scroll area inside tabwidget
    tabWidget->addTab(scroll,"tab1");



     QWidget *  tab2Content = new QWidget; tabWidget->addTab(tab2Content, "tab2");
     QWidget *  tab3Content = new QWidget; tabWidget->addTab(tab3Content, "tab3");
     QWidget *  tab4Content = new QWidget; tabWidget->addTab(tab4Content, "tab4");

}

Widget::~Widget()
{
}