August 9, 2012

stereomatching stereomatchi..
Robot Herder
218 posts

Forbid selection of the column of view(model/view )

 

When I show the QDirModel by QTableView
There will be four columns — Name, Size, Type, Date Modified
I don’t want the user be able to select Size, Type, and Date Modified
How could I disable the selection on specific columns?

The most easiest solution I could think of is design a delegate to
repaint the columns I don’t want the user to select

  1. class disableSelectionDelegate : public QStyledItemDelegate
  2. {
  3. public:    
  4.     explicit disableSelectionDelegate(QObject *parent = 0)
  5.         : QStyledItemDelegate(parent)
  6.     {
  7.     }
  8.  
  9.     void paint(QPainter *painter, const QStyleOptionViewItem &option,
  10.                const QModelIndex &index) const
  11.     {
  12.        
  13.         painter->drawText(option.rect, Qt::AlignCenter, index.data().toString() );
  14.     }  
  15. };

Thank you very much

2 replies

August 9, 2012

Denis Kvita Denis Kvita
Lab Rat
9 posts

You need subclass QDirModel and reimplement flags() function.

  1. Qt::ItemFlags DirModel::flags(const QModelIndex &index) const
  2. {
  3.  if(index.column() == 2) return QDirModel::flags(index)&~Qt::ItemIsSelectable;
  4.  return QDirModel::flags(index);
  5. }

August 10, 2012

stereomatching stereomatchi..
Robot Herder
218 posts

Thanks, I didn’t discover I could override the flag

 
  ‹‹ Where is the Qt5 beta release?      Qt program under linux problem ››

You must log in to post a reply. Not a member yet? Register here!