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. Forbid selection of the column of view(model/view )

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.4k 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.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    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

    @
    class disableSelectionDelegate : public QStyledItemDelegate
    {
    public:
    explicit disableSelectionDelegate(QObject *parent = 0)
    : QStyledItemDelegate(parent)
    {
    }

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index) const
    {
        
        painter->drawText(option.rect, Qt::AlignCenter, index.data().toString() );
    }   
    

    };
    @

    Thank you very much

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DKvita
      wrote on last edited by
      #2

      You need subclass QDirModel and reimplement flags() function.

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on last edited by
        #3

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

        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