Results 1 to 6 of 6

Thread: Creating an Icon-Editor

  1. #1
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Creating an Icon-Editor

    Hello,

    I'm currently reading the "C++ GUI Programming with Qt 4"-book.

    In chapter 5, you create a custom "icon editor", which enables you to edit each pixel of an icon. The icon is stored in a QImage (Format_ARGB32).

    When there is a mousePressEvent, a pixel is painted.

    Qt Code:
    1. void IconEditor::mousePressEvent(QMouseEvent *event)
    2. {
    3. if(event->buttons() & Qt::LeftButton) {
    4. setImagePixel(event->pos(), true);
    5. } else if(event->buttons() & Qt::RightButton) {
    6. setImagePixel(event->pos(), false);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void IconEditor::setImagePixel(const QPoint &pos, bool opaque)
    2. {
    3. int i = pos.x() / zoom;
    4. int j = pos.y() / zoom;
    5.  
    6.  
    7. if(image.rect().contains(i, j)) {
    8. QRgb pixelColor = image.pixel(i, j);
    9. if(opaque) {
    10. image.setPixel(i, j, penColor().rgba());
    11. } else {
    12. image.setPixel(i, j, qRgba(0, 0, 0, 0));
    13. }
    14.  
    15. update(pixelRect(i, j));
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void IconEditor::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4.  
    5. for(int i = 0; i < image.width(); ++i) {
    6. for(int j = 0; j < image.height(); ++j) {
    7. QRect rect = pixelRect(i, j);
    8. if(!event->region().intersect(rect).isEmpty()) {
    9. QColor color = QColor::fromRgba(image.pixel(i, j));
    10. if(color.alpha() < 255)
    11. painter.fillRect(rect, color);
    12. painter.fillRect(rect, color);
    13. }
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    It works, but unfortunately very slow. Especially when the images are quite large. Then it takes some seconds to draw a pixel and it is inpossible to draw a continuous line. How is this solved in "real" image processing applications?


    And sorry for my bad english.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Creating an Icon-Editor

    This paintEvent() implementation is very inefficient.
    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
    Dec 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating an Icon-Editor

    Hi,

    I know, that paintEvent() repainted the whole area, even if just one pixel changed.

    Now, paintEvent() looks like this:
    Qt Code:
    1. void IconEditor::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4.  
    5. QRect updateRect = event->rect();
    6. updateRect = QRect(updateRect.topLeft() / zoom, updateRect.size() / zoom);
    7.  
    8. for(int i = updateRect.left(); i <= updateRect.right(); ++i) {
    9. if(!image.rect().contains(i, 0)) break;
    10. for(int j = updateRect.top(); j <= updateRect.bottom(); ++j) {
    11. if(!image.rect().contains(0, j)) break;
    12. QRect rect = pixelRect(i, j);
    13. if(!event->region().intersect(rect).isEmpty()) {
    14. QColor color = QColor::fromRgba(image.pixel(i, j));
    15. if(color.alpha() < 255)
    16. painter.fillRect(rect, color);
    17. painter.fillRect(rect, color);
    18. }
    19. }
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    So, only the part that changed is repainted. But it's still not possible to paint a continuous line.

    Is there a better way to implement paintEvent()?
    Attached Images Attached Images

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating an Icon-Editor

    Since mouse events are not coming on every one pixel change when moving cursor fast, then you have draw line (not only pixel) from the last mouse position (which you have to save on mouse move event I think) to current position.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating an Icon-Editor

    That sounds simple. I will try it, thank you.

  6. #6
    Join Date
    Nov 2011
    Location
    Bangalore
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Creating an Icon-Editor

    I have this icon editor in my project..........i need to know how can i display the text on this icon editor....
    For Ex: I have a line edit in which am goingto eb=nter a text which it should display on the icon editor.......... how can i do this???pls help

Similar Threads

  1. Replies: 2
    Last Post: 12th October 2008, 14:42
  2. Couple of questions: main window icon + toolbar icon
    By bpackard in forum Qt Programming
    Replies: 0
    Last Post: 20th March 2008, 19:03
  3. Replies: 1
    Last Post: 17th January 2008, 10:17
  4. QTableWidget and creating my own editor
    By macias in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2007, 11:02
  5. Creating simple text editor from the eamples code
    By overcast in forum Qt Programming
    Replies: 4
    Last Post: 14th February 2007, 15:46

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.