Results 1 to 6 of 6

Thread: set QTableView column to read-only in Python

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

    Default set QTableView column to read-only in Python

    I have searched the archives and I found the following code:

    Qt Code:
    1. class ManifestModel: public QSqlTableModel
    2. {
    3. public:
    4. ManifestModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ):
    5. QSqlTableModel(parent, db)
    6. { }
    7. ~ManifestModel() { }
    8. Qt::ItemFlags flags ( const QModelIndex & index ) const
    9. {
    10. if (index.column() == 2)
    11. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    12. else
    13. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 

    Then use ManifestModel in place of QSqlTableModel.

    This does exactly what I need to do, but I don't understand C++ syntax. Could someone please convert this to Python for me. I would really appreciate it.

  2. #2
    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: set QTableView column to read-only in Python

    I won't do the literal translation for you, but I'll point you in the right direction:


    • ManifestModel is a class that subclasses QSqlTableModel
    • The ManifestModel has one method (function) that is overridden from the QSqlTableModel::flags method that take one argument, a QModelIndex
    • The method checks if the column in the QModelIndex is the 2nd column, it sets the flags to ItemIsEnabled + ItemIsSelectable + ItemIsEditable, otherwise it's just enabled and selectable (not editable)


    After reading the documentation for QSqlTableModel and/or QAbstractItemModel, you should be able to figure it out, assuming you know how to create a class in python, etc.

    Good luck.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

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

    Default Re: set QTableView column to read-only in Python

    I assume that you know that the documentation for pyqt is written in c++ and therefore is not really understandable for someone who does not know c++ syntax., or did you miss the part where I said that I don't know c++ syntax. Why did you bother to answer if you didn't want to actually help?

  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: set QTableView column to read-only in Python

    Quote Originally Posted by nlgootee View Post
    Why did you bother to answer if you didn't want to actually help?
    Heh, I thought I *was* actually helping you. I told you exactly what the C++ code was doing, so that you could write the same in Python. If you can't take that and run with it, then you are correct, I can't (or won't) help you.

    Good luck.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

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

    Default Re: set QTableView column to read-only in Python

    OMG! A c++ programmer with an attitude. You're a real asset to the list.

  6. The following user says thank you to nlgootee for this useful post:

    jefftee (30th June 2016)

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

    Default Re: set QTableView column to read-only in Python

    Quote Originally Posted by nlgootee View Post
    OMG! A c++ programmer with an attitude. You're a real asset to the list.
    Yes, jefftee is an asset, a poster with the attitude to help people in need of advice.

    Providing a translation of C++ code into general programming concepts, which a developer using a different language can easily translate into code again.

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    jefftee (1st July 2016)

Similar Threads

  1. Replies: 4
    Last Post: 27th February 2014, 09:42
  2. Replies: 0
    Last Post: 11th April 2010, 21:26
  3. Make a column read-only in a QSqlTableModel/QTableView
    By graciano in forum Qt Programming
    Replies: 0
    Last Post: 15th November 2009, 18:18
  4. Replies: 6
    Last Post: 6th August 2009, 17:18
  5. Replies: 8
    Last Post: 28th May 2007, 20:32

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.