【问题标题】:How to force a widget to resize together with its parent TabWidget - with code如何强制小部件与其父 TabWidget 一起调整大小 - 使用代码
【发布时间】:2020-08-04 09:21:44
【问题描述】:

我查看了每个线程,但没有一个有效。他们中的一些人在讨论如何从 Qt Designer 中进行操作(我已经知道该怎么做,但它没有用,因为选项卡是动态添加的)。 他们中的一些人讨论如何通过代码来做到这一点,但仍然没有任何效果。

重要的代码在 appendTab() 里面:

TabWidgetCSD::TabWidgetCSD(QWidget *parent)
    : QTabWidget(parent)
{
    TabBarCSD* bar = new TabBarCSD();
    setTabBar(bar);

    // create absolute button to the right. it will change the position everytime something happens
    // and it will  will take the last index
    QIcon icon(svgToColorIcon(":/images/plus.svg"));
    openTabBtn = new QPushButton(icon, "", this);
    openTabBtn->setFlat(true);
    openTabBtn->setFixedSize(QSize(29, 29));
    openTabBtn->setIconSize(QSize(25, 25));
    connect(openTabBtn, &QPushButton::clicked, this, [=, this](){
        appendTab();
    });
    appendTab();
}


void TabWidgetCSD::appendTab()
{
    // create page and get it's index
    auto l = new QVBoxLayout();
    auto mainMenuForm = new MainMenuForm;
    l->addWidget(mainMenuForm);
    setLayout(l);
    auto tabIdx = addTab(mainMenuForm, "");


    QIcon icon(svgToColorIcon(":/images/times.svg"));
    QLabel* label = new QLabel;
    label->setText("Main Window");
    label->setAlignment(Qt::AlignCenter);
    label->setFixedWidth(175);
    auto btn = new QPushButton(icon, "");
    btn->setFlat(true);
    btn->connect(btn, &QPushButton::clicked, this, [=, this]() {
        removeTab(tabIdx);
    });
    auto hbox = new QHBoxLayout();
    hbox->setAlignment(Qt::AlignCenter);
    hbox->addWidget(label);
    hbox->addWidget(btn);
    auto wrapper = new QWidget();
    wrapper->setLayout(hbox);
    wrapper->setFixedSize(220, 30);

    tabBar()->setTabButton(tabIdx, QTabBar::LeftSide, wrapper);
}

【问题讨论】:

    标签: c++ qt qwidget qtabwidget


    【解决方案1】:

    你的问题在这里:

        auto l = new QVBoxLayout();
        auto mainMenuForm = new MainMenuForm;
        l->addWidget(mainMenuForm);
        setLayout(l);
        auto tabIdx = addTab(mainMenuForm, "");
    

    我不知道你是否想在mainMenuForm 上设置布局l,但我相当肯定你不想在调用TabWidgetCSD 时设置它(我假设是@ 987654325@),对于每个添加的选项卡。 可能你想要的只是

        auto tabIdx = addTab(new MainMenuForm, "");
    

    (如果您确实需要在 MainMenuForm 上添加布局,您将在 MainMenuForm c'tor 中添加它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      相关资源
      最近更新 更多