环境配置错误

设置 QObject 属性

  • 动态属性

    • setter & getter

      • obj.setProperty("propName", "value")
      • obj.property("propName")
    • 遍历

      • obj.dynamicPropertyNames()
1
2
3
4
5
6
obj.setProperty("Full name", "Summer Festival")
obj.setProperty("Age", 16)
obj.setProperty("Gender", "female")
names = obj.dynamicPropertyNames()
for name in names:
    print(str(name, encoding="utf-8"))

使用 CSS

  • 方法

    • QObject.setStyleSheet("css string")
  • 定位

    • QObject.setObjectName

      • css id selector: "QLabel#objectName"
      • eg:

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        
        import sys
        from PySide6.QtWidgets import (
            QWidget,
            QLabel,
            QApplication
        )
        
        
        class Window(QWidget):
        
            def __init__(self):
            super().__init__()
            # 设置 CSS
            QApplication.instance().setStyleSheet("QLabel#yoyoyo {font-size: 20px; color: green; font-weight: bold;}")
            self.init_ui()
        
            def init_ui(self):
            self.setGeometry(400, 400, 300, 200)
            self.setWindowTitle("Summer Festival")
        
            label1 = QLabel(self)
            # Set name, QObject has properties, child controls can use
            # CSS 定位
            label1.setObjectName("yoyoyo")
            label1.setText("Shasha")
            label1.move(50, 50)
        
            label2 = QLabel(self)
            # Do not set name
            label2.setText("Respiratory World")
            label2.move(50, 100)
        
        
        app = QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec_())

子 Widget 遍历

  • QObject.children()

    • 遍历直属 子 Widget
  • QObject.findChildren(QObject)

    • 遍历所有 QObject

阻塞 Signal 传递

  • 操作

    • obj.mySignal.blockSignals(True)
  • 判断

    • obj.mySignal.signalIsBlocked()

signals

  • obj.objectNameChanged
  • obj.destroyed
  • button.clicked

类型判断

  • obj.isWidgetType()

layout 布局

  • 注意

    • layout::addWidget() 是正确做法
    • layout::addChildrenWidget()

      • protected 方法,调用是错误的

删除

  • obj.deleteLater()
  • 信号 signal

    • obj.destroyed
    • obj.