英文:
How to display tab widget corner widget on the left/right side?
问题
我有一个QTabWidget
,其选项卡位于西侧,然后我尝试向其添加一个角部件,但未显示。如果我将选项卡位置设置为北侧或南侧,则会显示角部件,但不会显示在侧面。
以下是一个最小可重现示例(MRE):
QWidget *w = new QWidget();
QTabWidget *t = new QTabWidget(w);
w->setMinimumSize(800, 600);
t->addTab(new QWidget(), "Tab");
t->addTab(new QWidget(), "Tab");
t->addTab(new QWidget(), "Tab");
t->addTab(new QWidget(), "Tab");
t->setTabPosition(QTabWidget::TabPosition::East);
t->setGeometry(100, 100, 400, 400);
QPushButton *button = new QPushButton("Button");
// 用于调试目的
button->setObjectName("ss");
// 用于调试目的
t->setStyleSheet("background: red");
button->setStyleSheet("background: blue;");
// 用于调试目的
t->setCornerWidget(button, Qt::TopLeftCorner);
//t->setCornerWidget(button1, Qt::TopRightCorner);
//t->setCornerWidget(button1, Qt::BottomLeftCorner);
//t->setCornerWidget(button1, Qt::BottomRightCorner);
w->show();
// 用于调试目的
//qDebug() << button->geometry();
//button->setGeometry(0, 0, 50, 50);
// 用于调试目的
button->connect(button, &QPushButton::clicked, []() { qDebug() << "corner widget click?"; });
我尝试使用了所有4个角,右侧角没有效果,但左侧角会在选项卡之前留下空白,如下所示,空白区域位于右上角:
我尝试设置按钮作为角部件的样式表,以便在隐藏时可能会显示,但没有任何效果。
我检查了按钮的几何属性,它为QRect(0, 0 0x0)
,并且这是在使用show()
之后。因此,我尝试设置它的几何属性,但同样没有效果(但几何属性得到了正确的更新),按钮仍然没有显示。
我还尝试通过连接其clicked()
信号到qDebug()
来检查按钮的位置,我在整个小部件上都点击了一遍,但没有输出。
注意:角部件设计用于北侧和南侧的选项卡位置;已知其他方向可能无法正常工作。
从中我得到,这不完全不可能使其工作。
是否有方法在QTabWidget
的侧面获取角部件?
英文:
I have a QTabWidget
with its tabs on the West, then I tried to add a corner widget to it, but it did not appear. If I set the tabs position to North or South, the corner widget gets displayed, but not on the side.
Here's an MRE:
QWidget *w = new QWidget();
QTabWidget *t = new QTabWidget(w);
w->setMinimumSize(800,600);
t->addTab(new QWidget(),"Tab");
t->addTab(new QWidget(),"Tab");
t->addTab(new QWidget(),"Tab");
t->addTab(new QWidget(),"Tab");
t->setTabPosition(QTabWidget::TabPosition::East);
t->setGeometry(100,100,400,400);
QPushButton *button = new QPushButton("Button");
//for debugging purposes
button->setObjectName("ss");
//for debugging purposes
t->setStyleSheet("background: red");
button->setStyleSheet("background: blue;");
//for debugging purposes
t->setCornerWidget(button1,Qt::TopLeftCorner);
//t->setCornerWidget(button1,Qt::TopRightCorner);
//t->setCornerWidget(button1,Qt::BottomLeftCorner);
//t->setCornerWidget(button1,Qt::BottomRightCorner);
w->show();
//for debugging purposes
//qDebug()<<button->geometry();
//button->setGeometry(0,0,50,50);
//for debugging purposes
button->connect(button,&QPushButton::clicked,[](){qDebug()<<"corner widget click?";});
I tried to use all 4 corners, and right corners have no effect, but the left causes an empty space before the tabs, here's how it looks, the empty space is at the top right corner:
I tried to set the stylesheet of the button I'm using as a corner widget, so that it might appear if hidden, but resulted in nothing.
I checked the button's geometry, and it got QRect(0,0 0x0)
, and that is after using show()
. So i tried to set its geometry, but resulted in nothing as well (but the geometry got updated correctly), button still not displayed.
I also tried to check for the button whereabouts by connecting its clicked()
signal to a qDebug()
, I clicked all over the widget and got no output.
>Note: Corner widgets are designed for North and South tab positions; other orientations are known to not work properly.
I get from that, that it's not entirely impossible to get it to work.
Is there a way to get a QTabWidget
corner widget on the side?
答案1
得分: 0
从Qt文档(qt6)中,QTabWidget::setCornerWidget:说:
注意:角部件设计用于北和南选项卡位置;其他方向已知不能正常工作。
这意味着它们可以工作,但使用起来会凌乱且不方便。
以下是一种在侧边使用QTabWidget
角部件的方法:
正如问题中已经指出的,当选项卡位置为West
或East
时,设置一个角部件会导致在选项卡之前出现一个小间隙,但什么都不会显示在那里。
但是,如果设置QTabWidget
的角部件的最小大小,它将出现,这解决了一个问题,但引入了另一个问题,因为现在我需要自己计算那个大小,或者为我的角部件腾出空间。
以下是我用来尝试如何获得空白角尺寸的最小可重现示例:
QTabWidget *t = new QTabWidget();
// 我需要堆叠窗口小部件,以便可以使用它的几何信息来计算空白角的尺寸
QStackedWidget *stack_widget = t->findChild<QStackedWidget*>("qt_tabwidget_stackedwidget");
t->setMinimumSize(800, 600);
t->addTab(new QWidget(), "Tab1");
t->addTab(new QWidget(), "Tab2");
t->addTab(new QWidget(), "Tab3");
t->addTab(new QWidget(), "Tab4");
t->setTabPosition(QTabWidget::TabPosition::West);
QToolButton *button1 = new QToolButton();
button1->setIcon(QIcon(":/icons/collapse.png"));
t->setCornerWidget(button1, Qt::TopLeftCorner);
t->show();
// 宽度等于堆叠窗口小部件开始的位置(x坐标)
// 高度等于选项卡栏开始的位置(y坐标)
// 我从stackwidget的x坐标中减去了1,因为看起来更好
t->cornerWidget(Qt::TopLeftCorner)->setMinimumSize(stack_widget->geometry().x() - 1,
t->tabBar()->geometry().y());
// 检查相关小部件的几何信息
/*qDebug()<< "cornerWidget geo" << t->cornerWidget(Qt::TopLeftCorner)->geometry();
qDebug()<< "tabBar rect" << t->tabBar()->tabRect(0);
qDebug()<< "tabBar geo" << t->tabBar()->geometry();
qDebug()<< "stackwidget geo" << sw->geometry();*/
以下是它的外观,我使用了自定义图标:
如果您需要更多空间来放置角部件,您需要移动选项卡栏,因为角部件将覆盖它,您可以使用样式表来实现这一点。请参阅这个链接:Qt样式表示例:自定义QTabWidget和QTabBar。
以下是样式表的示例:
t->setStyleSheet("QTabWidget::tab-bar "
"{"
"top: 50px;" /* 向下偏移50px */
"}");
以下是只有这个添加和更改的MRE的外观:
建议: QTabWidget::paintEvent可能是一个更好的解决方案。
英文:
From the Qt documentation (qt6), QTabWidget::setCornerWidget: says:
>Note: Corner widgets are designed for North and South tab positions; other orientations are known to not work properly.
That means they do work, but are messy to and inconvenient to use.
Here's a way to use QTabWidget
corner widget on the side:
As already noted in the question, setting a corner widget while tabs position is West
or East
, causes a small gap before the tabs without anything appearing there.
But if you set QTabWidget
's corner widget minimum size, it will appear, which solves a problem but causes another, because now I need to calculate that size myself, or make room for my corner widget.
Here's an MRE that I used to try and figure how to get that empty corner size:
QTabWidget *t = new QTabWidget();
//I needed the stacked widget so I can use its geometry to calculate the empty corner size
QStackedWidget *stack_widget = t->findChild<QStackedWidget*>("qt_tabwidget_stackedwidget");
t->setMinimumSize(800,600);
t->addTab(new QWidget(),"Tab1");
t->addTab(new QWidget(),"Tab2");
t->addTab(new QWidget(),"Tab3");
t->addTab(new QWidget(),"Tab4");
t->setTabPosition(QTabWidget::TabPosition::West);
QToolButton *button1 = new QToolButton();
button1->setIcon(QIcon(":/icons/collapse.png"));
t->setCornerWidget(button1,Qt::TopLeftCorner);
t->show();
//width is equal to where the stack widget starts (x coordinate)
//height is equal to where the tab bar starts (y coordinate)
//I subtracted 1 from stackwidget's x because it simply looked better
t->cornerWidget(Qt::TopLeftCorner)->setMinimumSize(stack_widget->geometry().x()-1,
t->tabBar()->geometry().y());
//checking related widgets geometries
/*qDebug()<<"cornerWidget geo"<<t->cornerWidget(Qt::TopLeftCorner)->geometry();
qDebug()<<"tabBar rect"<<t->tabBar()->tabRect(0);
qDebug()<<"tabBar geo"<<t->tabBar()->geometry();
qDebug()<<"stackwidget geo"<<sw->geometry();*/
Here's how it looks, I used a custom icon:
If you need more space for the corner widget, you'll need to move the tab bar, because corner widget will cover it, you can do that using stylesheet. See this: Qt Style Sheets Examples: Customizing QTabWidget and QTabBar.
Here's an example of stylesheet:
t->setStyleSheet("QTabWidget::tab-bar "
"{"
"top: 50px;" /* push down by 50px */
"}");
Here's how it looks with that being the only addition and change to my MRE:
Suggestion: QTabWidget::paintEvent might be a better solution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论