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. How do I make a QTableView readonly?

How do I make a QTableView readonly?

Scheduled Pinned Locked Moved Solved General and Desktop
table viewreadonly
7 Posts 4 Posters 912 Views
  • 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.
  • J Offline
    J Offline
    jdent
    wrote on 21 Mar 2024, 14:14 last edited by
    #1

    Hi,

    I need to prevent the user to- editing the columns in a QTableView, yet I don't find setReadOnly(). How is this accomplished?

    Regards,
    Juan

    C 1 Reply Last reply 21 Mar 2024, 14:34
    0
    • J jdent
      21 Mar 2024, 17:09

      @Christian-Ehrlicher sorry did not find any info on ItemFlags for QTableView
      can you please be more specific?

      M Offline
      M Offline
      mpergand
      wrote on 21 Mar 2024, 17:55 last edited by
      #6

      @jdent
      Try :

      tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

      or in your item model :

      Qt::ItemFlags  flags(const QModelIndex &index) const
                     {
                     return Qt::ItemIsEnabled;
                     }
      
      1 Reply Last reply
      2
      • J jdent
        21 Mar 2024, 14:14

        Hi,

        I need to prevent the user to- editing the columns in a QTableView, yet I don't find setReadOnly(). How is this accomplished?

        Regards,
        Juan

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 21 Mar 2024, 14:34 last edited by
        #2

        @jdent don't return that it's editable in flags()

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        J 1 Reply Last reply 21 Mar 2024, 14:36
        2
        • C Christian Ehrlicher
          21 Mar 2024, 14:34

          @jdent don't return that it's editable in flags()

          J Offline
          J Offline
          jdent
          wrote on 21 Mar 2024, 14:36 last edited by
          #3

          @Christian-Ehrlicher No, that would make the model readonly - I only want the QTableView readonly...

          C 1 Reply Last reply 21 Mar 2024, 14:37
          0
          • J jdent
            21 Mar 2024, 14:36

            @Christian-Ehrlicher No, that would make the model readonly - I only want the QTableView readonly...

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 21 Mar 2024, 14:37 last edited by
            #4

            @jdent The flags are for the view. Please read the docs.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            J 1 Reply Last reply 21 Mar 2024, 17:09
            1
            • C Christian Ehrlicher
              21 Mar 2024, 14:37

              @jdent The flags are for the view. Please read the docs.

              J Offline
              J Offline
              jdent
              wrote on 21 Mar 2024, 17:09 last edited by
              #5

              @Christian-Ehrlicher sorry did not find any info on ItemFlags for QTableView
              can you please be more specific?

              M J 2 Replies Last reply 21 Mar 2024, 17:55
              0
              • J jdent
                21 Mar 2024, 17:09

                @Christian-Ehrlicher sorry did not find any info on ItemFlags for QTableView
                can you please be more specific?

                M Offline
                M Offline
                mpergand
                wrote on 21 Mar 2024, 17:55 last edited by
                #6

                @jdent
                Try :

                tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

                or in your item model :

                Qt::ItemFlags  flags(const QModelIndex &index) const
                               {
                               return Qt::ItemIsEnabled;
                               }
                
                1 Reply Last reply
                2
                • J jdent has marked this topic as solved on 21 Mar 2024, 18:14
                • J jdent
                  21 Mar 2024, 17:09

                  @Christian-Ehrlicher sorry did not find any info on ItemFlags for QTableView
                  can you please be more specific?

                  J Offline
                  J Offline
                  JonB
                  wrote on 21 Mar 2024, 18:50 last edited by
                  #7

                  @jdent said in How do I make a QTableView readonly?:

                  @Christian-Ehrlicher sorry did not find any info on ItemFlags for QTableView

                  In Qt's model-view paradigm the flags are returned by the model: Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const, values flags Qt::ItemFlags. Qt::ItemIsEditable determines whether the model tells the view items are editable or not. A view like QTableView will call this, behind the scenes without you doing anything, and make its interface correspond to what the model wants. @mpergand's implementation does not return Qt::ItemIsEditable so items won't be editable. QTreeView will show items as non-editable.

                  1 Reply Last reply
                  0

                  5/7

                  21 Mar 2024, 17:09

                  • Login

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