Skip to content

Instantly share code, notes, and snippets.

@peteristhegreat
Created January 17, 2017 20:59
Show Gist options
  • Save peteristhegreat/c92a56ca29f41d5c96f644cb906e7566 to your computer and use it in GitHub Desktop.
Save peteristhegreat/c92a56ca29f41d5c96f644cb906e7566 to your computer and use it in GitHub Desktop.
Read-only QItemDelegate for use with Qt QTableView
#include "readonlydelegate.h"
ReadOnlyDelegate::ReadOnlyDelegate(QObject *parent) :
QItemDelegate(parent)
{
}
bool ReadOnlyDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
return false;
}
QWidget* ReadOnlyDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
{
return NULL;
}
#ifndef READONLYDELEGATE_H
#define READONLYDELEGATE_H
#include <QItemDelegate>
// based off of http://stackoverflow.com/a/19251035/999943
class ReadOnlyDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ReadOnlyDelegate(QObject *parent = 0);
signals:
public slots:
protected:
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
QWidget* createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const;
};
#endif // READONLYDELEGATE_H
#include "readonlydelegate.h"
// ..
for(int c = 0; c < view->model()->columnCount(); c++)
{
if(c != 1)
view->setItemDelegateForColumn(c, new ReadOnlyDelegate(view));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment