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. Resizing image to display in TableView

Resizing image to display in TableView

Scheduled Pinned Locked Moved Solved General and Desktop
image displayresizetableview
4 Posts 3 Posters 2.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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I'm trying to resize an image pulled from a database to be able to display it in TableView.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QSqlDatabase db;
            db = QSqlDatabase::addDatabase ("QSQLITE");
            db.setDatabaseName ("C:/Programming/Qtsamples/Image_from_db_to_Table/db.db");
    
            if(!db.open ())
            {
                qDebug() << "The database is NOT open!";
            }
            else
            {
                qDebug() << "The database is open!";
            }
    
            QSqlQuery query("SELECT ID,Pic FROM Items");
    
            if(query.isActive()==true)
              {
                  qDebug() << "The query is active.";
              }
              else
              {
                 qDebug() << "The query is NOT active.";
              } 
    
            query.first ();
           	int ID;
    
            QStandardItemModel *smodel = new QStandardItemModel;
            QStandardItem *Item = new QStandardItem();
            QStandardItem *Item2 = new QStandardItem();
    
            Item->setData (QVariant(ID = query.value (0).toInt ()));
            qDebug() << "ID = " << Item->data ().toInt ();
            smodel->setItem (0,0,Item);
    
            QByteArray ByteArray;
            ByteArray = query.value (1).toByteArray ();
         	QPixmap Pixmap;
    
    		Pixmap.loadFromData (ByteArray);
    
    		Pixmap.scaled (100,100,Qt::KeepAspectRatio);
    
            Item2->setData (QVariant(Pixmap),Qt::DecorationRole);
    
    		smodel->setItem (0,1,Item2);
            ui->tableView->verticalHeader ()->setDefaultSectionSize (100);
            ui->tableView->horizontalHeader ()->setDefaultSectionSize (100);
    
            ui->tableView->setModel (smodel);
    
    db.close ();
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    It displays the image but without resizing. What can I do differently to be able to see the whole image?
    Thank you.

    1 Reply Last reply
    0
    • RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by
      #2

      May be you can try this

       Pixmap.scaled (100,100 Qt::IgnoreAspectRatio, Qt::FastTransformation));

      --Alles ist gut.

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        The scaled function returns the modified version
        not changing the original one.
        so
        Pixmap.scaled (100,100,Qt::KeepAspectRatio);
        should be
        Pixmap=Pixmap.scaled (100,100,Qt::KeepAspectRatio);
        or
        QPixmap myscaled=Pixmap.scaled (100,100,Qt::KeepAspectRatio);
        If you want to use the scaled version later on.

        http://doc.qt.io/qt-5.5/qpixmap.html#scaled

        1 Reply Last reply
        1
        • G Offline
          G Offline
          gabor53
          wrote on last edited by
          #4

          Thank you. It worked.

          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