Results 1 to 7 of 7

Thread: Passing a QPixmap to other functions?

  1. #1
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Passing a QPixmap to other functions?

    Hi. I'm very new to C++ and Qt.

    In my constructor, I have a QPixmap called "overlay" and I set a label's pixmap to overlay so that it displays on the screen.

    I want to allow the user to draw a rectangle on the screen by clicking, dragging, and releasing. I did this exact thing in Java yesterday, so I understand how those mechanics work.

    My problem is: I don't know how to pass the overlay QPixmap to my events so that they can update the rectangle. Here's is what is the relevant snippet of what currently have:

    Qt Code:
    1. QPoint start;
    2. QPoint end;
    3.  
    4. MyWindow::MyWindow() {
    5. //Up here i define the layout, label, etc. that all works correctly
    6. QPixmap overlay; //I define this correctly, but chose not to include the code because it is irrelevant
    7. myLabel->setPixmap(overlay);
    8. }
    9.  
    10. void MyWindow::mousePressEvent(QMouseEvent *event) {
    11. start = event->globalPos();
    12. updateRectangle();
    13. }
    14.  
    15. void MyWindow::mouseMoveEvent(QMouseEvent *event) {
    16. end = event->globalPos();
    17. updateRectangle();
    18. }
    19.  
    20. void MyWindow::mouseReleaseEvent(QMouseEvent *event) {
    21. end = event->globalPos();
    22. updateRectangle();
    23. }
    24.  
    25. void updateRectangle() { //This section does not work. I need to get the QPixmap "overlay" to here so I can update it.
    26. QPainter p(&overlay);
    27. p.setPen(Qt::blue);
    28. p.drawRect(start.x(), start.y(), end.x() - start.x(), end.y() - start.y());
    29. p.end();
    30. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, I really don't know what I'm doing. Right now I get "overlay: undeclared identifier" and if I put overlay into the global scope, I get an error saying I can't make a painter before an app.

    Help would be greatly appreciated.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Passing a QPixmap to other functions?

    The thing that gives you visibility to "overlay" in the event handlers is to make it a member variable of your MyWindow class. After that all functions of MyWindow will see it. As it is now, it is local of and only visible to the constructor of MyWindow.

    That means that you also need to make updateRectangle() a member of MyWindow, that is, MyWindow::updateRectangle().

  3. The following user says thank you to mvuori for this useful post:

    DrAgonmoray (1st August 2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing a QPixmap to other functions?

    Quote Originally Posted by mvuori View Post
    The thing that gives you visibility to "overlay" in the event handlers is to make it a member variable of your MyWindow class. After that all functions of MyWindow will see it. As it is now, it is local of and only visible to the constructor of MyWindow.

    That means that you also need to make updateRectangle() a member of MyWindow, that is, MyWindow::updateRectangle().
    Wow, that is very good to know. Thank you.

    It now runs without erroring, however it doesn't work and I'm not sure why. The overlay appears on screen as expected, but when I click and drag, nothing at all happens. I drew a rectangle on screen earlier, but right here it doesn't work.
    Here's my current code (it has a few changes):
    Qt Code:
    1. void MyWindow::mousePressEvent(QMouseEvent *event) {
    2. start = event->globalPos();
    3. end = event->globalPos();
    4. updateRectangle();
    5. }
    6.  
    7. void MyWindow::mouseMoveEvent(QMouseEvent *event) {
    8. end = event->globalPos();
    9. updateRectangle();
    10. }
    11.  
    12. void MyWindow::mouseReleaseEvent(QMouseEvent *event) {
    13. end = event->globalPos();
    14. updateRectangle();
    15. }
    16.  
    17. void MyWindow::updateRectangle() {
    18. QPainter p(&overlay);
    19. p.setPen(Qt::blue);
    20. p.drawRect(start.x(), start.y(), end.x() - start.x(), end.y() - start.y());
    21. p.end();
    22. //myLabel->setPixmap(overlay);
    23. }
    To copy to clipboard, switch view to plain text mode 
    The overlay shows on screen but nothing happens when I click and drag. If I uncomment that line at the bottom, the overlay shows up but when I click the screens turns grey (as if the label is empty)
    Last edited by DrAgonmoray; 1st August 2012 at 00:57.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Passing a QPixmap to other functions?

    Before you go too much further, are you trying to do what QRubberBand can do for you?

  6. #5
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing a QPixmap to other functions?

    Quote Originally Posted by ChrisW67 View Post
    Before you go too much further, are you trying to do what QRubberBand can do for you?
    I think I am.

    Thanks, I'll try it out.


    Added after 38 minutes:


    Quote Originally Posted by ChrisW67 View Post
    Before you go too much further, are you trying to do what QRubberBand can do for you?
    Hi Chris,

    I got the rubber band working successfully, however I would like to change the appearance of it in two ways:
    • Make the border thicker
    • Change the background of the selected area


    Unfortunately I don't think this is possible, so I have to go back to what I was doing. Could I get some help there, please?
    Last edited by DrAgonmoray; 1st August 2012 at 01:47.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Passing a QPixmap to other functions?

    Is the aim of the exercise for the user to draw a single filled rectangle with a thick border by click and drag? You can use the QRubberBand to give the user an idea what size the box will be as they move the mouse then draw the final rectangle and hide the rubber band when the mouse button is released.

    You will need to think about how the rectangle will be drawn (painted). Painting should occur in the widget's paintEvent(). Have a look at the Scribble Example which draws in an off-screen QImage and then paints that.

  8. #7
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing a QPixmap to other functions?

    Quote Originally Posted by ChrisW67 View Post
    Is the aim of the exercise for the user to draw a single filled rectangle with a thick border by click and drag? You can use the QRubberBand to give the user an idea what size the box will be as they move the mouse then draw the final rectangle and hide the rubber band when the mouse button is released.

    You will need to think about how the rectangle will be drawn (painted). Painting should occur in the widget's paintEvent(). Have a look at the Scribble Example which draws in an off-screen QImage and then paints that.
    The aim IS to simply select the area, however there is a specific look I am trying to achieve.
    When the program starts, it 'freezes' the screen and blurs it (takes a screenshot, blurs it, displays it in the label that takes up the entire screen). Then the user should select an area of the screen to capture. Inside the captured area, the screen should be crystal clear (i.e. not blurry).

    I did this in Java yesterday, but due to Java's limitations (i couldn't get native keyboard input), I set out to do this using C++. Here is the exact effect that i am trying to achieve:
    Untitled.jpg

Similar Threads

  1. Replies: 10
    Last Post: 9th May 2011, 11:22
  2. Passing values to custom 'slot' functions (pyqt)
    By Richie in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2009, 08:05
  3. Replies: 4
    Last Post: 28th August 2008, 14:13
  4. Replies: 1
    Last Post: 21st August 2008, 08:44
  5. Replies: 5
    Last Post: 9th April 2007, 15:26

Tags for this Thread

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.