Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Re-sizing of a icon within a QTableWidgetItem
Forum Updated to NodeBB v4.3 + New Features

Re-sizing of a icon within a QTableWidgetItem

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suths
    wrote on last edited by
    #1

    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);@

    !http://dl.dropbox.com/u/14313753/iconcolours.png(Icon)!

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Nosf
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved