【问题标题】:Need help when autohiding a tabBar in Qt with C++使用 C++ 在 Qt 中自动隐藏 tabBar 时需要帮助
【发布时间】:2022-01-11 21:16:44
【问题描述】:

我是 C++ 中 Qt 编程的新手。我使用 Visual Studio 2019 作为编辑器。我制作了两个按钮(b1 和 b2)和一个选项卡小部件。它们在 UI 中正确可见,如下图所示。

我想要什么?我想自动隐藏标签区域。

我做了什么?这是包含小部件(选项卡和按钮)的代码。

main.cpp

#include "tabbar.h"
#include <QtWidgets/QApplication>

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    TabBar w;
    w.show();
    return a.exec();
}

tabbar.h

#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_tabbar.h"
#include<qtabbar.h>
#include<qpushbutton.h>
#include<qgridlayout.h>
#include<qdebug.h>

class TabBar : public QMainWindow
{
    Q_OBJECT

public:
    TabBar();
};

tabbar.cpp

#include "tabbar.h"
TabBar::TabBar()
{
    QPushButton* b1 = new QPushButton("b1");
    QPushButton* b2 = new QPushButton("b2");
    QTabBar* tabBar = new QTabBar;
    tabBar->addTab("Tab 1");
    tabBar->addTab("Tab 2");
    tabBar->setTabText(0, "Hello tab 1");
    tabBar->setTabText(1, "Hello tab 2");
    QString str;
    str = tabBar->tabText(1);
    qDebug() << str;
    tabBar->setTabToolTip(0, "ToolTip for tab 1");
    tabBar->setTabToolTip(1, "ToolTip for tab 2");

    tabBar->autoHide();

    QGridLayout* layout = new QGridLayout;

    layout->addWidget(b1, 0, 0);
    layout->addWidget(b2, 0, 1);
    layout->addWidget(tabBar, 0, 2);

    QWidget* wid = new QWidget;
    wid->setLayout(layout);
    setCentralWidget(wid);
}

主要问题:即使我通过tabBar() 函数调用函数autoHide(),选项卡也没有隐藏。我错过了什么吗?

【问题讨论】:

  • 来自documentation"If true, the tab bar is automatically hidden when it contains less than 2 tabs"。您的QTabBar 有两个选项卡,因此不会被隐藏。正确的?还是我误会了?
  • @G.M.并且OP正在调用getter ...设置自动隐藏属性,她应该调用“setAutoHide(true)”...
  • @ΦXocę웃Пepeúpaツ 我的错——我看错了代码。谢谢指正。

标签: c++ qt


【解决方案1】:

您使用的方法实际上是属性的“getter”,您需要调用setter setAutoHide(bool hide),另外您应该阅读文档:

自动隐藏:布尔 如果为 true,当标签栏包含少于 2 个标签时,它会自动隐藏。

所以有 2 个标签不会发生任何事情

【讨论】:

  • 实际上,我想要一个动作,当鼠标光标到达 qt 窗口的右边缘时,它应该取消隐藏 tabBar。那么如何“隐藏”和“取消隐藏”tabBar?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多