Created
January 17, 2017 20:59
-
-
Save peteristhegreat/c92a56ca29f41d5c96f644cb906e7566 to your computer and use it in GitHub Desktop.
Read-only QItemDelegate for use with Qt QTableView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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