View Full Version : align column of QTableView in pyqt5
nlgootee
1st July 2016, 17:26
I have this code
class ManifestModel(QtSql.QSqlTableModel):
def __init__(self, parent=None, db=QtSql.QSqlDatabase()):
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:
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?
anda_skoa
1st July 2016, 17:36
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,
_
nlgootee
1st July 2016, 18:35
I found this code:
QVariant MyModel::data(const QModelIndex& index, int role) const {
if (index.column() == yourCellIndex && role == Qt::TextAlignmentRole) {
return Qt::AlignLeft;
} else {
return QAbstractTableModel::data(index, role);
}
}
Unfortunately it is in c++ and I don't know how to convert it to python. What does :: mean anyway?
jefftee
1st July 2016, 20:08
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:
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:
class ManifestModel(QtSql.QSqlTableModel):
def __init__(self, parent=None, db=QtSql.QSqlDatabase()):
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:
return QtSql.QSqlTableModel.data(index, role)
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.
nlgootee
6th July 2016, 17:44
[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.
jefftee
6th July 2016, 18:03
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?
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.