我想在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();
}
输出:
关于c++ - 如何在QTabWidget标题中设置QWidget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042508/