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. Showing pictures in a tableView by reading from a database?
Forum Updated to NodeBB v4.3 + New Features

Showing pictures in a tableView by reading from a database?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 289 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
    RootLearner
    wrote on last edited by
    #1

    I have a tableView whose code is:

    qry.prepare("select * from movie ");
        if (qry.exec())
        {
           model->setQuery(qry);
           ui->tableView->setModel(model);
         }
    

    But I have pictures in this database. How do I make sure the pictures get shown in the table in the ui?
    The pictures are stored in BLOB formate using QbyteArray. There are 2 columns having pictures in them. But the ui shows garbage value in the column where image should be present. How do I solve this?

    eyllanescE 2 Replies Last reply
    0
    • R RootLearner

      I have the following code to store my image in the db:

      
                  QPixmap *inPixmap = new QPixmap(filename);
                  QByteArray inByteArray;
                  QBuffer inBuffer(&inByteArray);
                  inBuffer.open(QIODevice::WriteOnly);
                  inPixmap->save(&inBuffer, "PNG");
                  QSqlQuery query = QSqlQuery(db)
      

      Filename has the path of the images. This is how I have added the image into the database. How do I use your code? I have shown the code I use to display the model in tableView. How do I add your code into my project? Can you guide me a little? I am very confused.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #5

      @RootLearner Oops, I forgot to point out that you have to add:

      ui->tableView->setItemDelegateForColumn(column1, new ImageDelegate);
      ui->tableView->setItemDelegateForColumn(column2, new ImageDelegate);
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      1
      • R RootLearner

        I have a tableView whose code is:

        qry.prepare("select * from movie ");
            if (qry.exec())
            {
               model->setQuery(qry);
               ui->tableView->setModel(model);
             }
        

        But I have pictures in this database. How do I make sure the pictures get shown in the table in the ui?
        The pictures are stored in BLOB formate using QbyteArray. There are 2 columns having pictures in them. But the ui shows garbage value in the column where image should be present. How do I solve this?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #2

        @RootLearner Are the images stored in raw or base64 format?

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        0
        • R RootLearner

          I have a tableView whose code is:

          qry.prepare("select * from movie ");
              if (qry.exec())
              {
                 model->setQuery(qry);
                 ui->tableView->setModel(model);
               }
          

          But I have pictures in this database. How do I make sure the pictures get shown in the table in the ui?
          The pictures are stored in BLOB formate using QbyteArray. There are 2 columns having pictures in them. But the ui shows garbage value in the column where image should be present. How do I solve this?

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by
          #3

          @RootLearner You can use a delegate:

          class ImageDelegate: public QStyledItemDelegate{
          public:
              using QStyledItemDelegate::QStyledItemDelegate;
              void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
                  const QByteArray & bytes = index.data().toByteArray();
                  QPixmap pixmap;
                  if(!bytes.isNull() && pixmap.loadFromData(bytes)){
                      // for BASE64 change to
                      // pixmap.loadFromData(QByteArray::fromBase64(bytes))
                      painter->drawPixmap(option.rect, pixmap.scaled(option.rect.size(),
                                                                     Qt::KeepAspectRatio,
                                                                     Qt::SmoothTransformation));
                      return;
                  }
                  return QStyledItemDelegate::paint(painter, option, index);
              }
          };
          

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply
          2
          • R Offline
            R Offline
            RootLearner
            wrote on last edited by
            #4

            I have the following code to store my image in the db:

            
                        QPixmap *inPixmap = new QPixmap(filename);
                        QByteArray inByteArray;
                        QBuffer inBuffer(&inByteArray);
                        inBuffer.open(QIODevice::WriteOnly);
                        inPixmap->save(&inBuffer, "PNG");
                        QSqlQuery query = QSqlQuery(db)
            

            Filename has the path of the images. This is how I have added the image into the database. How do I use your code? I have shown the code I use to display the model in tableView. How do I add your code into my project? Can you guide me a little? I am very confused.

            eyllanescE 1 Reply Last reply
            0
            • R RootLearner

              I have the following code to store my image in the db:

              
                          QPixmap *inPixmap = new QPixmap(filename);
                          QByteArray inByteArray;
                          QBuffer inBuffer(&inByteArray);
                          inBuffer.open(QIODevice::WriteOnly);
                          inPixmap->save(&inBuffer, "PNG");
                          QSqlQuery query = QSqlQuery(db)
              

              Filename has the path of the images. This is how I have added the image into the database. How do I use your code? I have shown the code I use to display the model in tableView. How do I add your code into my project? Can you guide me a little? I am very confused.

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by eyllanesc
              #5

              @RootLearner Oops, I forgot to point out that you have to add:

              ui->tableView->setItemDelegateForColumn(column1, new ImageDelegate);
              ui->tableView->setItemDelegateForColumn(column2, new ImageDelegate);
              

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              1 Reply Last reply
              1

              • Login

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