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

Thread: Thumbnail View

  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Thumbnail View

    How would I make a list of image files and their thumbnails? What kind of model and delegate would I need, and how do I use threads to make it not block the GUI while loading the image files?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Thumbnail View

    A model would be pretty simple.
    Use a standard item or list model, you can put an image or a pointer to an image in the data.
    You'll need to create a simple delegate that actually displays the image. You can use a custom role for that when you add the image to the model.

    As for the scaling, there's a nice example: http://doc.qt.nokia.com/4.6/qtconcur...gescaling.html

  3. #3
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    OK, so the model will store the filename and thumbnail, and the delegate will get the thumbnail and draw it.

    Also, I'd like to use a thread to load the thumbnails so that the UI isn't blocked, and just insert a temporary image while the thumbnail is loaded. Once the thread signals the model to set a thumbnail, how do I tell the delegate to redraw the item?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Thumbnail View

    You can put the thread creating the thumbnail in your model if you want.

    But, do not use a thread to do the actual painting in the view. This means: draw a thumnail that is already there directly on the screen, don't do this via a thread. Qt considers drawing on widgets in a separate thread illegal (unless you apply some voodoo magic)

  5. #5
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    But still, when the thread gives the model the image, I have to tell the delegate to redraw the item for it to show, right? How do I do that?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Thumbnail View


  7. #7
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    OK, but I have a few more questions:

    How do I load only one thumbnail at a time? I don't want to instantly start hundreds of threads for each image, right?

    If the data in the model changed, how do I cancel any thumbnail threads for indexes that don't exist any more or change those whose index changed?

    How to I pass a QImage in a QVariant? Somehow the delegate has to get the thumbnail. Or maybe have a thumbnail(QModelIndex) method instead of using the data() method?

  8. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Thumbnail View

    This is a little tricky but once you get the hang of it, it's pretty easy.

    For any custom type that you want to use with a QVariant, you need to declare and register it so the metacompiler knows about it.

    You declare your custom type with:
    Qt Code:
    1. Q_DECLARE_METATYPE(QImage);
    To copy to clipboard, switch view to plain text mode 

    The registering is done via:
    Qt Code:
    1. qRegisterMetaType<QImage>();
    2. qRegisterMetaType<QImage *>();
    To copy to clipboard, switch view to plain text mode 
    Registering is mandatory when using threads (queued connections)

    http://doc.qt.nokia.com/4.6/custom-types.html

    You can then use it in a QVariant like this:
    Qt Code:
    1. QImage myImage(...);
    2. QVariant myImageVariant = QVariant(myImage);
    3. QImage anotherImage = myImageVariant.value<QImage>();
    To copy to clipboard, switch view to plain text mode 

    About the drawing:
    Always draw only those things that are really on the screen. Things that are not on the screen waste computing power when they are drawn (unless of course you want to do some caching for example).

    The models come with an option called fetchMore and canFetchMore.
    Implement those to tell the view to only get the data from the model for the items on the screen, or the first 20 items etc...

  9. #9
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    Quote Originally Posted by tbscope View Post
    This is a little tricky but once you get the hang of it, it's pretty easy.

    For any custom type that you want to use with a QVariant, you need to declare and register it so the metacompiler knows about it.

    You declare your custom type with:
    Qt Code:
    1. Q_DECLARE_METATYPE(QImage);
    To copy to clipboard, switch view to plain text mode 

    The registering is done via:
    Qt Code:
    1. qRegisterMetaType<QImage>();
    2. qRegisterMetaType<QImage *>();
    To copy to clipboard, switch view to plain text mode 
    Where do I put those?

    Quote Originally Posted by tbscope View Post
    Registering is mandatory when using threads (queued connections)
    What do you mean? And what is a metatype?

  10. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Thumbnail View

    Quote Originally Posted by MTK358 View Post
    Where do I put those?
    You don't need to add these lines, sorry.
    QImage is already registered.

    Quoting the docs:
    A Note on GUI Types
    Because QVariant is part of the QtCore library, it cannot provide conversion functions to data types defined in QtGui, such as QColor, QImage, and QPixmap. In other words, there is no toColor() function. Instead, you can use the QVariant::value() or the qVariantValue() template function. For example:

    QVariant variant;
    ...
    QColor color = variant.value<QColor>();The inverse conversion (e.g., from QColor to QVariant) is automatic for all data types supported by QVariant, including GUI-related types:

    QColor color = palette().background().color();
    QVariant variant = color;

  11. #11
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    Anyway, I made a class called "ThumbThread" that inherits QThread, and it has one slot that adds a file to its internal queue. In its main thread, it loops forever, yielding if the queue is empty, otherwise retrieving a thumbnail of the first file in the queue and emitting a signal. But there are problems:

    It still kind of blocks the GUI (but not much) and the thumbnails appear in groups, instead of one by one.

    How do I draw the standard selection indicator from my delegate (or can I do this without a custom delegate)?

    Should I scale the image in the thumbnail thread or in the GUI thread based on the zoom setting, to be later implemented?

    What if files are removed from the model and useless files are queued in the thread?

  12. #12
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    Anyone?

    (10 char limit)

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

    Default Re: Thumbnail View

    You would do perfectly fine without using QThread and all the problems and programming errors related to it. Use QtConcurrent::run() instead.

    As for the delegate - do you need a custom delegate? The default one can draw thumbnails just fine.
    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. #14
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    Quote Originally Posted by wysota View Post
    You would do perfectly fine without using QThread and all the problems and programming errors related to it. Use QtConcurrent::run() instead.
    I'll read about that.

    Quote Originally Posted by wysota View Post
    As for the delegate - do you need a custom delegate? The default one can draw thumbnails just fine.
    How do I do that? Do I need to return an image for a certain role?

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

    Default Re: Thumbnail View

    Quote Originally Posted by MTK358 View Post
    Do I need to return an image for a certain role?
    Yes, for Qt::DecorationRole.
    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.


  16. #16
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    How do I make the thumbnail function return two values (a persistent model index and an image)?

    Also, I think that to make it easier to manage I need the model's internal list to be one of filename/thumbnail pairs. How wouldI do that?

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

    Default Re: Thumbnail View

    Why do you need a persistent model index?
    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.


  18. #18
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    What if items are added/removed from my model while the concurrent function is getting the thumbnail (and it somehow needs to tell the model which item the thumbnail belongs to)?

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

    Default Re: Thumbnail View

    I don't know what is your internal representation of the model but you can map QFuture to pointers to your items (in the internal representation) and use that to find the proper item. Of course you can use a persistent model index for this too but the concurrent function doesn't have to know the index. You can have something like QMap<QFuture*,QPersistentModelIndex> in your model. Just be aware that the keeping many persistent indexes is quite expensive.
    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. #20
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thumbnail View

    My internal representation is a list of file paths. Seems like it would make a lot of sense for it to be a list of file path/thumbnail pairs.

Similar Threads

  1. photo thumbnail viewer
    By chezifresh in forum Qt Programming
    Replies: 12
    Last Post: 3rd January 2012, 21:25
  2. show thumbnail
    By adonaicanez in forum Newbie
    Replies: 1
    Last Post: 19th October 2009, 07:46
  3. Replies: 0
    Last Post: 16th August 2009, 17:46
  4. how to browse images in thumbnail view?
    By zl2k in forum Qt Programming
    Replies: 12
    Last Post: 23rd April 2009, 02:20
  5. Creating thumbnail of image through qt
    By vishal.chauhan in forum Newbie
    Replies: 1
    Last Post: 30th January 2007, 13:30

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.