Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Model\View Framework
Qt 6.11 is out! See what's new in the release blog

Model\View Framework

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seba84
    wrote on last edited by
    #1

    Hello all,

    I have a QSqlRelationalTableModel which read data from a sqlite database. This model is used in a QTableView widget on my main window to view and edit data. I would like to do two things:

    To set the items horizontal alignement to centered on some columns (for program aesthetics),

    To enable items edit only on some of the table columns.

    I searched Qt documentation following all object hierachies and got nothing for both of the subjects.
    Thanks for your help!
    Seba84

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Easiest is to use a proxy model. You can subclass [[doc:QIdentityProxyModel]] (if you're on 4.8) or [[doc::QSortFilterProxyModel]] (for lower versions). There, you reimplement the data() method to return the Qt::AlignHCenter for the Qt::TextAlignmentRole. For all other roles, or for columns you don't want to center, you simply return QIdentityProxyModel::data().

      For the read-only columns, you will have to reimplement the flags() method as well.

      There are "examples":http://developer.qt.nokia.com/wiki/QSortFilterProxyModel_subclass_for_readonly_columns_columns_with_checkboxes_and_password_columns on the wiki.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Seba84
        wrote on last edited by
        #3

        Thanks a lot Andre for your rapid answer! I thought I had the last version but not, I am using 4.7.4. I will subclass QSortFilterProxyModel as you just explained for both functionallities.

        Anyway, what I ask to do is very simple and I think I am not the only one to need it. I don't understand why this function is not already inside the Qt library, maybe implemented directly on the model/view classes.

        Seba84

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Seba84
          wrote on last edited by
          #4

          For those who want to solve the same problem, I have written a Wiki page with the code snippet "link":https://developer.qt.nokia.com/wiki/QSortFilterProxyModel_subclass_for_text_alignment_-and_readonly_columns.

          Thanks again Andre for the solution proposal. I hope the code I have written can evolve to some more complete class which can satisfy every user. Feel free to edit this Wiki page! (which by the way I don't know if it is possible)

          Seba84

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved