• 5715阅读
  • 2回复

求。QItemDelegate不管用。无效啊 [复制链接]

上一主题 下一主题
离线downstairs
 

只看楼主 倒序阅读 楼主  发表于: 2011-12-01
— 本帖被 XChinux 从 Qt基础编程 移动到本区(2011-12-01) —
mydelegate.h
  1. #ifndef MYDELEGATE_H
  2. #define MYDELEGATE_H
  3. #include <QItemDelegate>
  4. class myDelegate : public QItemDelegate
  5. {
  6.     Q_OBJECT
  7. public:
  8.     myDelegate(QObject *parent =0);
  9.     ~myDelegate();
  10.     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index);\
  11.     void setEditorData(QWidget *editor, const QModelIndex &index);
  12.     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index);
  13.     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index);
  14.     
  15. };
  16. #endif // MYDELEGATE_H
mydelegate.cpp
  1. #include "StdAfx.h"
  2. #include "mydelegate.h"
  3. myDelegate::myDelegate(QObject *parent)
  4.     : QItemDelegate(parent)
  5. {
  6. }
  7. myDelegate::~myDelegate()
  8. {
  9. }
  10. QWidget * myDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index )
  11. {
  12.     return new QLabel(parent);
  13. }
  14. \
  15. void myDelegate::setEditorData( QWidget *editor, const QModelIndex &index )
  16. {
  17. int bb;
  18. }
  19. void myDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index )
  20. {
  21. int aa;
  22. }
  23. void myDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index )
  24. {
  25.     
  26.     
  27.         QString timeformat="yyyy-MM-dd";
  28.         QDateTime dtval = index.model()->data(index, Qt::DisplayRole).toDateTime();
  29.         QString textvalue = tr(" ");
  30.         if(dtval.isValid())
  31.         {
  32.             textvalue = dtval.toString(timeformat);
  33.         }
  34.         textvalue=dtval.toString(timeformat);
  35.         //QStyleOptionViewItem myOption = option;
  36.         //myOption.displayAlignment = Qt::LeftToRight | Qt::AlignVCenter;
  37.         drawDisplay(painter, option, option.rect, textvalue);
  38.         drawFocus(painter, option, option.rect);
  39.     
  40. }
调用
  1. #include "stdafx.h"
  2. #include "hhtt.h"
  3. hhtt::hhtt(QWidget *parent, Qt::WFlags flags)
  4.     : QMainWindow(parent, flags)
  5. {
  6.     
  7.     QTableWidget* tb=new QTableWidget;
  8.     myDelegate *m=new myDelegate(tb);
  9.     tb->setRowCount(12);
  10.     tb->setColumnCount(15);
  11.     tb->setItemDelegate(m);
  12.     this->setCentralWidget(tb);
  13. }
  14. hhtt::~hhtt()
  15. {
  16. }


也不报错。下断点。发现只会进入delegate的初始化。但不会进入createEditor和paint 和setModelData
喜爱编程的猫头鹰
离线downstairs

只看该作者 1楼 发表于: 2011-12-01
在 .h 里的类声明里。这样


QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;

后面要加const ..坑爹啊。。搞了1天。。。。
喜爱编程的猫头鹰
离线jacke_wang
只看该作者 2楼 发表于: 2011-12-12
.......
快速回复
限100 字节
 
上一个 下一个