Skip to content Skip to sidebar Skip to footer

How I Can Align Values Exported From Database With Query In Pyqt5 Table View

I have the following issue. I have a very simple code which is connecting to my local sql database, and from there I am exporting a data set with a query. Then I display this data

Solution 1:

You have to modify the displayAlignment property of QStyleOptionViewItem and apply the delegate to all columns:

class BlobDelegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(BlobDelegate, self).initStyleOption(option, index)
        option.displayAlignment = QtCore.Qt.AlignCenter

    def displayText(self, value, locale):
        if isinstance(value, QtCore.QByteArray):
            value = value.data().decode()
        return super(BlobDelegate, self).displayText(value, locale)
delegate = BlobDelegate(w)
w.setItemDelegate(delegate)

Post a Comment for "How I Can Align Values Exported From Database With Query In Pyqt5 Table View"