Results 1 to 10 of 10

Thread: Making Icon with more than one image

  1. #1
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Red face Making Icon with more than one image

    Hye,

    I want to display some icon with my model.
    When I just use some simple picture (QIcon) it work well.

    But, I would like this icon to be an addition of an icon that represent the type of data, and an icon that repsent it's state.

    For exemple, I would like:
    qtg_aircraft.png + qtg_locked.png => qtg_aircraft_locked.png

    I tried to make a QIcon, and use the method addPixmap, after I resised the pixmap... but it do not work.
    I'm a big noob in interface coding, and image manipulation... if some of you can help

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

    Default Re: Making Icon with more than one image

    addPixmap won't work. Create a new icon which is a composition of both these icons and replace one icon with the other when you need it.

  3. #3
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Making Icon with more than one image

    Quote Originally Posted by wysota View Post
    addPixmap won't work.
    I've seen :P

    Quote Originally Posted by wysota View Post
    Create a new icon which is a composition of both these icons and replace one icon with the other when you need it.
    That exactly what I do not know how to do it: the composition

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Making Icon with more than one image

    Quote Originally Posted by weepdoo View Post
    That exactly what I do not know how to do it: the composition
    There's no generic plain-Qt way to achieve that... Still you can do it quite easily because QPixmap is a QPaintDevice... Hence you can do something like this (I ignored size and position stuff here but you'll have to play with them...) :
    Qt Code:
    1. QPixmap layer1("plane.png"), layer2("lock.png"), compose;
    2.  
    3. QPainter p(&compose);
    4. p.drawPixmap(layer1);
    5. p.drawPixmap(layer2);
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  5. The following user says thank you to fullmetalcoder for this useful post:

    weepdoo (11th June 2007)

  6. #5
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Making Icon with more than one image

    Hye everybody !

    I can now compose icon, but, I stell have a problem: the background color.
    I used the "setBackground" and "setBrush" method, but it do not seems to work as I wish.
    I always have a black background (and my image are in png with alpha).

    One more time: I'm developing in Python with PyQt :P

    Here is the display of the pixmap alone (without painting):
    image_without_composition.jpg

    The code for this:
    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui
    3. from PyQt4 import QtCore
    4.  
    5. if __name__ == "__main__":
    6. app = QtGui.QApplication(sys.argv)
    7.  
    8. i1 = QtGui.QPixmap('qtg_aircraft.png')
    9.  
    10. label = QtGui.QLabel()
    11. label.setPixmap(i1) #Just display the pixmap
    12. label.show()
    13. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    The same picture, with painting:
    image_composition_alone.jpg

    The code for this is:
    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui, QtCore
    3.  
    4. if __name__ == "__main__":
    5. app = QtGui.QApplication(sys.argv)
    6.  
    7. i1 = QtGui.QPixmap('qtg_aircraft.png')
    8.  
    9. result = QtGui.QPixmap(QtCore.QSize(24, 24))
    10. painter = QtGui.QPainter(result)
    11.  
    12. painter.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush))
    13. painter.drawImage(QtCore.QRect(0, 0, 24, 24), #<= we paint the Pixmap
    14. i1.toImage(),
    15. QtCore.QRect(0, 0, 24, 24))
    16. painter.end()
    17.  
    18. label = QtGui.QLabel()
    19. label.setPixmap(result) # <= display the result painted
    20. label.show()
    21. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    And last (but not least), with the same code, but I load one more pixmap, I've got this:
    image_composition_with_pixmap.jpg

    The code is:
    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui, QtCore
    3.  
    4. if __name__ == "__main__":
    5. app = QtGui.QApplication(sys.argv)
    6.  
    7. i1 = QtGui.QPixmap('qtg_aircraft.png')
    8. i2 = QtGui.QPixmap('qtg_locked.png') #<= the only new line !!!
    9.  
    10. result = QtGui.QPixmap(QtCore.QSize(24, 24))
    11. painter = QtGui.QPainter(result)
    12.  
    13. painter.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush))
    14.  
    15. painter.drawImage(QtCore.QRect(0, 0, 24, 24),
    16. i1.toImage(),
    17. QtCore.QRect(0, 0, 24, 24))
    18.  
    19. painter.end()
    20.  
    21. label = QtGui.QLabel()
    22. label.setPixmap(result)
    23. label.show()
    24. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Why my background is not transparent ? Am I steal wrong anywhere ?

    Moreover, why loading a new pixmap modify my painter ? Is it a bug in PyQt ?

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

    Default Re: Making Icon with more than one image

    Make sure the pixmap has an alpha channel. It might be easier for you to operate on QImages.

    Qt Code:
    1. QPixmap px(24,24);
    2. QPainter pt(&px);
    3. pt.fill(Qt::transparent); // this might not be required
    4. pt.drawImage(...);
    5. pt.drawImage(...);
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Making Icon with more than one image

    Here it is:
    image_with_alpha.jpg

    The code for this is:
    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui, QtCore
    3.  
    4. if __name__ == "__main__":
    5. app = QtGui.QApplication(sys.argv)
    6.  
    7. i1 = QtGui.QPixmap('qtg_aircraft.png')
    8. i2 = QtGui.QPixmap('qtg_locked.png')
    9.  
    10. alpha = QtGui.QPixmap(QtCore.QSize(24, 24))
    11. alpha.fill(QtCore.Qt.transparent)
    12.  
    13. result = QtGui.QPixmap(QtCore.QSize(24, 24))
    14.  
    15. painter = QtGui.QPainter(result)
    16.  
    17. painter.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush))
    18.  
    19. painter.drawPixmap(QtCore.QRect(0, 0, 24, 24),
    20. alpha,
    21. QtCore.QRect(0, 0, 24, 24))
    22.  
    23. painter.drawImage(QtCore.QRect(0, 0, 24, 24),
    24. i1.toImage(),
    25. QtCore.QRect(0, 0, 24, 24))
    26.  
    27. painter.drawImage(QtCore.QRect(0, 0, 15, 15),
    28. i2.toImage(),
    29. QtCore.QRect(0, 0, 24, 24))
    30.  
    31. painter.end()
    32.  
    33. label = QtGui.QLabel()
    34. label.setPixmap(result)
    35. label.show()
    36. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Why does it do not work ???

  9. #8
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Making Icon with more than one image

    I tried this:
    Qt Code:
    1. [...]
    2. i1 = QtGui.QPixmap('qtg_aircraft.png')
    3. i2 = QtGui.QPixmap('qtg_locked.png')
    4.  
    5. alpha = QtGui.QPixmap(QtCore.QSize(24, 24))
    6. alpha.fill(QtCore.Qt.transparent)
    7.  
    8. result = QtGui.QPixmap(QtCore.QSize(24, 24))
    9.  
    10. painter = QtGui.QPainter(result)
    11.  
    12. brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
    13. brush.setColor(QtGui.QColor(QtCore.Qt.transparent))
    14.  
    15. painter.setBrush(brush)
    16.  
    17. painter.drawPixmap(QtCore.QRect(0, 0, 24, 24),
    18. alpha,
    19. QtCore.QRect(0, 0, 24, 24))
    20.  
    21. painter.drawImage(QtCore.QRect(0, 0, 24, 24),
    22. i1.toImage(),
    23. QtCore.QRect(0, 0, 24, 24))
    24. [...]
    To copy to clipboard, switch view to plain text mode 

    But it do not work... any other idea ?

  10. #9
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Making Icon with more than one image

    I've played a bit with a very small code sample and here's what I come to so far :

    Qt Code:
    1. QPixmap layer1("tv.png"),
    2. layer2("locked.png"),
    3. compose(layer1.size());
    4.  
    5. compose.fill(Qt::transparent);
    6.  
    7. QPixmap alphacopy = layer2;
    8. alphacopy.setMask(layer2.mask());
    9.  
    10. QPainter p(&compose);
    11. p.drawPixmap( 0,
    12. 0,
    13. layer1);
    14.  
    15. p.drawPixmap( layer1.width() - layer2.width(),
    16. layer1.height() - layer2.height(),
    17. alphacopy);
    To copy to clipboard, switch view to plain text mode 

    Note : I'm not familiar with Python so I preferred posting my C++ code and let you (or someone else) port it to Python. I should not be hard anyway...

    Note bis : the tv.png comes from crystalsvg (64x64) and the locked.png I use is a 22x22, renamed, encrypted.png from crystalsvg.
    Attached Images Attached Images
    Current Qt projects : QCodeEdit, RotiDeCode

  11. The following user says thank you to fullmetalcoder for this useful post:

    weepdoo (11th June 2007)

  12. #10
    Join Date
    Jul 2006
    Location
    France
    Posts
    34
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Making Icon with more than one image

    Here is how I "translate" it to python:
    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui, QtCore
    3.  
    4. if __name__ == "__main__":
    5. app = QtGui.QApplication(sys.argv)
    6.  
    7. i1 = QtGui.QPixmap('qtg_aircraft.png')
    8. i2 = QtGui.QPixmap('qtg_locked.png')
    9.  
    10. result = QtGui.QPixmap(QtCore.QSize(24, 24))
    11. result.fill(QtCore.Qt.transparent)
    12.  
    13. alpha = QtGui.QPixmap(i2)
    14. alpha.setMask(i2.mask())
    15.  
    16. painter = QtGui.QPainter(result)
    17.  
    18. painter.drawPixmap(QtCore.QRect(0, 0, 24, 24),
    19. i1,
    20. QtCore.QRect(0, 0, 24, 24))
    21.  
    22. painter.drawPixmap(QtCore.QRect(0, 0, 15, 15),
    23. alpha,
    24. QtCore.QRect(0, 0, 24, 24))
    25.  
    26. painter.end()
    27.  
    28. label = QtGui.QLabel()
    29. label.setPixmap(result)
    30. label.show()
    31. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Thank to everyone who help me

Similar Threads

  1. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  2. Help needed handling image data
    By toratora in forum General Programming
    Replies: 2
    Last Post: 11th May 2007, 09:24
  3. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 14:39
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.