• <i id='4R570'><tr id='4R570'><dt id='4R570'><q id='4R570'><span id='4R570'><b id='4R570'><form id='4R570'><ins id='4R570'></ins><ul id='4R570'></ul><sub id='4R570'></sub></form><legend id='4R570'></legend><bdo id='4R570'><pre id='4R570'><center id='4R570'></center></pre></bdo></b><th id='4R570'></th></span></q></dt></tr></i><div id='4R570'><tfoot id='4R570'></tfoot><dl id='4R570'><fieldset id='4R570'></fieldset></dl></div>
    <legend id='4R570'><style id='4R570'><dir id='4R570'><q id='4R570'></q></dir></style></legend>

      <small id='4R570'></small><noframes id='4R570'>

      • <bdo id='4R570'></bdo><ul id='4R570'></ul>
        <tfoot id='4R570'></tfoot>

      1. PyQt5:样式表和来自 QWidget 的继承

        时间:2023-08-04
      2. <legend id='8Kswn'><style id='8Kswn'><dir id='8Kswn'><q id='8Kswn'></q></dir></style></legend>
          <bdo id='8Kswn'></bdo><ul id='8Kswn'></ul>
          • <tfoot id='8Kswn'></tfoot>

              <tbody id='8Kswn'></tbody>

            <small id='8Kswn'></small><noframes id='8Kswn'>

            <i id='8Kswn'><tr id='8Kswn'><dt id='8Kswn'><q id='8Kswn'><span id='8Kswn'><b id='8Kswn'><form id='8Kswn'><ins id='8Kswn'></ins><ul id='8Kswn'></ul><sub id='8Kswn'></sub></form><legend id='8Kswn'></legend><bdo id='8Kswn'><pre id='8Kswn'><center id='8Kswn'></center></pre></bdo></b><th id='8Kswn'></th></span></q></dt></tr></i><div id='8Kswn'><tfoot id='8Kswn'></tfoot><dl id='8Kswn'><fieldset id='8Kswn'></fieldset></dl></div>

                • 本文介绍了PyQt5:样式表和来自 QWidget 的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我的代码生成一个包含两个 QWidget 的窗口,一个红色 (wg1) 和一个蓝色 (wg2).如果它们变成继承自 QWidget 的 Fields(注释掉),它们的颜色就会消失.

                  My code generates a Window containing two QWidgets, a red (wg1) and a blue (wg2) one. If they become Fields (commented out) which inherits from QWidget, their color disappears.

                  我的问题是:为什么 Field(继承自 QWidget)需要有一行

                  My question is: Why is it necessary for a Field (which inherits from QWidget), to have the line

                  self.setAttribute(Qt.WA_StyledBackground, True)

                  在它的 __init__ 方法中,而 QWidget 不需要这一行?

                  in its __init__-method while a QWidget doesn't need this line?

                  import sys
                  from PyQt5 import QtWidgets
                  from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout
                  from PyQt5.QtCore import Qt
                  
                  class Field(QWidget):
                      def __init__(self, name):
                          super().__init__()
                          self.name = name
                          # self.setAttribute(Qt.WA_StyledBackground, True)# !!!
                          
                      def mousePressEvent(self, event):
                          print(f" click makes {self.name} green")
                          self.setStyleSheet("background: #00ff00;")
                  
                  if __name__ == "__main__":
                      app = QApplication(sys.argv)
                      root = QWidget()
                      grid = QGridLayout()
                      root.setLayout(grid)
                      root.resize(300,100)
                  
                      wg1 = QWidget()
                      wg2 = QWidget()
                      # wg1 = Field('wg1')
                      # wg2 = Field('wg2')
                  
                      wg1.setStyleSheet("background: #aa1111;")
                      grid.addWidget(wg1, 0, 0)
                  
                      wg2.setStyleSheet("background: #1111aa;")
                      grid.addWidget(wg2, 0, 2)
                  
                      root.show()
                      sys.exit(app.exec_())
                  

                  推荐答案

                  解释在样式表参考中与 QWidget 相关的部分:

                  如果您从 QWidget 继承,您需要为您的自定义 QWidget 提供一个paintEvent,如下所示:

                  If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

                  默认情况下,QWidget 会默认执行此操作,但 QWidget 子类不会,因此您要么设置标志,要么实现paintEvent.

                  By default, QWidget does that by default, but QWidget subclasses do not, so you either set the flag or you implement the paintEvent.

                  无论如何,请考虑使用 样式表选择器 并避免通用/通用语法,因为它们通常会导致复杂的子小部件出现意外行为.

                  In any case, consider using stylesheet selectors and avoid generic/universal syntax as they often result in unexpected behavior for complex child widgets.

                  这篇关于PyQt5:样式表和来自 QWidget 的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 PyQt5 中检测外部键盘事件 下一篇:为 QPrintWidget、QPrintPrevwiwWidget 添加页脚

                  相关文章

                  最新文章

                      • <bdo id='EZO78'></bdo><ul id='EZO78'></ul>
                      <i id='EZO78'><tr id='EZO78'><dt id='EZO78'><q id='EZO78'><span id='EZO78'><b id='EZO78'><form id='EZO78'><ins id='EZO78'></ins><ul id='EZO78'></ul><sub id='EZO78'></sub></form><legend id='EZO78'></legend><bdo id='EZO78'><pre id='EZO78'><center id='EZO78'></center></pre></bdo></b><th id='EZO78'></th></span></q></dt></tr></i><div id='EZO78'><tfoot id='EZO78'></tfoot><dl id='EZO78'><fieldset id='EZO78'></fieldset></dl></div>
                    1. <legend id='EZO78'><style id='EZO78'><dir id='EZO78'><q id='EZO78'></q></dir></style></legend>
                    2. <tfoot id='EZO78'></tfoot>

                      <small id='EZO78'></small><noframes id='EZO78'>