Программирование на C++ с использованием библиотеки Qt4

Делегаты (файл examples-qt/db03/db03.h)


1 #include <QtGui>
2 #include <QtSql>
3 4 class MyModel : public QSqlQueryModel { 5 Q_OBJECT 6 public: 7 MyModel(QObject *parent = 0);
8 Qt::ItemFlags flags(const QModelIndex &index) const; 9 QVariant data(const QModelIndex &index, 10 int role = Qt::DisplayRole) const; 11 bool setData(const QModelIndex &index, 12 const QVariant &value, int role);
13 private: 14 void refresh();
15 }; 16 17 //----------------------------------------------- 18 class MyView : public QTableView { 19 Q_OBJECT 20 public: 21 MyView(QWidget *parent = 0);
22 private: 23 virtual void resizeEvent(QResizeEvent *event);
24 }; 25 26 //----------------------------------------------- 27 class MyDSBDelegate : public QItemDelegate { 28 Q_OBJECT 29 public: 30 MyDSBDelegate(double min=0.00, 31 double max=999999999.99, 32 double step=0.1, 33 int precision=2, 34 QObject *parent = 0);
35 QWidget *createEditor( 36 QWidget *parent, 37 const QStyleOptionViewItem &option, 38 const QModelIndex &index) const; 39 void setEditorData(QWidget *editor, 40 const QModelIndex &index) const; 41 void setModelData(QWidget *editor, 42 QAbstractItemModel *model, 43 const QModelIndex &index) const; 44 void updateEditorGeometry( 45 QWidget *editor, 46 const QStyleOptionViewItem &option, 47 const QModelIndex &index) const; 48 private: 49 double m_min; 50 double m_max; 51 double m_step; 52 int m_precision; 53 }; 54 55 //--------------------------------------------- 56 class MyDEDelegate : public QItemDelegate { 57 Q_OBJECT 58 public: 59 MyDEDelegate(bool calpopup = true, 60 QObject *parent = 0);
61 QWidget *createEditor( 62 QWidget *parent, 63 const QStyleOptionViewItem &option, 64 const QModelIndex &index) const; 65 void setEditorData(QWidget *editor, 66 const QModelIndex &index) const; 67 void setModelData(QWidget *editor, 68 QAbstractItemModel *model, 69 const QModelIndex &index) const; 70 void updateEditorGeometry( 71 QWidget *editor, 72 const QStyleOptionViewItem &option, 73 const QModelIndex &index) const; 74 private: 75 bool m_calpopup; 76 };

  • (4-15) Модель таблицы.
  • (18-24) Представление таблицы.
  • (27-53) Класс-делегат для вещественных чисел (на основе элемента QDoubleSpinBox).
  • (56-76) Класс-делегат для ввода даты (на основе элемента QDateEdit).


  • Содержание раздела