简体   繁体   English

如何在PyQt5中自定义QGroupBox标题?

[英]How to customise QGroupBox title in PyQt5?

Here's a piece of code that creates a simple QGroupBox: 这是一段创建简单QGroupBox的代码:

from PyQt5.QtWidgets import (QApplication, QWidget,
                             QGroupBox, QGridLayout)

class QGroupBoxTest(QWidget):
  def __init__(self):
    super().__init__()
    self.initUI()

  def initUI(self):
    gb = QGroupBox()
    gb.setTitle('QGroupBox title')

    appLayout = QGridLayout()
    appLayout.addWidget(gb, 0, 0)
    self.setLayout(appLayout)

    self.setWindowTitle('QGroupBox test window')
    self.setGeometry(300, 300, 300, 200)

if __name__ == "__main__":

  import sys

  app = QApplication(sys.argv)
  test = QGroupBoxTest()
  test.show()

  sys.exit(app.exec_())

and here's what the output looks like to me: 这是输出对我来说的样子:

QGB-1.JPG

Now let's say I want to add some style to it and I do this by adding this line to the initUI method: 现在假设我想为它添加一些样式,我通过将此行添加到initUI方法来实现:

gb.setStyleSheet("border: 1px solid gray; border-radius: 5px")

here's the output: 这是输出:

QGB-1.JPG

As can be clearly seen from the pic, the title has ended up inside the frame and now is overlapping the frame border. 从图中可以清楚地看到,标题已经在框架内部结束,现在与框架边框重叠。

So I have three questions actually: 所以我实际上有三个问题:

  1. Why did the title move? 标题为什么会移动?
  2. How do I move it about and place it wherever I want (if possible)? 如何移动它并将其放置在我想要的任何地方(如果可能)?
  3. What if I simply want to round off border corners without specifying the border-style property. 如果我只想在不指定边框样式属性的情况下舍入边角,该怎么办? Let's say I want the border-style to stay the same as in the first pic but with rounded corners. 假设我希望边框样式与第一张照片保持一致,但带有圆角。 How do I do that? 我怎么做?

1) Probably that's the default QT placement, in the first image the platform style is used, and its take care of borders and title placement, when you change the stylesheet you override something and you get the ugly placement. 1)可能是默认的QT位置,在第一个图像中使用平台样式,并且它处理边框和标题放置,当您更改样式表时,您覆盖某些内容并且您获得了丑陋的位置。

2) You can control the "title" position using the QGroupBox:title controls, for example: 2)您可以使用QGroupBox:title控件控制“标题”位置,例如:

gb.setStyleSheet('QGroupBox:title {'
                 'subcontrol-origin: margin;'
                 'subcontrol-position: top center;'
                 'padding-left: 10px;'
                 'padding-right: 10px; }')

will result in something like this: 将导致这样的事情:

标题

3) My suggestion is to create different strings for the stylesheet attributes you want to change, then compose them to create the style you want. 3)我的建议是为要更改的样式表属性创建不同的字符串,然后组合它们以创建所需的样式。

Even though this question has already been answered, I will post what I've figured out regarding technics of applying style sheets to widgets in PyQt which partly answers my original question. 即使这个问题已经得到解答,我也会发布我已经想到的关于在PyQt中将样式表应用到小部件的技术,这部分回答了我原来的问题。 I hope someone will find it useful. 我希望有人会发现它很有用。

I think it's nice to keep the styles in separate css(qss) files: 我认为将样式保存在单独的css(qss)文件中是很好的:

/*css stylesheet file that contains all the style information*/

QGroupBox {
  border: 1px solid black;
  border-radius: 5px;
}

QGroupBox:title{
  subcontrol-origin: margin;
  subcontrol-position: top center;
  padding: 0 3px 0 3px;
}

and the python code looks like this: 并且python代码如下所示:

from PyQt5.QtWidgets import (QApplication, QWidget,
                             QGroupBox, QGridLayout)
from PyQt5.QtCore import QFile, QTextStream

class QGroupBoxTest(QWidget):
  def __init__(self):
    super().__init__()
    self.initUI()

  def initUI(self):
    gb = QGroupBox()
    gb.setTitle('QGroupBox title:')

    gb.setStyleSheet(self.getStyleSheet("./styles.qss"))

    appLayout = QGridLayout()
    appLayout.addWidget(gb, 0, 0)
    self.setLayout(appLayout)

    self.setWindowTitle('QGroupBox test window')
    self.setGeometry(300, 300, 300, 300)

  def getStyleSheet(self, path):
    f = QFile(path)
    f.open(QFile.ReadOnly | QFile.Text)
    stylesheet = QTextStream(f).readAll()
    f.close()
    return stylesheet

if __name__ == "__main__":

  import sys

  app = QApplication(sys.argv)
  test = QGroupBoxTest()
  test.show()

  sys.exit(app.exec_())

which yields the following output: 产生以下输出:

QGB-1.JPG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM