user6190168
user6190168

Reputation:

Set Vertical and Horizontal alignment in PySide2

The QSplashScreen.showMessage()method accepts an alignment flag i.e Qt.AlignLeft. What is the syntax for setting both a horizontal and vertical alignment flag? All attempts yields "too many variables" error.

Upvotes: 5

Views: 8672

Answers (2)

eyllanesc
eyllanesc

Reputation: 244369

Qt::Alignment are QFlags that use bitwise operators &, | and ^, in the case you want to combine flags you must use the | operator, in the next part I show an example:

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_())

Upvotes: 3

JaminSore
JaminSore

Reputation: 3936

From the Qt5 docs, alignment flags are bit fields so to combine them you use the | operator (e.g., Qt.AlignLeft | Qt.AlignTop for "top left" alignment)

Upvotes: 4

Related Questions