Can you explain what the function arguments should take

setAlignment (Union, Qt_Alignment = None, Qt_AlignmentFlag = None):

Thanks in advance.

  • What type? - mkkik

1 answer 1

Let's look at the description of the PyQt5 method:

 # real signature unknown; restored from __doc__ def setAlignment(self, Union, Qt_Alignment=None, Qt_AlignmentFlag=None): """ setAlignment(self, Union[Qt.Alignment, Qt.AlignmentFlag]) """ pass 

It shows that the real signature described in __doc__ has one parameter (this is confirmed when trying to pass more than one parameter to the function), which is either Qt.Alignment or Qt.AlignmentFlag .

Therefore, the parameters Union, Qt_Alignment=None, Qt_AlignmentFlag=None in fact one parameter.

Additional confirmation can be seen if you look at the description of the same method in the Qt documentation itself :

 void setAlignment(Qt::Alignment) 

And then the question may arise:

Why in PyQt5 this parameter is described as Union [Qt.Alignment, Qt.AlignmentFlag], and in the original documentation just Qt :: Alignment?

To answer the question, you also need to look in the Qt documentation for listing Qt :: Alignment :

 enum Qt::AlignmentFlag flags Qt::Alignment 

Qt::Alignment is a set of Qt::AlignmentFlag flags: Qt::AlignLeft , Qt::AlignRight , etc., in other words, Qt::Alignment can transmit a specific flag or several at once: for example, the Qt::AlignCenter is built from Qt::AlignVCenter and Qt::AlignHCenter

To compile a list of flags they need to be listed via bitwise или - operator | :

 my_align_center = Qt::AlignVCenter | Qt::AlignHCenter 

And Union[Qt.Alignment, Qt.AlignmentFlag] is an indication of one of the listed variations in the parameter that fits with what I wrote above.

I explained why Union[Qt.Alignment, Qt.AlignmentFlag] in the description of the setAlignment parameter.

Now, practice a little.


Below is an example of using Alignment :

 from PyQt5.QtWidgets import QLabel, QApplication, QGridLayout, QWidget from PyQt5.QtCore import Qt def create_label(alignment): label = QLabel('Test') label.setFixedSize(40, 40) label.setFrameStyle(QLabel.Box) label.setAlignment(alignment) return label app = QApplication([]) layout = QGridLayout() layout.addWidget(create_label(Qt.AlignLeft), 0, 0) layout.addWidget(create_label(Qt.AlignLeft | Qt.AlignBottom), 0, 1) layout.addWidget(create_label(Qt.AlignLeft | Qt.AlignVCenter), 0, 2) layout.addWidget(create_label(Qt.AlignLeft | Qt.AlignTop), 0, 3) layout.addWidget(create_label(Qt.AlignHCenter | Qt.AlignTop), 0, 4) layout.addWidget(create_label(Qt.AlignRight | Qt.AlignVCenter), 0, 5) w = QWidget() w.setLayout(layout) w.show() app.exec() 

Screenshot:

enter image description here

  • Can you please tell us again what is the difference between AlignmentFlag and Alignment - Gleb
  • one
    AlignmentFlag - a specific flag, for example, Left. Alignment - a set of flags from one and more - gil9red