[PyQt] DeprecationWarning using TextAlignmentRole in a QStandardItem subclass

Sibylle Koczian nulla.epistola at web.de
Sat Jan 18 12:15:40 GMT 2020


If I execute the following script I get this warning:

Warning (from warnings module):
   File "D:\Sibylle\Src\PythonVersuche\PyQt\test_warning.py", line 77
     app.exec_()
DeprecationWarning: an integer is required (got type Alignment).
Implicit conversion to integers using __int__ is deprecated, and may be
removed in a future version of Python.

The script works as expected, but of course I want to know what is
happening here. The warning doesn't appear if I comment out the data()
method of the model class, and it doesn't appear if I run the script
with test_model() instead of main(). I suppose in that second case the
TextAlignmentRole is not used, right?

I tried to google the warning but got only some entries about the same
sort of warning in numpy regarding conversions from float to int.

Python version is 3.8.1, PyQt5 is version 5.14.1 on windows 10, same
versions on ArchLinux.

#!/usr/bin/env python3
# test_warning.py
# Example for DeprecationWarning

import sys
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets

NUMERALS = [(1, "one"), (7, "seven"), (34, "thirty-four"),
            (108, "one hundred and eight")]

class NumeralModel(QtGui.QStandardItemModel):

     def __init__(self, parent=None):
         super().__init__(0, 2, parent)
         self.setHorizontalHeaderLabels(["Number", "Text"])
         self.import_data(NUMERALS)

     def data(self, index, role):
         """
         Number columns should be right-aligned.
         """
         if role == QtCore.Qt.TextAlignmentRole:
             col = index.column()
             if col == 0:
                 h_align = QtCore.Qt.AlignRight
             else:
                 h_align = QtCore.Qt.AlignLeft
             return h_align | QtCore.Qt.AlignVCenter
         return super().data(index, role)

     def import_data(self, data):
         self.removeRows(0, self.rowCount())
         for (number, word) in data:
             number_item = QtGui.QStandardItem()
             number_item.setData(number, QtCore.Qt.DisplayRole)
             word_item = QtGui.QStandardItem(word)
             self.appendRow([number_item, word_item])

     def show_data(self):
         for row in range(self.rowCount()):
             number_data = self.item(row, 0).data(QtCore.Qt.DisplayRole)
             word_data = self.item(row, 1).data(QtCore.Qt.DisplayRole)
             print(f"{number_data} = {word_data}")


class NumeralsMW(QtWidgets.QMainWindow):

     def __init__(self, parent=None):
         super().__init__(parent)
         self.model = NumeralModel(self)
         mainwidget = self.create_mainwidget()
         self.setCentralWidget(mainwidget)
         self.view.setModel(self.model)
         self.btClose.clicked.connect(self.close)

     def create_mainwidget(self):
         self.view = QtWidgets.QTableView(self)
         self.btClose = QtWidgets.QPushButton("Fertig")
         mainLayout = QtWidgets.QVBoxLayout()
         mainLayout.addWidget(self.view)
         mainLayout.addWidget(self.btClose)
         mw = QtWidgets.QWidget()
         mw.setLayout(mainLayout)
         return mw

def test_model(args):
     app = QtCore.QCoreApplication(args)
     model = NumeralModel()
     model.show_data()

def main(args):
     app = QtWidgets.QApplication(args)
     mainwin = NumeralsMW()
     mainwin.show()
     app.exec_()

if __name__ == "__main__":
     main(sys.argv)
     #test_model(sys.argv)


More information about the PyQt mailing list