3

I am trying to make QTableView/QStandardItemModel with arbitrary sized qIcons. In the MWE below, I have successfully changed the height of a row using a delegate. I can't figure out how to make it use a larger icon size in the larger row. Any help appreciated. Note that all rows can be the same height as long as I can set that height. For example, how do I make the icons 50x50 in the example below.

#include <QtGui>

#include "test.h"
//////////////////////////////////////////////////////////////////////////
// TEST.H file:
//#include <QtGui>
//
//class MainWindow : public QMainWindow {
//  Q_OBJECT
//
//  public:
//  MainWindow(QWidget *parent = 0);
//};

class ItemDelegate : public QItemDelegate {
public:
  ItemDelegate() {}
  QSize sizeHint ( const QStyleOptionViewItem &, const QModelIndex & ) const {
return QSize(50,50); 
  }
};

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {

  QImage iconImage(100, 100, QImage::Format_RGB32);
  iconImage.fill(0xF08080);

  const int ARRAY_HEIGHT = 2;
  const int ARRAY_WIDTH  = 2;

  QStandardItemModel *model = new QStandardItemModel( ARRAY_HEIGHT, ARRAY_WIDTH );

  model->setHeaderData( 1, Qt::Horizontal, QString("Icon") );
  model->setHeaderData( 2, Qt::Horizontal, QString("Text") );

  for (int yy=0; yy < ARRAY_HEIGHT; yy++ ) {
QStandardItem *newIconItem = new QStandardItem;
newIconItem->setIcon( QPixmap::fromImage( iconImage ) );
model->setItem( yy, 0, newIconItem );

QStandardItem *newTypeItem = new QStandardItem( QString("Foo") );
model->setItem( yy, 1, newTypeItem );
  }

  QTableView *table = new QTableView;

  ItemDelegate *delegate = new ItemDelegate();

  table->setModel(model);
  table->setItemDelegate(delegate);

  table->setSelectionMode(QAbstractItemView::SingleSelection);
  table->setSelectionBehavior(QAbstractItemView::SelectRows);
  table->verticalHeader()->setVisible(false);
  table->verticalScrollBar()->setVisible(false);

  table->resizeRowsToContents();
  table->resizeColumnsToContents();

  QHeaderView *headerView = table->horizontalHeader();
  headerView->setStretchLastSection(true);

  setCentralWidget( table );
}

int main( int argc, char *argv[] ) {

  QApplication app(argc, argv);
  MainWindow win;

  win.show();

  return app.exec();
}
user2064766
  • 31
  • 1
  • 2
  • 1
    I've been continuing to look at this. The way to deal with this is to not use "setIcon", which converts the QPixmap into an icon. Instead, calling setData( QPixmap::fromImage( iconImage ), Qt::DecorationRole ) does the trick. I confess that while I've worked with Qt for a while, I do not yet have my head wrapped around all of the uses of StandardItems, StandardItemModels, and QVariants. Also not that if I were writing this from scratch, I would look at QTableWidgets (see "Cannot put an image in a table" for example) – user2064766 Jul 12 '13 at 16:33

1 Answers1

1

try

ListView->setIconSize(QSize(100,100));

김병현
  • 31
  • 2
  • Try to avoid comment in answer unless you have specific detail answer. Please use comment box below and Once you have sufficient reputation you will be able to comment on any post. :) – Rucha Bhatt Joshi Jun 05 '19 at 09:03