class ImageLabel2 : public QWidget{ Q_OBJECTpublic: int startHeigth; int startWidth; explicit ImageLabel2(QWidget *parent = 0); const QPixmap* pixmap() const;public slots: void setPixmap(const QPixmap&);protected: void paintEvent(QPaintEvent *);private: QPixmap pix;};
ImageLabel2::ImageLabel2(QWidget *parent) : QWidget(parent){}void ImageLabel2::paintEvent(QPaintEvent *event) { QWidget::paintEvent(event); if (pix.isNull()) return; QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); QSize pixSize = pix.size(); pixSize.scale(this->size(), Qt::KeepAspectRatio);//event->rect().size() QPixmap scaledPix = pix.scaled(pixSize, Qt::KeepAspectRatio, Qt::SmoothTransformation ); painter.drawPixmap(QPoint(), scaledPix); qDebug() << pixSize.width() << " " << scaledPix.width() << " " << this->width();}const QPixmap* ImageLabel2::pixmap() const { return &pix;}void ImageLabel2::setPixmap (const QPixmap &pixmap){ pix = pixmap; startHeigth = pixmap.height(); startWidth = pixmap.width();}
QVBoxLayout *scrollWidgetLayout = new QVBoxLayout(ui->scrollArea); QFrame* frame = new QFrame(ui->scrollArea); frame->setLayout(scrollWidgetLayout); ImageLabel2 *label = new ImageLabel2(ui->scrollAreaWidgetContents); //label->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum); label->setPixmap(QPixmap::fromImage(QImage("1.jpg"))); scrollWidgetLayout->addWidget(label); ImageLabel2 *label2 = new ImageLabel2(ui->scrollAreaWidgetContents); //label->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum); label2->setPixmap(QPixmap::fromImage(QImage("1.jpg"))); scrollWidgetLayout->addWidget(label2); ImageLabel2 *label3 = new ImageLabel2(ui->scrollAreaWidgetContents); //label->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum); label3->setPixmap(QPixmap::fromImage(QImage("1.jpg"))); scrollWidgetLayout->addWidget(label3); ui->scrollArea->setWidget(frame);
C++ (Qt)virtual void QStyledItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
C++ (Qt)virtual QSize QStyledItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
class MyDelegate : public QStyledItemDelegate { public: MyDelegate(QObject *parent=0) : QStyledItemDelegate (parent) { } void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const { if(option.state & QStyle::State_Selected){ painter->fillRect(option.rect, option.palette.color(QPalette::Highlight)); return; } QVariant v = index.data(Qt::UserRole); QImage img = v.value<QImage>(); painter->drawImage(option.rect,img); } QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const { QImage img = index.data(Qt::UserRole).value<QImage>(); int heigth = img.height(); int width = img.width(); return QSize(((QListWidget*)parent())->width(), ((QListWidget*)parent())->width()*heigth/width); }};
QImage image1("1.jpg"); QListWidgetItem *item1 = new QListWidgetItem(); item1->setData(Qt::UserRole, image1); ui->listWidget->addItem(item1); QImage image2("2.jpg"); QListWidgetItem *item2 = new QListWidgetItem(); item2->setData(Qt::UserRole, image2); ui->listWidget->addItem(item2); QImage image3("3.jpg"); QListWidgetItem *item3 = new QListWidgetItem(); item3->setData(Qt::UserRole, image3); ui->listWidget->addItem(item3); QImage image4("4.jpg"); QListWidgetItem *item4 = new QListWidgetItem(); item4->setData(Qt::UserRole, image4); ui->listWidget->addItem(item4); ui->listWidget->setItemDelegate(new MyDelegate(ui->listWidget));
C++ (Qt)items[itemsCounter]->setSizeHint(QSize(items[itemsCounter]->sizeHint().width(), newHeight)); // При условии, что они хранятся в векторе
class MyQListWidget:public QListWidget{ void resizeEvent(QResizeEvent *e) { for(int i = 0; i < this->count(); i++) { QListWidgetItem*item = this->item(i); QImage img = item->data(Qt::UserRole).value<QImage>(); item->setSizeHint(QSize(this->width(),(this->width()*img.height())/img.width())); } }};
C++ (Qt)void QAbstractItemDelegate::sizeHintChanged(const QModelIndex & index)
C++ (Qt) class MyDelegate: public QStyledItemDelegate{ Q_OBJECTpublic: MyDelegate(QAbstractItemView *view) : QStyledItemDelegate(view), m_view(view) { m_view->installEventFilter(this); } virtual bool eventFilter(QObject *watched, QEvent *event) { if (watched == m_view && event->type() == QEvent::Resize && m_view->model()) { QResizeEvent *ev = static_cast<QResizeEvent*>(event); if (ev->oldSize().width() != ev->size().width()) { QAbstractItemModel *model = m_view->model(); int count = model->rowCount(); for (int i = count; i > 0; --i) emit sizeHintChanged(model->index(i, 0)); } } return QStyledItemDelegate::eventFilter(watched, event); } virtual void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { ... } QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index) const { ... } private: QAbstractItemView *m_view;};