Results 1 to 5 of 5

Thread: Icon on right side of a call in qtableview filled by model

  1. #1
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Question Icon on right side of a call in qtableview filled by model

    I have a text and an icon.
    I want to display the text in the left side of the cell and icon to the extreme right of the cell.
    How can I do this?

    The code I have included is as follows
    Qt Code:
    1. model = new ItemModel(5, 3, this);
    2. QPixmap arrowIcon(":/arrowBig.png");
    3. int i=0;
    4. for (QStringList::iterator it= rowName.begin(); it != rowName.end(); ++it)
    5. {
    6. QModelIndex index = model->index(i++,columnNameNum,QModelIndex());
    7. QString current = *it;
    8. qDebug()<<current;
    9.  
    10. model->setData(index,arrowIcon,Qt::DecorationRole);
    11.  
    12. model->setData(index,current);
    13.  
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class ItemModel : public QStandardItemModel
    2. {
    3. public:
    4. ItemModel(int rows, int columns, QObject *parent = 0)
    5. :
    6. QStandardItemModel(rows, columns, parent)
    7. {}
    8.  
    9. QVariant data(const QModelIndex &index, int role) const
    10. {
    11. if (role == Qt::TextAlignmentRole) {
    12. return Qt::AlignLeft; // <- Make alignment look different, i.e.
    13. // <- text at the bottom.
    14. }
    15. else {
    16. return QStandardItemModel::data(index, role);
    17. }
    18. }
    19. };
    To copy to clipboard, switch view to plain text mode 

    thanks in advance
    Last edited by anda_skoa; 6th February 2015 at 12:15. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Icon on right side of a call in qtableview filled by model

    You will most likely need your own delegate, e.g. a subclass of QStyledItemDelegate, that positions text and icon the way you want instead of the standard implementation's way.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Icon on right side of a call in qtableview filled by model

    Thanks.
    I am a newbie to QT. Could you provide a sample to implement QStyledItemDelegate

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Icon on right side of a call in qtableview filled by model

    Quote Originally Posted by ejoshva View Post
    Thanks.
    I am a newbie to QT. Could you provide a sample to implement QStyledItemDelegate
    The documentation of QStyledItemDelegate points to an official example: http://doc.qt.io/qt-5/qtwidgets-item...e-example.html

    Cheers,
    _

  5. #5
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Icon on right side of a call in qtableview filled by model

    I have tried and implemented but still I find only the text being displayed and arrow is not displayed.
    In Main , adding to tableview as below
    Qt Code:
    1. cellDelegate *cDO = new cellDelegate();
    2. tableView1->setItemDelegate(cDO);
    To copy to clipboard, switch view to plain text mode 

    Implementation of subclass as follows.
    Qt Code:
    1. void cellDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. painter->save();
    4. QStandardItemModel columnName1 ;
    5.  
    6. int mTextLeftOffset = 10,mIconSize=30,mIconTopOffset=20,mTextTopOffset=0;
    7. // QIcon icon(":/arrowBig.png");
    8. QFont font = QApplication::font();
    9. font.setPointSize(10);
    10. QFontMetrics fm(font);
    11. QString text = "";
    12. text=qvariant_cast<QString>(index.data(Qt::DisplayRole));
    13. QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
    14. QRect textRect = option.rect;
    15. QRect iconRect = option.rect;
    16. int displayables = 0;
    17. if (!icon.isNull())
    18. icon.paint(painter, QRect(0, 0, 16, 16), Qt::AlignVCenter|Qt::AlignRight);
    19. QSize iconsize = icon.actualSize(option.decorationSize);
    20. iconRect.setTop (iconRect.top() + mIconTopOffset); //device-dependent?
    21. iconRect.setBottom (iconRect.top() + mIconSize);
    22. iconRect.setRight (iconRect.left() + iconsize.width());
    23. textRect.setLeft (iconRect.right() + (iconsize.width()/4) * 3);
    24. textRect.setRight (textRect.left() + fm.width(text));
    25. textRect.setTop (textRect.top() + ((option.rect.height() - fm.height()) / 2) + mTextTopOffset); //device-dependent?
    26. if (!icon.isNull())
    27. //painter->drawPixmap(iconRect.center(),icon.pixmap(mIconSize, mIconSize));
    28. painter->drawPixmap(QPoint(iconRect.right()+iconsize.width()/2+2,iconRect.top()+iconsize.height()/2+3),icon.pixmap(iconsize.width(),iconsize.height()));
    29. if (displayables == 0 || displayables == 2 || !(icon.isNull()) )
    30. {
    31. qDebug()<<"at 109";
    32. painter->setFont(font);
    33. painter->drawText(textRect.left(),textRect.top()+10,text);
    34. }
    35.  
    36. painter->restore();
    37. }
    To copy to clipboard, switch view to plain text mode 

    Thanx in advance
    Last edited by anda_skoa; 6th February 2015 at 12:16. Reason: missing [code] tags

Similar Threads

  1. How to call a static method of java from c++ side?
    By stereoMatching in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 31st December 2013, 15:54
  2. QTreeWidget Right side arrow icon
    By karankumar1609 in forum Qt Programming
    Replies: 1
    Last Post: 7th May 2013, 00:32
  3. Event handlers for QTableView model / selection model.
    By hickscorp in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2011, 17:57
  4. Replies: 2
    Last Post: 24th July 2009, 22:09
  5. Replies: 5
    Last Post: 15th February 2008, 02:54

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.