Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Howto display QPixmap and Text?

  1. #1
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Howto display QPixmap and Text?

    Hello,

    I have a sql database with the following tables:
    table content
    id integer, content text
    table images
    id integer, shortname text, data blob

    The following snippet is from content.conten:
    Qt Code:
    1. This is an example text with an image :img1. The text is very senseless. Here another image :img2.
    To copy to clipboard, switch view to plain text mode 
    I have to replace the ":img(id)" keywords by the correct image.
    I generate a QPixmap of each image by QPixmap::loadFromData(const QByteArray&). To show them in a QLabel inline the text module doesn't seem possible. I see no way to create a QLabel for each text module and each image. (IMHO would it be very bad design)
    Maybe QTextBrowser could be the right GUI element but sometimes I have to replace only the images on user input. Do I use a QTextDocument than?
    What other possibilities I have to display the information to the user?

    Bye

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    QLabel is fine - you can use the <img> tag to add images to your text.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    I know that QLabel supports rich text. But I have a QPixmap object and I don't know how to insert it inline a QLabel.
    Something like the following won't work:
    Qt Code:
    1. QPixmap pixmap;
    2. pixmap.loadFromData(_data);
    3. QLabel *label;
    4. label->setText("some senseless text with an image <img src="+pixmap+"/>");
    To copy to clipboard, switch view to plain text mode 
    Sure it is possible to get the image from the database, create a QPixmap object, save it in a temporary file and include this e.g. <img src="temp.png" />. But isn't there an easier way?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    There are many ways You can try embedding the data directly in the URL although I'm not sure if Qt Rich Text supports it or you can use the resource system or a similar system based on QAbstractFileEngine.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    Embedding in the URL isn't supported by Qt's Rich Text.
    I don't want to use the resource system because i have to export the images from database into regular files.
    Is subclassing QAbstractFileEngine really necessary?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    Quote Originally Posted by segi View Post
    Is subclassing QAbstractFileEngine really necessary?
    No, you can register the data with the resource system dynamically through QResource::registerResource(). Of course you need to properly feed the data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    I read much about QResource now. That isn't what I really want.
    To subclass QAbstractFileEngine seems the best solution. But IHMO it's a lot of work and the documentation of it is not as good as of the other Qt classes.
    I have a some code which not working well:
    Qt Code:
    1. // sqlfileengine.h
    2. #ifndef SQLFILEENGINE_H
    3. #define SQLFILEENGINE_H
    4.  
    5. #include <QAbstractFileEngine>
    6. #include <QAbstractFileEngineHandler>
    7.  
    8. class QByteArray;
    9. class QString;
    10.  
    11. class SqlFileEngine;
    12.  
    13. class SqlFileEngineHandler : public QAbstractFileEngineHandler
    14. {
    15. public:
    16. SqlFileEngineHandler();
    17. virtual QAbstractFileEngine* create(const QString& filename) const;
    18. };
    19.  
    20. class SqlFileEngine : public QAbstractFileEngine
    21. {
    22. public:
    23. SqlFileEngine();
    24. explicit SqlFileEngine(const QString& file);
    25.  
    26. virtual QString fileName(FileName file=DefaultName) const;
    27. virtual void setFileName(const QString& file);
    28.  
    29. bool open(QIODevice::OpenMode openMode);
    30. qint64 read(char* data, qint64 maxlen);
    31. bool atEnd () const;
    32.  
    33. qint64 size() const;
    34. qint64 pos() const;
    35.  
    36. private:
    37. void init();
    38.  
    39. QString _filename;
    40. QByteArray _data;
    41. qint64 _pos;
    42. };
    43.  
    44. #endif /* end of SQLFILEENGINE_H */
    45.  
    46. // sqlfileengine.cpp
    47. #include "sqlfileengine.h"
    48.  
    49. #include <QByteArray>
    50. #include <QDebug>
    51. #include <QString>
    52. #include <QStringList>
    53. #include <QSqlQuery>
    54. #include <QVariant>
    55.  
    56. // SqlFileEngineHandler
    57. SqlFileEngineHandler::SqlFileEngineHandler()
    58. {
    59. }
    60.  
    61. QAbstractFileEngine* SqlFileEngineHandler::create(const QString& filename) const
    62. {
    63. return filename.toLower().startsWith("sql:") ? new SqlFileEngine(filename) : 0;
    64. }
    65.  
    66. // SqlFileEngine
    67. SqlFileEngine::SqlFileEngine()
    68. {
    69. }
    70.  
    71. SqlFileEngine::SqlFileEngine(const QString& file)
    72. : _pos(0)
    73. {
    74. qDebug() << "SqlFileEngine ctor";
    75. setFileName(file);
    76. init();
    77. }
    78.  
    79. QString SqlFileEngine::fileName(FileName file) const
    80. {
    81. Q_UNUSED(file);
    82. return _filename;
    83. }
    84.  
    85. void SqlFileEngine::setFileName(const QString& file)
    86. {
    87. _filename=file.section(':', 1);
    88. qDebug() << "SqlFileEngine::setFileName() " << _filename;
    89. }
    90.  
    91. bool SqlFileEngine::open(QIODevice::OpenMode openMode)
    92. {
    93. Q_UNUSED(openMode);
    94. qDebug() << "SqlFileEngine::open() ";
    95. return true;
    96. }
    97.  
    98. qint64 SqlFileEngine::read(char* data, qint64 maxlen)
    99. {
    100. qDebug() << "SqlFileEngine::read(" << data << "," << maxlen << ")";
    101.  
    102. if(atEnd())
    103. return 0;
    104.  
    105. if(!_data.isEmpty())
    106. {
    107. QByteArray curData=_data.mid(_pos, _pos+maxlen);
    108. qstrcpy(data, curData.data());
    109. qDebug() << data;
    110. _pos+=curData.size();
    111. qDebug() << _pos;
    112. }
    113. return 0;
    114. }
    115.  
    116. bool SqlFileEngine::atEnd() const
    117. {
    118. return (_pos>=size());
    119. }
    120.  
    121. qint64 SqlFileEngine::size() const
    122. {
    123. return _data.size();
    124. }
    125.  
    126. qint64 SqlFileEngine::pos() const
    127. {
    128. return _pos;
    129. }
    130.  
    131. void SqlFileEngine::init()
    132. {
    133. // filename == table/col/id
    134. QStringList strl=_filename.split("/");
    135. QSqlQuery query;
    136.  
    137. query.prepare("SELECT "+strl.at(1)+" FROM "+strl.at(0)+" WHERE id=:id");
    138. query.bindValue(":table", strl.at(0));
    139. query.bindValue(":col", strl.at(1));
    140. query.bindValue(":id", strl.at(2));
    141.  
    142. qDebug() << "SELECT " << strl.at(1) << " FROM " << strl.at(0) << " WHERE id=" << strl.at(2);
    143.  
    144. if(query.exec())
    145. {
    146. while(query.next())
    147. {
    148. qDebug() << query.value(0).toByteArray();
    149. _data=query.value(0).toByteArray();
    150. }
    151. }
    152. }
    153.  
    154. // main.cpp
    155. #include "sqlfileengine.h"
    156.  
    157. #include <QApplication>
    158. #include <QLabel>
    159. #include <QSqlDatabase>
    160.  
    161. int main(int argc, char* argv[])
    162. {
    163. QApplication app(argc, argv);
    164.  
    165. QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
    166. db.setDatabaseName("/home/segi/user.db");
    167. if(!db.open())
    168. {
    169. qWarning("Couldn't open database.");
    170. return -1;
    171. }
    172.  
    173. SqlFileEngineHandler engine;
    174.  
    175. QLabel label;
    176. label.setText("<img src=\"sql:images/image/1\" />");
    177. label.show();
    178.  
    179. return app.exec();
    180. }
    To copy to clipboard, switch view to plain text mode 
    I only get something like that:
    Qt Code:
    1. SqlFileEngine::setFileName() images/image/1
    2. SELECT "image" FROM "images" WHERE id="1"
    3. SqlFileEngine::open()
    4. SqlFileEngine::read( ¶ôß3·iøìº¿ñs·&#9618; , 1 )
    5. SqlFileEngine::read( PNG
    6. &#9618;
    7. , 16384 )
    8. SqlFileEngine::read( ¶ôß3·iøìº¿ñs·&#9618; , 1 )
    9. SqlFileEngine::read( PNG
    10. &#9618;
    11. , 16384 )
    12. SqlFileEngine::read( ¶ôß3·iøìº¿ñs·&#9618;8 , 1 )
    13. SqlFileEngine::read( PNG
    14. &#9618;
    15. , 16384 )
    16. SqlFileEngine::read( ¶ôß3·iøìº¿ñs·&#9618;8 , 1 )
    17. SqlFileEngine::read( PNG
    18. &#9618;
    19. , 16384 )
    20. ...
    To copy to clipboard, switch view to plain text mode 
    Is there another method to tell that the end of file is reached than return 0 in SqlFileEngine::read?
    What do I wrong?
    Where can I find better/more information about subclassing QAbstractFileEngine? (What have i to reimplement and so on.)

    Bye

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    By the way, here is a quick implementation.
    Attached Files Attached Files
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    segi (26th July 2009)

  11. #10
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    Thank you very much.
    I get it working. But why the read method is called so much times?
    I add a debug message to your MemFileHandler::read method and get the following output:
    Qt Code:
    1. Starting /home/segi/tmp/memfilehandler/memfilehandler...
    2. read: return 2844 counter 0
    3. read: return 2844 counter 1
    4. read: return 2844 counter 2
    5. ...
    6. read: return 2844 counter 27
    To copy to clipboard, switch view to plain text mode 
    Can I avoid these additional calls?
    Sadly there are a lot of errors at runtime:
    I tried big images: e.g. /usr/share/wallpapers/default-1600x1200.png
    Qt Code:
    1. Starting /home/segi/tmp/memfilehandler/memfilehandler...
    2. read: return 16384 counter 0
    3. read: return 16384 counter 1
    4. read: return 16384 counter 2
    5. ...
    6. read: return 16384 counter 27
    7. read: return 53 counter 28
    8. read: return 16384 counter 29
    9. libpng error: IDAT: CRC error
    To copy to clipboard, switch view to plain text mode 
    What can I do to avoid this error?
    On jpeg files I get:
    Qt Code:
    1. Starting /home/segi/tmp/memfilehandler/memfilehandler...
    2. read: return 378 counter 0
    3. read: return 378 counter 1
    4. Not a JPEG file: starts with 0xff 0xd9
    To copy to clipboard, switch view to plain text mode 
    I created a new jpg with gimp (100x100px only a white background).
    qDebug() << QImageReader::supportedImageFormats():
    Qt Code:
    1. Starting /home/sebastian/tmp/memfilehandler/memfilehandler...
    2. ("BW", "EPS", "EPSF", "EPSI", "EXR", "PCX", "PSD", "RGB", "RGBA", "SGI", "TGA", "XCF",
    3. "bmp", "bw", "dds", "eps", "epsf", "epsi", "exr", "gif", "ico", "jp2", "jpeg", "jpg",
    4. "mng", "pbm", "pcx", "pgm", "png", "ppm", "psd", "rgb", "rgba", "sgi", "svg",
    5. "tga", "tif", "tiff", "xbm", "xcf", "xpm", "xv")
    To copy to clipboard, switch view to plain text mode 
    What's the problem?

    Bye

  12. #11
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    Quote Originally Posted by segi View Post
    T
    I tried big images: e.g. /usr/share/wallpapers/default-1600x1200.png
    Qt Code:
    1. ...
    2. libpng error: IDAT: CRC error
    To copy to clipboard, switch view to plain text mode 
    What can I do to avoid this error?
    Sorry, my fault. I just copied and modified your MemFileHandler::read() method.
    I add
    Qt Code:
    1. m_pos+=maxlen
    To copy to clipboard, switch view to plain text mode 
    and the error on big files is gone.
    But why is this method called so often?
    Why doesn't work jpg files?

    Bye

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    What does QImageReader::supportedImageFormats() have to do with this?

    About the handler - it's probably buggy (although I can't see the bug) or incomplete. You'd have to reimplement every method from the interface and see which methods get called.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    Quote Originally Posted by wysota View Post
    What does QImageReader::supportedImageFormats() have to do with this?
    I thought the jpeg error would reasoned by a missing library or something like that.

    Bye

  15. #14
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    I now reimplement QAbstractFileEngine::isSequential() because it's called very often in the following methods:
    Qt Code:
    1. ICOReader::canRead(QIODevice *iodev)
    2. static QImageIOHandler *createReadHandlerHelper(QIODevice *device, const QByteArray &format, bool autoDetectImageFormat)
    3. somewhere in /usr/lib/kde4/plugins/imageformats/kimg_eps.so
    To copy to clipboard, switch view to plain text mode 
    If QAbstractFileEngine::isSequential() returns false QAbstractFileEngine::seek() is calling and the file will read once again. My isSequential() returns always true. (Now isSequential() is calling over 30 times but IHMO it's ok.)

    wysota: Thank you for your efforts.

    I try to solve the JPEG problem in another thread.

    Bye

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    But the device is not sequential...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #16
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    Mh, I thought sequential means: "ABCDEFGH" and reading follows in the same order.
    I read the data from the sql table into a QByteArray. Isn't a QByteArray sequential in RAM?
    (I'm not a professional programmer. I don't really know how a QByteArray is stored in RAM.)
    This little change solves all my problems:
    Big files could be read in and jpg files are also read correctly.
    But I want to do it in the right way.
    I can't find another possibility to stop countless recalls of QAbstractFileEngine::read.
    Maybe QAbstractFileEngine::seek would be right. But I don't know what to change.

    Bye

  18. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    Quote Originally Posted by segi View Post
    Mh, I thought sequential means: "ABCDEFGH" and reading follows in the same order.
    Sequential means you don't know how much data is available in the device and that you can only go "forward" with processing the data and never look back. In other words a sequential device is a device where implementing size() and seek() is not possible.


    I read the data from the sql table into a QByteArray. Isn't a QByteArray sequential in RAM?
    An opposite to sequential is "random access". QByteArray is a random-access (index based) container.

    I can't find another possibility to stop countless recalls of QAbstractFileEngine::read.
    Maybe QAbstractFileEngine::seek would be right. But I don't know what to change.
    My implementation of seek() is correct, the problem is elsewhere.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    Here is a corrected implementation - I wasn't updating position after a read attempt.
    Attached Files Attached Files
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. The following user says thank you to wysota for this useful post:

    segi (27th July 2009)

  21. #19
    Join Date
    Jul 2009
    Posts
    15
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto display QPixmap and Text?

    Thank you very much. It works like expected. (and isSequential() returns false now)
    The main problem was that i have testing with your little application and don't update the file position, too. I already noticed this a few posts above and changed it only in my app. Sorry for any inconvenience.
    A last little question....
    The application output:
    Qt Code:
    1. /home/segi/tmp/IMG_9066.JPG
    2. Open ok
    3. RAW: 2188490
    4. File read
    5. virtual qint64 MemFileHandler::read(char*, qint64) 0 2188490
    6. READ: 2188490
    7. MEM: 2188490
    8. virtual qint64 MemFileHandler::read(char*, qint64) 0 16384
    9. READ: 16384
    10. virtual bool MemFileHandler::seek(qint64) 0
    11. virtual qint64 MemFileHandler::read(char*, qint64) 0 16384
    12. READ: 16384
    13. virtual bool MemFileHandler::seek(qint64) 0
    14. virtual bool MemFileHandler::seek(qint64) 16384
    15. virtual qint64 MemFileHandler::read(char*, qint64) 16384
    16. READ: 16384
    17. virtual qint64 MemFileHandler::read(char*, qint64) 32768 16384
    18. READ: 16384
    19. virtual qint64 MemFileHandler::read(char*, qint64) 49152 16384
    To copy to clipboard, switch view to plain text mode 
    Why does Qt call this methods a few times before reading the real data?
    Qt Code:
    1. virtual bool MemFileHandler::seek(qint64) 0
    2. virtual qint64 MemFileHandler::read(char*, qint64) 0 16384
    To copy to clipboard, switch view to plain text mode 
    Is it possible that the QImageIOHandler wants to find out which image format the file is of?

    Bye

  22. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Howto display QPixmap and Text?

    Yes, that's possible. But that's just a guess. Is it important for you in any way? You can probably force the device to be sequential to omit that, if you want. But I wouldn't do that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QPixmap display on QGraphicsScene
    By febil in forum Qt Programming
    Replies: 2
    Last Post: 26th February 2009, 09:27
  2. Need Graphical Text on QPushButton/QLabel
    By JimDaniel in forum Qt Programming
    Replies: 6
    Last Post: 9th June 2008, 08:12
  3. Text on QPixmap
    By Fabix in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2006, 14:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.