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. Scaling pixmap images in qscrollarea
QtWS25 Last Chance

Scaling pixmap images in qscrollarea

Scheduled Pinned Locked Moved Unsolved General and Desktop
qscrollareaqpixmapqlabel
4 Posts 3 Posters 1.0k Views
  • 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.
  • R Offline
    R Offline
    rjmoses
    wrote on 5 Aug 2019, 15:12 last edited by
    #1

    After many trials and tribulations and a lot of reading, I find myself in need of assistance from gurus who have dealt with images

    I am working on an application where I want to have an area that will display a variable number of images. The user will then select an image and drag it to a location on a simulated display page. The idea is to be able to position a variety of images at specific spots on multiple screen pages.

    I am looking to have two or more columns of images, appropriately scaled, in a scroll area. The entire application area is resizable, therefore, as space changes in the scroll area, the columns of images may grow or shrink as applicable.

    I have tried many, many different test runs working with QLabel and QPixmaps but have not found a way get them to work together nicely. Part of the issue is that I do not understand sizePolicy, QViewports, QSrollarea, sizeHint, etc., relationships.

    Any help in understanding how to have scalable images displayed in a scroll area will be greatly appreciated.

    Screen layout: Left size is scrollarea for images, right side is composing area.

    0_1565017170297_QTCreatorScalableImages.jpg

    Current Test Code:

    void MainWindow::loadImages()
    {

    #define NCOLS 2
    #define MAX_WIDTH 100

    QDir dir = QDir(imageLibrary);
    QStringList allImageFiles = dir.entryList(QStringList("*.jpg"));
    
    ui->currentLibrary->setText(imageLibrary);
    ui->currentLibrary->setMaximumWidth(MAX_WIDTH);
    
    ui->scrollArea->setGeometry(0,0,200,200);
    QRect geo_availImagesGeometry = ui->scrollArea->geometry();
    
    int width = geo_availImagesGeometry.width();   
    int height = geo_availImagesGeometry.height();
    
    QWidget * viewport = new QWidget;
    ui->scrollArea->setWidget(viewport);
    ui->scrollArea->setWidgetResizable(true);
    
    QGridLayout * gl = new QGridLayout(viewport);
    
    QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    
    for (int i = 0; i < allImageFiles.size(); i++) {
        QString thisfile = imageLibrary + "/" + allImageFiles[i];
    
        QPixmap pixmap(thisfile);
        QLabel * thisImage = new QLabel();
    
        //thisImage->setScaledContents(true); 	// Tried with and without this statement
        //pixmap.scaledToWidth(width);		// Tried with and without this statement
    
        thisImage->setSizePolicy(policy);
        thisImage->setPixmap(pixmap.scaled(width , width, Qt::KeepAspectRatio));
    
        thisImage->setToolTip(allImageFiles[i]);
        
    //thisImage->setText(allImageFiles[i]);  // ????? Using this causes a segmentation violation
    
        gl->addWidget(thisImage, i/NCOLS, i % NCOLS);	// Add at row/column in scrollarea
    
    }
    
    QRect scrollGeom = viewport->childrenRect();		// Check what
    ui->currentLibrary->setMaximumWidth(scrollGeom.width());	// Set width of library name to width of scroll area
    
    QList<QLabel *> vplist = viewport->findChildren<QLabel *>();	// Debug -- See what the viewport has to say about sizes
    
    ui->scrollArea->show();
    

    }

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 Aug 2019, 20:25 last edited by
      #2

      Hi,

      What about using a QListView in icon mode with two columns ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R Offline
        R Offline
        rjmoses
        wrote on 5 Aug 2019, 23:23 last edited by
        #3

        @sgaist

        Interesting idea. Will it scale pixmaps?

        M 1 Reply Last reply 6 Aug 2019, 11:42
        0
        • R rjmoses
          5 Aug 2019, 23:23

          @sgaist

          Interesting idea. Will it scale pixmaps?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 6 Aug 2019, 11:42 last edited by
          #4

          @rjmoses
          Yes it will scale all images to match IconSize.

          1 Reply Last reply
          1

          1/4

          5 Aug 2019, 15:12

          • Login

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