【问题标题】:Issue change tab QTabWidget & QScrollArea Qt问题更改选项卡 QTabWidget & QScrollArea Qt
【发布时间】:2020-08-25 09:26:11
【问题描述】:

我正在尝试在 QTabWidget 中创建 QScrollArea。

版本:

  • Qt 5.15.0
  • Qt 创建者 4.12.4
  • MSVC2019 64 位

首先,我创建了 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");

我可以添加

tabWidget->setEnable(true);

对于所有标签,0

tabWidget->setTabEnabled(i, true);

点击更改标签不起作用:https://i.stack.imgur.com/8r1Jg.png

奇怪的事情:颜色看起来像已启用,但我只能使用 ← → 更改选项卡,当我通过单击 tabWidget 之外的其他东西失去 tabWidget 焦点时,我无法重新获得焦点。

所以我创建了临时按钮来更改选项卡并像这样链接到 tabWidget:

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

效果很好。

因此,我开始创建 QScrollArea : 首先,它不起作用,所以我试图在互联网上找到某事: QScrollArea not working as expected with QWidget and QVBoxLayout

我的结果:https://i.stack.imgur.com/jvVol.png

我无法单击单个按钮,也无法滚动... 如果我尝试像这样强制滚动,它不会滚动

scrollArea->scroll(0, 50);

最后一点,没有无限循环或死锁,因为这个被诅咒的 tabWidget 和滚动区域周围的所有东西都可以正常工作。

我不知道如果somedoby有这种实验,为什么这些对象“不回答”你能帮我吗?

非常感谢您。

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    试试这个代码

    #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()
    {
    }
    
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多