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

    @
    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



  • 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);
    }
    @



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.