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. Qtableview-How to make noneditable column?
QtWS25 Last Chance

Qtableview-How to make noneditable column?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 2.0k 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.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by VRonin
    #1

    Qtableview-How to make noneditable column( i have 3 qtableview in all table i need to make 2 or 3 column as non editable

    noneditable is class extracting Qstandarditemmodel

    Qt::ItemFlags nonedittablemodel::flags(const QModelIndex& index) const
    
    {
    //table 1
    if (index.column() == 0 || index.column() == 1)
        return  QStandardItemModel::flags(index) & ~Qt::ItemIsEditable;
    else
        return  QStandardItemModel::flags(index) | Qt::ItemIsEditable;
    }
    

    But still its not working for me
    and where i should specify which model because i have 3 model 3Qtableview

    eyllanescE 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      There's no need to create a custom mode for that. You can simply set the flags on the items when you create them.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        There's no need to create a custom mode for that. You can simply set the flags on the items when you create them.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @SGaist said in Qtableview-How to make noneditable column?:

        Hi,
        There's no need to create a custom mode for that. You can simply set the flags on the items when you create them.

        @n-2204
        I said to you at https://forum.qt.io/topic/101447/qtableview-qstandarditemmodel-and-underlying-data-in-a-qlist/12

        You can use @VRonin's setFlags() if you want to set the editability on desired, explicitly specified items.

        If you want to do it your way by overriding flags(), your code should have read:
        ...
        You will have to derive from QStandardItemModel() if you are wanting to override its flags() method. Similar if you choose to use QAbstractItemModel() instead.

        You showed you already had a derived model class with a flags() method. You can do it either way.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          n-2204
          wrote on last edited by
          #4

          Qt::ItemFlags GAS_tool::flags(const QModelIndex& index) const
          {
          //table 1

          if (index.column() == 0) //clumn 1 should not be editable
              return flags(index) & ~Qt::ItemIsEditable;
          else
              return  flags(index) | Qt::ItemIsEditable;
          

          }
          this is Not working

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You have to call the base class implementation.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            0
            • SGaistS SGaist

              You have to call the base class implementation.

              N Offline
              N Offline
              n-2204
              wrote on last edited by
              #6

              @SGaist Qt::ItemFlags GAS_tool::flags(const QModelIndex& index) const
              {
              //table 1
              // QStandardItemModel* model;
              QAbstractItemModel* table1 = ui.tableView->model();
              int iRows = table1->rowCount();
              int iCols = table1->columnCount();
              QModelIndex index = ui.tableView;
              if (index.column() == 0)//clumn 1 should not be editable
              return flags(index) & ~Qt::ItemIsEditable;
              else
              return flags(index) | Qt::ItemIsEditable;

              } like this ?

              1 Reply Last reply
              0
              • N n-2204

                Qtableview-How to make noneditable column( i have 3 qtableview in all table i need to make 2 or 3 column as non editable

                noneditable is class extracting Qstandarditemmodel

                Qt::ItemFlags nonedittablemodel::flags(const QModelIndex& index) const
                
                {
                //table 1
                if (index.column() == 0 || index.column() == 1)
                    return  QStandardItemModel::flags(index) & ~Qt::ItemIsEditable;
                else
                    return  QStandardItemModel::flags(index) | Qt::ItemIsEditable;
                }
                

                But still its not working for me
                and where i should specify which model because i have 3 model 3Qtableview

                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on last edited by eyllanesc
                #7

                @n-2204 A solution that does not require override any method of the model is to use a delegate:

                class ReadOnlyDelegate: public QStyledItemDelegate{
                public:
                    using QStyledItemDelegate::QStyledItemDelegate;
                    QWidget *QStyledItemDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override {
                        return nullptr;
                    }
                };
                
                tableView->setItemDelegateForColumn(0, new ReadOnlyDelegate);
                tableView->setItemDelegateForColumn(1, new ReadOnlyDelegate);
                

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                N 1 Reply Last reply
                1
                • eyllanescE eyllanesc

                  @n-2204 A solution that does not require override any method of the model is to use a delegate:

                  class ReadOnlyDelegate: public QStyledItemDelegate{
                  public:
                      using QStyledItemDelegate::QStyledItemDelegate;
                      QWidget *QStyledItemDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override {
                          return nullptr;
                      }
                  };
                  
                  tableView->setItemDelegateForColumn(0, new ReadOnlyDelegate);
                  tableView->setItemDelegateForColumn(1, new ReadOnlyDelegate);
                  
                  N Offline
                  N Offline
                  n-2204
                  wrote on last edited by
                  #8

                  @eyllanesc Actually for column 1 and 2 i am using intdelegate so is it possible to add one more delegate to same column?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @n-2204 said in Qtableview-How to make noneditable column?:

                    so is it possible to add one more delegate to same column?

                    No but you can modify your delegate and add the functionality @eyllanesc showed you.

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

                    N 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @n-2204 said in Qtableview-How to make noneditable column?:

                      so is it possible to add one more delegate to same column?

                      No but you can modify your delegate and add the functionality @eyllanesc showed you.

                      N Offline
                      N Offline
                      n-2204
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher thanks
                      understood

                      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