QTabBar 下的小部件带有不需要的框架

发布于 2024-11-11 14:18:35 字数 1757 浏览 1 评论 0原文

我的 QTabBar/QTabWidget 有问题。这就是我的程序目前的样子,使用 QTabBar:

在此处输入图像描述

如您所见,有一个QTabBar 和它下面的 QScrollArea 之间的线难看。这条线是 QScrollArea 框架的一部分,我不能简单地将其删除,因为其他三边都需要它。我意识到我可以使用 QTabWidget,但随后我必须为每个选项卡创建一个小部件,这在这里不可行:QScrollArea 的内容根据所选选项卡而变化,但只有一个 QScrollArea 小部件。 (每次创建新选项卡时复制它都会导致自己的问题。)

那么有人知道一种方法吗:
(i) 告诉 QScrollArea 绘制一个没有顶线的框架;或
(ii) 对 QTabWidget 中的每个选项卡使用相同的小部件?

更新 3 对于另一种方法,请参阅下面我的回答。

更新1我已经实施了zvezdi的建议,难看的线条消失了:

在此处输入图像描述

这是一个改进。但这是不对的。查看滚动条和边框之间的间隙。右边是两个像素,而不是一个;在底部,它是三个像素。 QScrollArea 边框和 mainWidget 边框之间的右侧间隙太大了 1 个像素。这是由于 QTabWidget 的边框样式造成的,我在尝试更改它时失去了理智。如果我说:

MyTabWidget -> setStyleSheet ("QTabWidget::pane { margin: 0px,0px,0px,0px }") ;

那么边距似乎是正确的,但边框消失了:

在此处输入图像描述

如果我说:

MyTabWidget -> setStyleSheet ("QTabWidget::pane { "
  " margin: 0px,0px,0px,0px;"
  " border: 1px solid darkgray;"
  "}") ;

那么我几乎回到了开始的地方:

在此处输入图像描述

如果我尝试用以下方法解决此问题:

ApplicationTabWidget -> setStyleSheet ("QTabWidget::pane { "
"   margin: 0px,0px,0px,0px;"
" border: 1px solid darkgray;"
" border-top: 0px;"
      "}") ;

那么我又是嘲笑我的痛苦:

在此处输入图像描述

更新 2 如果我忘记 setStyleSheet 并且只是打开 documentMode ,这就是我得到的:

在此处输入图像描述

请有人告诉我我很愚蠢,而且对于这一切,有一个非常简单的解决方案。

I have a problem with QTabBar/QTabWidget. This is what my program looks like at the moment, using QTabBar:

enter image description here

As you can see, there is an unsightly line between the QTabBar and the QScrollArea underneath it. This line is part of the frame of the QScrollArea, which I can't simply get rid of, because it is required on the other three sides. I realise I could use QTabWidget, but then I would have to create a widget for each tab, which is not feasible here: the contents of the QScrollArea change according to the selected tab, but there is only one QScrollArea widget. (Duplicating it each time a new tab is created would cause its own problems.)

So does anybody know a way to either:
(i) tell the QScrollArea to draw a frame without the top line; or
(ii) use the same widget for each tab in a QTabWidget?

Update 3 For another approach, see my answer below.

Update 1 I have implemented zvezdi's suggestion, and the unsightly line has disappeared:

enter image description here

This is an improvement. But it's not right. Look at the gaps between the scroll bars and the border. On the right, it's two pixels instead of one; on the bottom, it's three pixels. And the gap on the right between the QScrollArea border and the mainWidget border is one pixel too big. This is due to QTabWidget's border style, which I am losing my sanity trying to change. If I say:

MyTabWidget -> setStyleSheet ("QTabWidget::pane { margin: 0px,0px,0px,0px }") ;

then the margins seem to be right, but the borders disappear:

enter image description here

If I say:

MyTabWidget -> setStyleSheet ("QTabWidget::pane { "
  " margin: 0px,0px,0px,0px;"
  " border: 1px solid darkgray;"
  "}") ;

then I'm almost back to where I started:

enter image description here

