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 to make only certain cells in tableview editable

How to make only certain cells in tableview editable

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 520 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1
    this->setEditTriggers(QAbstractItemView::NoEditTriggers | QAbstractItemView::DoubleClicked);
    

    I am wondering how to make only certain cells in tableview editable. For now, I use setEditTriggers to make it editable.

    The problem is that all cells are editable.
    But I want to make only certain cells editable.
    How can I do that?

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IknowQT
      wrote on last edited by
      #5

      self-response

      QWidget* usrTableDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
      {
      //QWidget* w = QStyledItemDelegate::createEditor(parent, option, index);
      //QLineEdit* lineEdit = qobject_cast<QLineEdit*>(w);

      usrSetupLineEdit* pEdit = new usrSetupLineEdit(parent);//qobject_cast<usrSetupLineEdit*>(lineEdit);
      pEdit->SetOnlyNumber(true, true);
      pEdit->SetRange(-4.0, 4.0);
      pEdit->setAlignment(Qt::AlignCenter);
      
      
      return pEdit;
      

      }

      void usrTableDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
      {
      editor->setGeometry(option.rect);
      }

      1 Reply Last reply
      0
      • I Offline
        I Offline
        IknowQT
        wrote on last edited by
        #2
        void wStandardCurve::CellDoubleClicked(const QModelIndex& index)
        {
        	if (index.column() == 0)
        	{
        		//index.data().isE
        
        		QStandardItem* cast = m_pDataModel->item(index.row(), index.column());
        		usrStandardItem* item = static_cast<usrStandardItem*>(cast);
        
        		item->setFlags(item->flags() | Qt::ItemIsEditable);
        		item->setEditable(true);
        		item->isEditable();
        		//QString str = item->text();
        		QString str = index.data().toString();
        		
        
        	}
        	else if (index.column() == 2)
        	{
        		QStandardItem* cast = m_pDataModel->item(index.row(), index.column());
        		usrStandardItem* item = static_cast<usrStandardItem*>(cast);
        		
        		item->setFlags(Qt::ItemIsEditable);
        		item->setEditable(true);
        		item->isEditable();
        		QString str = item->text();
        
        		//QString str = index.data().toString();
        
        	}
        
        }
        

        I tried this way but couldn't get the result I want.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #3

          If particular items in your model should not be (or are not) editable in any view then the items in the model should have the default Qt::ItemIsEditable flag removed (not added as you attempt above). You can do this calling QStandardItem::setEditable(), or through the model interface with setFlags().

          If the items in the model are generally editable, but should be read only in a particular view only, then you can approach this by using a QAbstractProxyModel that changes the base model flags on selected items..

          Base model <=> Proxy model <=> View
          
          1 Reply Last reply
          2
          • I Offline
            I Offline
            IknowQT
            wrote on last edited by
            #4

            cdf4d25f-b858-4a04-a684-1f96262cc669-image.png

            QWidget* usrTableDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
            {
            	QWidget* w = QStyledItemDelegate::createEditor(parent, option, index);	
            	//QLineEdit* lineEdit = qobject_cast<QLineEdit*>(w);
            
            	usrSetupLineEdit* pEdit = new usrSetupLineEdit(w);//qobject_cast<usrSetupLineEdit*>(lineEdit);
            	pEdit->SetOnlyNumber(true, true);
            	pEdit->SetRange(-4.0, 4.0);
            	pEdit->setAlignment(Qt::AlignCenter);
            
            
            	
            	return pEdit;
            }
            
            
            I'll ask you one more thing
            When double-clicking, I want to show the custom control I made.
            
            The result does not happen inside the table cell, but outside the table cell.
            How do I put it into a table?
            
            1 Reply Last reply
            0
            • I Offline
              I Offline
              IknowQT
              wrote on last edited by
              #5

              self-response

              QWidget* usrTableDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
              {
              //QWidget* w = QStyledItemDelegate::createEditor(parent, option, index);
              //QLineEdit* lineEdit = qobject_cast<QLineEdit*>(w);

              usrSetupLineEdit* pEdit = new usrSetupLineEdit(parent);//qobject_cast<usrSetupLineEdit*>(lineEdit);
              pEdit->SetOnlyNumber(true, true);
              pEdit->SetRange(-4.0, 4.0);
              pEdit->setAlignment(Qt::AlignCenter);
              
              
              return pEdit;
              

              }

              void usrTableDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
              {
              editor->setGeometry(option.rect);
              }

              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