Advertisement
VRonin

TemplateWidgetDelegate

Oct 24th, 2017
3,746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QStyledItemDelegate>
  2. #include <QPainter>
  3. template <class T>
  4. class WidgetDelegate : public QStyledItemDelegate{
  5. #ifdef Q_COMPILER_STATIC_ASSERT
  6.     static_assert(std::is_base_of<QWidget,T>::value,"Template argument must be a QWidget");
  7. #endif
  8.     Q_DISABLE_COPY(WidgetDelegate)
  9. public:
  10.     explicit WidgetDelegate(QObject* parent = Q_NULLPTR)
  11.         :QStyledItemDelegate(parent)
  12.         , m_baseWid(new T)
  13.     {}
  14.     ~WidgetDelegate(){
  15.         delete m_baseWid;
  16.     }
  17.     void paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const Q_DECL_OVERRIDE{
  18.         setSubEditorData(m_baseWid,index);
  19.         m_baseWid->resize(option.rect.size());
  20.         QPixmap pixmap(option.rect.size());
  21.         m_baseWid->render(&pixmap);
  22.         painter->drawPixmap(option.rect,pixmap);
  23.     }
  24.     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE{
  25.         Q_UNUSED(option);
  26.         setSubEditorData(m_baseWid,index);
  27.         return m_baseWid->sizeHint();
  28.     }
  29.     QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE Q_DECL_FINAL {
  30.         Q_UNUSED(option);
  31.         Q_UNUSED(index);
  32.         T* editor = new T;
  33.         editor->setParent(parent);
  34.         return editor;
  35.     }
  36.     virtual void setSubEditorData(T* editor, const QModelIndex &index) const =0;
  37.     void setEditorData(QWidget* editor, const QModelIndex &index) const Q_DECL_OVERRIDE Q_DECL_FINAL {
  38.         T* subEdit = qobject_cast<T*>(editor);
  39.         Q_ASSERT(subEdit);
  40.         return setSubEditorData(subEdit,index);
  41.     }
  42. }
  43. private:
  44.     T* m_baseWid;
  45. };
  46.  
  47. // An example usage would be:
  48.  
  49. class LabelDelegate : public WidgetDelegate<QLabel>{
  50.     Q_OBJECT
  51.     Q_DISABLE_COPY(LabelDelegate)
  52. public:
  53.     explicit LabelDelegate(QObject* parent=Q_NULLPTR)
  54.         :WidgetDelegate<QLabel>(parent)
  55.     {}
  56.     void setSubEditorData(QLabel* editor, const QModelIndex &index) const Q_DECL_OVERRIDE{
  57.         editor->setText("Content: <b>" + displayText(index.data(), editor->locale()) + "</b>");
  58.     }
  59. };
  60.  
  61. int main(int argc, char *argv[])
  62. {
  63.  
  64.     QApplication app(argc,argv);
  65.     QListWidget w;
  66.     w.model()->insertColumns(0,2);
  67.     w.model()->insertRows(0,2);
  68.     w.model()->setData(w.model()->index(0,0),"0,0");
  69.     w.model()->setData(w.model()->index(1,0),"1,0");
  70.     LabelDelegate* tempDelegate = new LabelDelegate(&w);
  71.     w.setItemDelegate(tempDelegate);
  72.     w.show();
  73.     return app.exec();
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement