【问题标题】:Set Vertical and Horizontal alignment in PySide2在 PySide2 中设置垂直和水平对齐
【发布时间】:2019-04-22 12:44:48
【问题描述】:

QSplashScreen.showMessage()方法接受一个对齐标志,即Qt.AlignLeft。设置水平和垂直对齐标志的语法是什么?所有尝试都会产生“变量过多”错误。

【问题讨论】:

    标签: python pyqt pyqt5 pyside pyside2


    【解决方案1】:

    the Qt5 docs 开始,对齐标志是位字段,因此您可以使用 | 运算符来组合它们(例如,Qt.AlignLeft | Qt.AlignTop 用于“左上”对齐)

    【讨论】:

      【解决方案2】:

      Qt::Alignment 是使用位运算符 &|^QFlags,如果要组合标志,则必须使用 | 运算符,在下一部分中,我将展示示例:

      import random
      from PyQt5 import QtCore, QtGui, QtWidgets
      
      if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          pixmap = QtGui.QPixmap(640, 480)
          h_alignments = (QtCore.Qt.AlignLeft, QtCore.Qt.AlignRight, QtCore.Qt.AlignHCenter, QtCore.Qt.AlignJustify)
          v_alignments = (QtCore.Qt.AlignTop, QtCore.Qt.AlignBottom, QtCore.Qt.AlignVCenter, QtCore.Qt.AlignBaseline)
          pixmap.fill(QtGui.QColor("green"))
          w  = QtWidgets.QSplashScreen(pixmap)
      
          def on_timeout():
              a = random.choice(h_alignments) | random.choice(v_alignments)
              w.showMessage("Stack Overflow", alignment=a)
      
          timer = QtCore.QTimer(interval=100, timeout=on_timeout)
          timer.start()
          QtCore.QTimer.singleShot(10*1000, QtWidgets.QApplication.quit)
          w.show()
          sys.exit(app.exec_())
      

      【讨论】:

        猜你喜欢
        • 2012-10-10
        • 2017-07-01
        • 2013-11-25
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 1970-01-01
        相关资源
        最近更新 更多