align column of QTableView in pyqt5
I have this code
Code:
super(ManifestModel, self).__init__(parent, db)
def flags(self, index):
if (index.column() == 4):
return QtCore.Qt.ItemIsEnabled
elif (index.column() == 6):
return QtCore.Qt.ItemIsEnabled
elif (index.column() == 3):
return QtCore.Qt.AlignHCenter
else:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
when I run it I get an error:
builtins.TypeError: invalid result from ManifestModel.flags(), AlignmentFlag cannot be converted to PyQt5.QtCore.ItemFlags in this context
In the same routine that uses ManifestModel I have the code:
Code:
ui.
label = QtWidgets.
QLabel(ui.
page)ui.
label.
setGeometry(QtCore.
QRect(308,
0,
131,
20))ui.label.setAlignment(QtCore.Qt.AlignCenter)
So what do I have to do to change the alignment in a QTableView column?
Re: align column of QTableView in pyqt5
The flags method is for item flags, AlignCenter is not one of those.
If you want to change the text alignment of a cell, you need to return the appropriate alignment value when the data method is called with AlignmentRole.
Cheers,
_
Re: align column of QTableView in pyqt5
I found this code:
Code:
QVariant MyModel
::data(const QModelIndex
& index,
int role
) const { if (index.column() == yourCellIndex && role == Qt::TextAlignmentRole) {
return Qt::AlignLeft;
} else {
}
}
Unfortunately it is in c++ and I don't know how to convert it to python. What does :: mean anyway?
Re: align column of QTableView in pyqt5
Quote:
Originally Posted by
nlgootee
What does :: mean anyway?
It's the scope resolution operator for C++, here's a good description: https://en.wikipedia.org/wiki/Scope_resolution_operator.
Added after 13 minutes:
Quote:
Originally Posted by
nlgootee
Unfortunately it is in c++ and I don't know how to convert it to python.
You need to create a data method for your ManifestModel class much like you did for the "def flags(index)" method, etc. The following should at least get you started:
Code:
super(ManifestModel, self).__init__(parent, db)
def flags(self, index):
if (index.column() == 4):
return QtCore.Qt.ItemIsEnabled
elif (index.column() == 6):
return QtCore.Qt.ItemIsEnabled
else:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
def data(self, index, role):
if (index.column() == 2 and role == QtCore.Qt.TextAlignmentRole):
return QtCore.Qt.AlignHCenter
else:
The code above approximates the C++ translation to the best of my ability (I don't program in Python), so don't blame me if it requires some tweaking... I hope, however, that it's enough to get you going. :)
You'll find that you have to do this all of the time, where you subclass a Qt provided class and override virual methods defined in the documentation. Regardless of whether or not you know C++, you'll have to be able to read the doc, understand the concepts (ignoring syntax) and know how to translate those concepts into Python code/syntax.
Good luck.
Re: align column of QTableView in pyqt5
[QUOTE understand the concepts (ignoring syntax) and know how to translate those concepts into Python code/syntax.
Good luck.[/QUOTE]
I gennerally understand the concepts, but I am having a lot of trouble translating them into Python. Python itself is ok, but Pyqt5 has been really hard.
Re: align column of QTableView in pyqt5
Quote:
Originally Posted by
nlgootee
Python itself is ok, but Pyqt5 has been really hard.
Given that all of the Qt documentation is specific to C++ (as you already pointed out), I understand and feel your pain... I assume you've google'd around and that there is some level of PyQt training/examples that are available?