c++ - 如何在QTabWidget标题中设置QWidget?

标签 c++ qt qt5 qt4.8 qtabwidget

我想在QTabWidget的标题中插入QLabel和QLineEdit。我已经阅读了Qt的文档,但没有找到任何可以在QTabWidget的Header中设置任何Qwidget的函数。

我该怎么做?或者我必须重写 QTabWidget Painter 函数吗?

有什么建议吗?

最佳答案

您必须使用setTabButton功能:

void QTabBar::setTabButton(int index, ButtonPosition position, QWidget * widget)

Sets widget on the tab index. The widget is placed on the left or right hand side depending upon the position.

Any previously set widget in position is hidden.

The tab bar will take ownership of the widget and so all widgets set here will be deleted by the tab bar when it is destroyed unless you separately reparent the widget after setting some other widget (or 0).

This function was introduced in Qt 4.5.

这与 QTabWidget 无关,但与 QTabBar 相关。

要获取 QtabBar,您必须使用以下函数:

QTabBar * QTabWidget::tabBar() const

Returns the current QTabBar.

示例:

#include <QApplication>

#include <QLabel>
#include <QTabBar>
#include <QTabWidget>
#include <QLineEdit>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QTabWidget w;
    w.addTab(new QLabel("widget 1"), "1");
    w.addTab(new QLabel("widget 2"), "2");

    QTabBar *tabBar = w.tabBar();

    tabBar->setTabButton(0, QTabBar::LeftSide, new QLineEdit("LineEdit0"));
    tabBar->setTabButton(0, QTabBar::RightSide, new QLabel("label0"));

    tabBar->setTabButton(1, QTabBar::LeftSide, new QLineEdit("LineEdit1"));
    tabBar->setTabButton(1, QTabBar::RightSide, new QLabel("label1"));
    w.show();

    return a.exec();
}

输出:

enter image description here

关于c++ - 如何在QTabWidget标题中设置QWidget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042508/

相关文章:

c++ - 找不到 Swift 编译器错误 'iostream'

c++ - Qt : C++ dynamic parameter GUI for plugin parameters

c++ - 是否可以从 C++ 创建 Qml 组件?

C++11 函数返回时会发生什么

c++ - 将逻辑或与 cout 运算符结合使用

c++ - 将 QuaZip 添加到项目中

c++ - 从内联函数调用中调用内联函数?

c++ - 添加文本停止渲染自定义 GL 项目

qt - QComboBox 的初始项目是否可以显示比项目列表中更短的文本项目?

c++ - ffmpeg 将视频解码为 YUV 和损坏的像素