PDA

View Full Version : Icon on right side of a call in qtableview filled by model



ejoshva
5th February 2015, 09:51
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


model = new ItemModel(5, 3, this);
QPixmap arrowIcon(":/arrowBig.png");
int i=0;
for (QStringList::iterator it= rowName.begin(); it != rowName.end(); ++it)
{
QModelIndex index = model->index(i++,columnNameNum,QModelIndex());
QString current = *it;
qDebug()<<current;

model->setData(index,arrowIcon,Qt::DecorationRole);

model->setData(index,current);


}



class ItemModel : public QStandardItemModel
{
public:
ItemModel(int rows, int columns, QObject *parent = 0)
:
QStandardItemModel(rows, columns, parent)
{}

QVariant data(const QModelIndex &index, int role) const
{
if (role == Qt::TextAlignmentRole) {
return Qt::AlignLeft; // <- Make alignment look different, i.e.
// <- text at the bottom.
}
else {
return QStandardItemModel::data(index, role);
}
}
};


thanks in advance

anda_skoa
5th February 2015, 10:32
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,
_

ejoshva
5th February 2015, 11:34
Thanks.
I am a newbie to QT. Could you provide a sample to implement QStyledItemDelegate

anda_skoa
6th February 2015, 08:57
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-itemviews-stardelegate-example.html

Cheers,
_

ejoshva
6th February 2015, 10:45
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


cellDelegate *cDO = new cellDelegate();
tableView1->setItemDelegate(cDO);


Implementation of subclass as follows.


void cellDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
painter->save();
QStandardItemModel columnName1 ;

int mTextLeftOffset = 10,mIconSize=30,mIconTopOffset=20,mTextTopOffset=0 ;
// QIcon icon(":/arrowBig.png");
QFont font = QApplication::font();
font.setPointSize(10);
QFontMetrics fm(font);
QString text = "";
text=qvariant_cast<QString>(index.data(Qt::DisplayRole));
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QRect textRect = option.rect;
QRect iconRect = option.rect;
int displayables = 0;
if (!icon.isNull())
icon.paint(painter, QRect(0, 0, 16, 16), Qt::AlignVCenter|Qt::AlignRight);
QSize iconsize = icon.actualSize(option.decorationSize);
iconRect.setTop (iconRect.top() + mIconTopOffset); //device-dependent?
iconRect.setBottom (iconRect.top() + mIconSize);
iconRect.setRight (iconRect.left() + iconsize.width());
textRect.setLeft (iconRect.right() + (iconsize.width()/4) * 3);
textRect.setRight (textRect.left() + fm.width(text));
textRect.setTop (textRect.top() + ((option.rect.height() - fm.height()) / 2) + mTextTopOffset); //device-dependent?
if (!icon.isNull())
//painter->drawPixmap(iconRect.center(),icon.pixmap(mIconSize , mIconSize));
painter->drawPixmap(QPoint(iconRect.right()+iconsize.width( )/2+2,iconRect.top()+iconsize.height()/2+3),icon.pixmap(iconsize.width(),iconsize.height( )));
if (displayables == 0 || displayables == 2 || !(icon.isNull()) )
{
qDebug()<<"at 109";
painter->setFont(font);
painter->drawText(textRect.left(),textRect.top()+10,text);
}

painter->restore();
}


Thanx in advance