Results 1 to 7 of 7

Thread: How to paint a selection rectangle on a pixmap?

  1. #1
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation How to paint a selection rectangle on a pixmap?

    Hi, I'm using Qt4 opensource version on Windows XP. I want to create a simple photo editor program. One of the options is to extract a rectangular part of the image and save it into a new image. The original image can be very large so my idea is to insert the image as a QPixmap in a QLabel and this QLabel is inserted in a QScrollArea. I have subclassed QScrollArea to reimplement the paintEvent method. This is the constructor:

    FotoAreaEditor::FotoAreaEditor(EditorFotos *parent)
    : QScrollArea(parent)
    {
    fotoLabel = new QLabel;
    fotoLabel -> setScaledContents(true);
    fotoLabel -> setPixmap(QPixmap("../book.png"));

    setWidget(fotoLabel);
    setWidgetResizable(false);
    setBackgroundRole(QPalette:ark);
    }

    In the paintEvent method I create a QPainter object with parent the viewport of the QScrollArea and I try to draw a rectangle on the label just to view the results:

    void FotoAreaEditor:aintEvent(QPaintEvent *event)
    {
    //QScrollArea:aintEvent(event);

    QPainter painter(this -> viewport());
    painter.setPen(Qt::blue);
    painter.setBrush(Qt:ense1Pattern);
    painter.drawRect(10, 20, 80, 60);
    }

    but only the pixmap in the QLabel is painted. And if I don't set any pixmap in the qlabel the rectangle is painted. Does anybody know why?. What am I doing wrong? I dont' have much experience with QPainter...

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: How to paint a selection rectangle on a pixmap?

    You should not use a QLabel but rather also paint the visible part of your image in the paintEvent of your scrollarea. The problem is that you paint in the scrollview but the label is above everything you paint.
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to paint a selection rectangle on a pixmap?

    Ok, I understand. But this solution will cause a lot of flickering when I move the scroll bars. It' wouldn't be more simple just inserting the image as the background of the QScrollArea with setPalette?

  4. #4
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: How to paint a selection rectangle on a pixmap?

    It will not cause a lot of flickering if done right, e.g. using double buffering
    It's nice to be important but it's more important to be nice.

  5. #5
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to paint a selection rectangle on a pixmap?

    Yes you are right. But I will be very grateful if you can give me some more details of how to do this double buffering (I haven't much practice working with images...)

  6. #6
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: How to paint a selection rectangle on a pixmap?

    If you are using Qt4, you're fine: Just draw the pixmap using QPainter::drawPixmap. If you use Qt3, first draw in a QPixmap and then bitBlt the QPixmap to the screen.

    I think Johan's pixmap view provides source that could be helpfull:

    http://www.digitalfanatics.org/e8joh...ies/index.html
    It's nice to be important but it's more important to be nice.

  7. #7
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to paint a selection rectangle on a pixmap?

    Ok thanks again

Similar Threads

  1. QPainter::begin: Cannot paint on a null pixmap
    By sabeesh in forum Qt Programming
    Replies: 5
    Last Post: 27th July 2010, 19:03
  2. How to remove the dotted rectangle of selection
    By jiveaxe in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2010, 23:00
  3. QTableView, no selection rectangle
    By alisami in forum Qt Programming
    Replies: 13
    Last Post: 25th December 2008, 20:50
  4. Paint XP radio button to pixmap
    By Ben.Hines in forum Qt Programming
    Replies: 2
    Last Post: 26th April 2006, 22:15

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.