Results 1 to 6 of 6

Thread: align column of QTableView in pyqt5

  1. #1
    Join Date
    Apr 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default align column of QTableView in pyqt5

    I have this code

    Qt Code:
    1. class ManifestModel(QtSql.QSqlTableModel):
    2.  
    3. def __init__(self, parent=None, db=QtSql.QSqlDatabase()):
    4. super(ManifestModel, self).__init__(parent, db)
    5.  
    6.  
    7. def flags(self, index):
    8. if (index.column() == 4):
    9. return QtCore.Qt.ItemIsEnabled
    10. elif (index.column() == 6):
    11. return QtCore.Qt.ItemIsEnabled
    12. elif (index.column() == 3):
    13. return QtCore.Qt.AlignHCenter
    14. else:
    15. return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. ui.label = QtWidgets.QLabel(ui.page)
    2. ui.label.setGeometry(QtCore.QRect(308, 0, 131, 20))
    3. ui.label.setAlignment(QtCore.Qt.AlignCenter)
    To copy to clipboard, switch view to plain text mode 

    So what do I have to do to change the alignment in a QTableView column?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default Re: align column of QTableView in pyqt5

    I found this code:

    Qt Code:
    1. QVariant MyModel::data(const QModelIndex& index, int role) const {
    2. if (index.column() == yourCellIndex && role == Qt::TextAlignmentRole) {
    3. return Qt::AlignLeft;
    4. } else {
    5. return QAbstractTableModel::data(index, role);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately it is in c++ and I don't know how to convert it to python. What does :: mean anyway?

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: align column of QTableView in pyqt5

    Quote Originally Posted by nlgootee View Post
    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 View Post
    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:

    Qt Code:
    1. class ManifestModel(QtSql.QSqlTableModel):
    2.  
    3. def __init__(self, parent=None, db=QtSql.QSqlDatabase()):
    4. super(ManifestModel, self).__init__(parent, db)
    5.  
    6.  
    7. def flags(self, index):
    8. if (index.column() == 4):
    9. return QtCore.Qt.ItemIsEnabled
    10. elif (index.column() == 6):
    11. return QtCore.Qt.ItemIsEnabled
    12. else:
    13. return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
    14.  
    15. def data(self, index, role):
    16. if (index.column() == 2 and role == QtCore.Qt.TextAlignmentRole):
    17. return QtCore.Qt.AlignHCenter
    18. else:
    19. return QtSql.QSqlTableModel.data(index, role)
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by jefftee; 1st July 2016 at 20:56.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. The following user says thank you to jefftee for this useful post:

    nlgootee (6th July 2016)

  6. #5
    Join Date
    Apr 2016
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products

    Default 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.

  7. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: align column of QTableView in pyqt5

    Quote Originally Posted by nlgootee View Post
    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?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 4
    Last Post: 27th February 2014, 09:42
  2. Set an editable column for QTableView
    By SIFE in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2012, 23:09
  3. QTableView with a column of images
    By elanari in forum Qt Programming
    Replies: 13
    Last Post: 7th February 2012, 12:25
  4. QTableView and problems with align
    By mamyte03@gmail.com in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 07:10
  5. Fixating column from QTableView ?
    By pinktroll in forum Qt Programming
    Replies: 2
    Last Post: 4th September 2007, 10:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.