If I try to remedy this with:

ApplicationTabWidget -> setStyleSheet ("QTabWidget::pane { "
"   margin: 0px,0px,0px,0px;"
" border: 1px solid darkgray;"
" border-top: 0px;"
      "}") ;

then again I am mocked for my pains:

enter image description here

Update 2 If I forget setStyleSheet and just turn documentMode on, this is what I get:

enter image description here

Please somebody, tell me I'm being stupid, and there's a perfectly simple solution to all this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热鲨 2024-11-18 14:18:35

您说“QScrollArea 的内容根据选定的选项卡而变化”,如果我认为这不是真的,那么您的意思是滚动区域内的小部件的内容发生变化,那么您可以尝试以下操作:

在 QTabWidget 中创建与您需要的选项卡一样多的 QScrollArea 对象,但只有一个,例如 QTextEdit,您将在每个滚动区域中显示它,并且哪些内容将在选项卡更改时更改(从旧选项卡的 QScrollArea 和 takeWidget() 中获取)。 setWidget() 在新选项卡的 QScrollArea 上)

我不知道您是如何设计代码的,但是要尝试我的建议,您的代码应该围绕 QScrollArea 内的小部件设计,而不是 QScrollArea 本身。

You said "the contents of the QScrollArea change according to the selected tab" well if I assume that this is not true, and what you mean is that the content of the widget that is inside the scroll area changes, then you can try this:

Make as many QScrollArea objects as many tabs you need in your QTabWidget, but only one, for example QTextEdit, which you will show in every scroll area, and which content will change on tab change (takeWidget() from the old tab's QScrollArea & setWidget() on the new tab's QScrollArea)

I don't know how you've designed your code, but to try my suggestion your code should be designed around the widget inside QScrollArea, rather than the QScrollArea itself.

蓝戈者 2024-11-18 14:18:35

除非我误解,如果您通过将 frameShape 设置为 NoFrame 来关闭 QScrollArea 的边框,选项卡小部件仍然具有其框架线侧面和底部都在您想要的位置。

Unless I misunderstand, if you turn off the QScrollArea's border by setting the frameShape to NoFrame, the tab widget still has its frame lines on the sides and the bottom where you want them.

沉睡月亮 2024-11-18 14:18:35

我尝试了另一种方法:使用 QTabBar,如第一个屏幕截图所示,然后更改 MyScrollArea 的样式(显然,回想起来):

MyScrollArea -> setStyleSheet ("QScrollArea {"
  "border: 1px solid #898C95;"
  "border-top: 0px;"
  " }") ;

这是结果:

在此处输入图像描述

几乎正确!存在三个问题:
- 两个滚动条交叉处的小方块缺少底部边框(但如果我单击滚动条,或调整窗口大小,或者主窗口失去焦点,它就会被绘制);
- 颜色#898C95是硬编码的,所以如果用户更改样式,颜色将不正确。但如果我省略 border 样式,那么整个边框都会被漆成白色。有没有办法查询样式当前的边框颜色?
- 最严重的是,左侧断点小部件的背景颜色不再是白色。

但我想我已经在这上面浪费了足够多的时间了!我将坚持使用这个解决方案,除非有人可以建议尝试其他方法。

I have tried another approach: Use QTabBar, as in the first screenshot, and then change the style for MyScrollArea (obvious, in retrospect):

MyScrollArea -> setStyleSheet ("QScrollArea {"
  "border: 1px solid #898C95;"
  "border-top: 0px;"
  " }") ;

This is the result:

enter image description here

Almost right! There are three problems:
- the little square at the intersection of the two scroll bars is missing its bottom border (but it gets drawn if I click on the scroll bars, or resize the window, or the main window loses focus);
- the colur #898C95 is hard-coded, so it won't be right if the user changes the style. But if I leave out the border style, then the whole border is painted white. Is there a way to query the current border colour of a style?
- most seriously, the background colour of the breakpoint widget on the left-hand side is not white any more.

But I think I've wasted enough time on this! I will stick with this solution unless anybody can suggest something else to try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文