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. [SOLVED]Save and load image with database(Postgresql)
Qt 6.11 is out! See what's new in the release blog

[SOLVED]Save and load image with database(Postgresql)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 11.9k 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.
  • H Offline
    H Offline
    Hostel
    wrote on last edited by
    #1

    I have a problem with loading image from database to QLabel. My code looks like this:

    Save a image to db:
    @
    QByteArray baPhoto;
    QBuffer buffer( &baPhoto );
    buffer.open( QBuffer::WriteOnly );
    ui->lbImage->pixmap()->save( &buffer, "PNG" );

    QSqlQuery query;
    query.prepare( "UPDATE foo_table SET photo = :photo" );
    query.bindValue( ":photo", baPhoto.toBase64() ) );
    qDebug() << query.exec&#40;&#41;; // return true
    

    @

    load image from db
    @
    // _val is a QVariant which I get from QRecord from photo field(bytea)
    QPixmap* pixmap = new QPixmap();
    qDebug() << pixmap->loadFromData( _val.toByteArray().toBase64() ); // return false
    qDebug() << pixmap->loadFromData( _val.toByteArray() ); // return false

        ui->lbImage->setPixmap( *pixmap ); // empty QLabel
    

    @

    What is wrong?

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Try this:
      @QByteArray baPhoto;
      QBuffer buffer( &baPhoto );
      buffer.open( QBuffer::WriteOnly );
      ui->lbImage->pixmap()->save( &buffer, "PNG" );

      QSqlQuery query;
      query.prepare( "UPDATE foo_table SET photo = :photo" );
      query.bindValue( ":photo", baPhoto ) );
      qDebug() << query.exec(); // return true@

      without toBase64()

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        And this:
        @pixmap->loadFromData( _val.toByteArray(), "PNG" ); @

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

          If the column is of type bytea in the database, the use of a byte array as pointed out by qxoz should work.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hostel
            wrote on last edited by
            #5

            Yes - code without
            @
            toBase64()
            @
            saves pictures, but when I'm doing update I have a warning:
            @
            WARNING: nonstandard use of \ in a string literal
            LINE 1: EXECUTE qpsqlpstmt_4 ('\211PNG\015\012\032\012\000\00...
            ^
            HINT: Use the escape string syntax for backslashes, e.g., E'\'.
            @
            Can I ignore this warning?

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hostel
              wrote on last edited by
              #6

              Ok I resolve this warning. Now I'm saving picture in this way:
              @
              // like in 1st post
              QByteArray baPhoto;
              QBuffer buffer( &baPhoto );
              buffer.open( QBuffer::WriteOnly );
              ui->lbImage->pixmap()->save( &buffer, "PNG" );

              QSqlQuery query;
              query.prepare( "UPDATE foo_table SET photo = :photo" );
              query.bindValue( ":photo", baPhoto.toBase64() ) ); // without toBase64() is warning
              qDebug() << query.exec(); // return true
              @
              Load pic:
              @
              // we need convert from base64 to standard bytearray
              pixmap->loadFromData( QByteArray::fromBase64( _val.toByteArray() ) );
              @

              Thanx for help!

              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