Re-sizing of a icon within a QTableWidgetItem
-
Hi all,
The image included show the current QTableWidget i'm populating, as you can see the icons on the left appear as small rectangles, and ideally I would like them to will the entire contents of that QTableWidgetItem.
These icons are created via a QPainter drawing on a pixel map then converting that pixel map into an icon and setting the QTableWidgetItem to use that icon.I did notice a setIconSize function for a QTableWidget but giving it a different size seems to have no effect on the size of the icon.
@ QPixmap pixmapitem (50,50);
pixmapitem.fill(QColor(Qt::white));
QPainter p(&pixmapitem);
p.setRenderHint(QPainter::Antialiasing,true);
QLinearGradient grad1(0,0,50,50);
int red = colourList[i].red();
int blue = colourList[i].blue();
int green = colourList[i].green();grad1.setColorAt(1,QColor(red,green,blue,50));
grad1.setColorAt(0,QColor(red,green,blue,255));QRect rec (0,0,50,50);
p.fillRect(rec,grad1);
QIcon tempicon;
tempicon.addPixmap(pixmapitem,QIcon::Normal,QIcon::On);
colourItem->setIcon(tempicon);@ -
Rather than scaling the icon / pixmap, you can just create your QPixmap pixmapitem with the size of the QTableWidgetItem rather than 50, 50
@
QPixmap pixmapitem(colourItem->sizeHint());
pixmapitem.fill(QColor(Qt::white));
QPainter p(&pixmapitem);
p.setRenderHint(QPainter::Antialiasing,true);
QLinearGradient grad1(0,0,colourItem->sizeHint().width(),colourItem->sizeHint().height());
int red = colourList[i].red();
int blue = colourList[i].blue();
int green = colourList[i].green();grad1.setColorAt(1,QColor(red,green,blue,50));
grad1.setColorAt(0,QColor(red,green,blue,255));QRect rec (QPoint(0,0), colourItem->sizeHint());
p.fillRect(rec,grad1);
QIcon tempicon;
tempicon.addPixmap(pixmapitem,QIcon::Normal,QIcon::On);
colourItem->setIcon(tempicon);
@As an alternative you can also scale your pixmap before adding it to the icon to get the icon at the correct size. "scaled":http://qt-project.org/doc/qt-4.8/qpixmap.html#scaled
-
Hi,
If we add a pixmap to the QTableWidgetItem the cell of the TableWidget displays the icon as well as the text. In the above case the text value is empty. In order to check u can double click your icon cell and see that you can edit the text.
For the above requirement one approach can be to create a delegate and override the paint() method then set the delegate to the TableWidget
Eg:
.h
@class TableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit TableDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;};@
.cpp
@void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{//here you can write the logic for painting each row with different color gradients.
if(index.column()==1)
painter->fillRect(option.rect,Qt::red); //painting the whole cell if column count ==1}@
then in the constructor of your class you can give:
@delegate = new TableDelegate();
ui->tableWidget->setItemDelegate(delegate);@You can also make the color column as non-editable.
Regards
Soumitra