Results 1 to 7 of 7

Thread: right click QPainter rectangle?

  1. #1
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default right click QPainter rectangle?

    hey guys, im using PyQt4 and i have created a grid of rectangles using the QPainter:

    painter = QtGui.QPainter(self)
    painter.drawRect(Ypos, Xpos, square_width, square_height)

    its nothing fancy, it just does the job for now.

    my question is simply is it possible to store a different string (im going to use it to store names for now) in each painted square, right click and print the string for example?

    im very new to PyQt4 and any examples would be much appreciated. thanks

    matty

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: right click QPainter rectangle?

    The way you are doing it now (painting a rect) the short answer is 'no', because the rect is not an object, just some coordinates you are giving to the painter.
    But instead of drawing the rects your self, you could just place QFrames (or QLabels) on your widget, then you can either use the obejectName() method, or set properties, or subclass and add a special field, a setter and getter for it, and as added bonus you don't have to worry about painting at all!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: right click QPainter rectangle?

    thanks alot, thats fantastic. will research into it now

  4. #4
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: right click QPainter rectangle?

    hey, using the frame method worked very well. i subclassed Qframe and added my own setter and getter methods. which work fine

    but still having trouble figuring out how to use the getter method when a qframe is clicked.

    any ideas how i might do this?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: right click QPainter rectangle?

    Have a look at QWidget::mousePressEvent()
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: right click QPainter rectangle?

    hey thanks worked like a charm.

    is there a way to do a mouse hover event on a qframe?

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: right click QPainter rectangle?

    Check the QEvent type enums.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 6
    Last Post: 5th June 2009, 09:38
  2. Replies: 2
    Last Post: 11th January 2009, 23:24
  3. how to draw columns in QPainter's rectangle
    By jyoti in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2007, 10:50
  4. Draw a rectangle alternating two colors with qPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 24th January 2006, 23:12
  5. Replies: 5
    Last Post: 12th January 2006, 15:40

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.