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. [solved] How to disable cells in QTableView
Forum Updated to NodeBB v4.3 + New Features

[solved] How to disable cells in QTableView

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 18.5k 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.
  • H Offline
    H Offline
    Hareen Laks
    wrote on last edited by
    #1

    Dear all,

    I have QTableView which created using QStandardItemModel.

    I also create QSpinBox delegate to edit the cells data.

    But I have to disable some SpinBoxes to avoid change value by user.

    So my problem is how to disable some cells of QTabelView.

    I find some were recommended to use
    @tableView.setEditTriggers(QAbstractItemView::NoEditTriggers);@

    But it disable entire TableView.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      You can use
      @void QStandardItem::setFlags(Qt::ItemFlags flags)@
      for that. "Flags":http://qt-project.org/doc/qt-5/qt.html#ItemFlag-enum .

      E 1 Reply Last reply
      0
      • H Offline
        H Offline
        Hareen Laks
        wrote on last edited by
        #3

        qxoz,

        Thank you very much for your helping hand. Your solution is working fine.

        I used below code.

        @
        for (int row = 0; row < rows().count(); ++row){
        for (int col = 0; col < cols().count(); ++col){

             QModelIndex index = item_model->index(row,col,QModelIndex());
        
              if(toBeDisabled()){
                     QStandardItem *my_item = item_model->itemFromIndex(index);
                      my_item->setFlags(!Qt::ItemIsEditable);
              }
              else{
                        table_view->openPersistentEditor(index);
              }
              item_model->setData(index,0);
         }
        

        }@

        Thanks again.

        1 Reply Last reply
        0
        • Q qxoz

          You can use
          @void QStandardItem::setFlags(Qt::ItemFlags flags)@
          for that. "Flags":http://qt-project.org/doc/qt-5/qt.html#ItemFlag-enum .

          E Offline
          E Offline
          Echeban
          wrote on last edited by
          #4

          @qxoz said in [solved] How to disable cells in QTableView:

          You can use
          @void QStandardItem::setFlags(Qt::ItemFlags flags)@
          for that. "Flags":http://qt-project.org/doc/qt-5/qt.html#ItemFlag-enum .

          How do you write this code in PyQt ? I wish I could translate Qt to PyQt code by just looking at it, but I can't.

          JonBJ 1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            model.item(row,column).setFlags(Qt.NoItemFlags)

            I wish I could translate Qt to PyQt code

            As a general rule if there is a symbol you don't understand replace it with . and it works :)

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • E Echeban

              @qxoz said in [solved] How to disable cells in QTableView:

              You can use
              @void QStandardItem::setFlags(Qt::ItemFlags flags)@
              for that. "Flags":http://qt-project.org/doc/qt-5/qt.html#ItemFlag-enum .

              How do you write this code in PyQt ? I wish I could translate Qt to PyQt code by just looking at it, but I can't.

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

              @Echeban
              I see that the OP, @Hareen-Laks , wrote as his own solution 5 years ago:

              my_item->setFlags(!Qt::ItemIsEditable);
              

              If you're thinking of translating that into Python/PyQt, I wouldn't. 'coz it's wrong ;-) (Compiles but isn't the right code.)

              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