Results 1 to 5 of 5

Thread: about QAbstractItemDelegate

  1. #1

    Default about QAbstractItemDelegate

    I implement QAbstractItemDelegate's Paint and sizeHint,But data can't display.follow is code ,does anybody help me?
    PluginDelegate.cpp
    Qt Code:
    1. #include"PluginDelegate.h"
    2. #include <QPainter>
    3.  
    4. void PluginDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    5. {
    6. if(option.state & QStyle::State_Selected)
    7. {
    8. painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
    9. }
    10. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    11. QString txt;
    12. QString txt1 = "shit";
    13. for(int i=0;i<strlist.count();i++)
    14. {
    15. txt = strlist.at(i++).toAscii();
    16. txt1 = strlist.at(i++).toAscii();
    17. }
    18.  
    19. QRect r = option.rect.adjusted(2, 2, -2, -2);
    20. ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
    21. r = r.adjusted(r.height()+20, 0, 0, 0);
    22. if(txt!=""&txt1!="")
    23. {
    24. painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft|Qt::TextWordWrap, txt, &r);
    25. r = r.adjusted(0, 0, 0, 0);
    26. painter->drawText(r.left()+60, r.top(), r.width()+20, r.height(), Qt::AlignTop|Qt::AlignLeft|Qt::TextWordWrap, txt1, &r);
    27. }
    28. }
    29. QSize PluginDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
    30. {
    31. return QSize(200, 52);
    32. }
    To copy to clipboard, switch view to plain text mode 

    PluginDelegate.h
    Qt Code:
    1. class PluginDelegate : public QAbstractItemDelegate
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. PluginDelegate(QObject *parent=0) : QAbstractItemDelegate(parent){};
    7.  
    8. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
    9. const QModelIndex &index) const;
    10.  
    11. virtual QSize sizeHint(const QStyleOptionViewItem &option,
    12. const QModelIndex &index) const;
    13.  
    14. };
    To copy to clipboard, switch view to plain text mode 

    showhistory.cpp
    Qt Code:
    1. showhistory::showhistory(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ui.setupUi(this);
    5. QWidget* window = new QWidget;
    6. window->setWindowTitle("History");
    7.  
    8. lv.setAutoFillBackground(true);
    9. model.setRowCount(8);
    10. model.setColumnCount(1);
    11.  
    12. QTextStream floStream(&file);
    13. QTextCodec *codec=QTextCodec::codecForName("GBK");
    14. floStream.setCodec(codec);
    15. int linenum=1;
    16. line = codec->fromUnicode(floStream.readLine());
    17. model.setData(model.index(count, 0), QPixmap(":Image/butterfly.png"), Qt::DecorationRole);
    18. model.setData(model.index(count++, 0), line);
    19. linenum++;
    20. QPainter painter(&lv);
    21. painter.setPen(Qt::blue);
    22. painter.drawLine(0,linenum*(140) ,230,linenum*(140) );
    23. }
    24. file.close();
    25. }
    26.  
    27. lv.setModel(&model);
    28. [COLOR="red"]lv.setItemDelegate(new PluginDelegate(&lv));[/COLOR]
    29. lv.setAlternatingRowColors(true);
    30.  
    31. QHBoxLayout *layout = new QHBoxLayout;
    32. layout->addWidget(&lv);
    33. window->setLayout(layout);
    34. window->showMaximized();
    35. };
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. showhistory w;
    5. w.show();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    but when i implement it follow code in main.cpp,everything is ok,could anyone tell me why?
    Qt Code:
    1. class PluginDelegate : public QAbstractItemDelegate {
    2. public:
    3. PluginDelegate(QObject *parent=0) : QAbstractItemDelegate(parent){}
    4. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    5. if(option.state & QStyle::State_Selected){
    6. painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
    7. }
    8. QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    9. QString str = index.data(Qt::DisplayRole).toString();
    10. QStringList strlist = str.split(";");
    11. QString txt;
    12. QString txt1 = "shit";
    13. for(int i=0;i<strlist.count();i++)
    14. {
    15. txt = strlist.at(i++).toAscii();
    16. txt1 = strlist.at(i++).toAscii();
    17. }
    18.  
    19. QRect r = option.rect.adjusted(2, 2, -2, -2);
    20. ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
    21. r = r.adjusted(r.height()+20, 0, 0, 0);
    22. if(txt!=""&txt1!="")
    23. {
    24. painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft|Qt::TextWordWrap, txt, &r);
    25. r = r.adjusted(0, 0, 0, 0);
    26. painter->drawText(r.left()+60, r.top(), r.width()+20, r.height(), Qt::AlignTop|Qt::AlignLeft|Qt::TextWordWrap, txt1, &r);
    27. }
    28. }
    29. QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    30. return QSize(200, 52); // very dumb value
    31. }
    32.  
    33. };
    34. int main(int argc, char *argv[])
    35. {
    36. QApplication a(argc, argv);
    37. QWidget* window = new QWidget;
    38. window->setWindowTitle("History");
    39.  
    40. lv.setAutoFillBackground(true);
    41. model.setRowCount(8);
    42. model.setColumnCount(1);
    43.  
    44. model.setData(model.index(count, 0), QPixmap(":/butterfly.png"), Qt::DecorationRole);
    45. model.setData(model.index(count++, 0), line);
    46. linenum++;
    47. QPainter painter(&lv);
    48. painter.setPen(Qt::blue);
    49. painter.drawLine(0,linenum*(140) ,230,linenum*(140) );
    50. }
    51. file.close();
    52. }
    53.  
    54. lv.setModel(&model);
    55. [COLOR="red"] lv.setItemDelegate(new PluginDelegate(&lv));[/COLOR]
    56. lv.setAlternatingRowColors(true);
    57.  
    58. QHBoxLayout *layout = new QHBoxLayout;
    59. layout->addWidget(&lv);
    60. window->setLayout(layout);
    61. window->showMaximized();
    62. return a.exec();
    63. }
    To copy to clipboard, switch view to plain text mode 
    the code is same,but different result?I just want to seperate it from main.cpp
    Last edited by drizzlefsh; 10th September 2009 at 10:18.

  2. #2

    Default Re: about QAbstractItemDelegate

    And Paint never be called when I set up a new cpp file for PluginDelegate,why,how i can call paint function

  3. #3
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: about QAbstractItemDelegate

    Do not create the model, the view and the delegate on the stack! Create them in the heap. Otherwise they will be destroyed after the constructor finishes its execution.

    Qt Code:
    1. QListView *lv = new QListView(this);
    2. lv->setAutoFillBackground(true);
    3. model->setRowCount(8);
    4. model->setColumnCount(1);
    5. ...
    6. lv->setModel(model);
    7. lv->setItemDelegate(new PluginDelegate(lv));
    To copy to clipboard, switch view to plain text mode 

  4. #4

    Default Re: about QAbstractItemDelegate

    HI,maybe you are right,but problem still exist.problem occur not because of this stack.in my opinion.

  5. #5

    Default Re: about QAbstractItemDelegate

    thanks for your help,problem has been resolve.i define them in .h file.things is expected.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.