Results 1 to 8 of 8

Thread: Custom QGraphicsRectItem with size grip

  1. #1
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Custom QGraphicsRectItem with size grip

    Hi all,
    I know that this topic has been covered in a couple of threads, so sorry for repeating, but...

    I'm trying to draw a size grip on my rectangles (class inherits from QGraphicsRectItem). I found this post: http://www.qtcentre.org/forum/f-newb...post33791.html and tried to do it in my reimplemented paint() method:

    Qt Code:
    1. void MyRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. q.drawControl(QStyle::CE_SizeGrip, option, painter, widget);
    4. QGraphicsRectItem::paint(painter, option, widget);
    5. }
    To copy to clipboard, switch view to plain text mode 

    But the only result I get is a lot of artefacts on the screen and not a proper grip in the (I presume) bottom right corner.

    Did any one succeed in implementing this? Would really appreciate your help.
    TIA,
    Regards,
    Bill

  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: Custom QGraphicsRectItem with size grip

    First of all you can't just create a new style object, use the one that's already there. Second of all you need to setup the option object you pass to drawControl. What it expects is surely different from what paint() receives from QGraphicsView.
    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
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom QGraphicsRectItem with size grip

    Hi,
    Thanks for the reply. I'm still a bit puzzled. To use the drawControl() method I need an object of class QStyle. QStyleOptionGraphicsItem (as I understand from the manual) is rather a structure for settings used by QStyle; it doesn't have the needed method.
    Could you please provide more detail?

    Thank you very much,
    Regards,
    Bill

  4. #4
    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: Custom QGraphicsRectItem with size grip

    QApplication::style() will return you the current style. As for the options object, you need to fill it out correctly. Looking at QSizeGrip code may be helpful.
    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.


  5. #5
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom QGraphicsRectItem with size grip

    Hi,
    I'm still trying to draw the resize grip on my custom rectangles, without any luck. Please find attached the header file and source code of my class with an image of the effects of the implementation... Maybe you will spot an error.

    I will appreciate any help.
    Regards,
    Bill

    The MyRectangle.h file:
    Qt Code:
    1. #ifndef __MYRECTANGLE_H__
    2. #define __MYRECTANGLE_H__
    3.  
    4. #include <QGraphicsRectItem>
    5.  
    6. class QPen;
    7. class QPainter;
    8.  
    9. class MyRectangle : public QGraphicsRectItem
    10. {
    11. public:
    12. MyRectangle(const QRectF &rect, QGraphicsItem *parent = 0);
    13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    14.  
    15. private:
    16. QPen pen;
    17. };
    18.  
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    The MyRectangle.cpp file:
    Qt Code:
    1. #include <QPen>
    2. #include <QStyleOptionGraphicsItem>
    3. #include <QStyleOptionSizeGrip>
    4. #include <QPainter>
    5. #include "myrectangle.h"
    6.  
    7. MyRectangle::MyRectangle(const QRectF &rect, QGraphicsItem *parent)
    8. : QGraphicsRectItem(rect,parent)
    9. {
    10. setFlags(ItemIsMovable|ItemIsSelectable);
    11. }
    12.  
    13. void MyRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    14. {
    15. opt.corner = Qt::BottomRightCorner;
    16. opt.rect = boundingRect().toRect();
    17. pen.setStyle(Qt::SolidLine);
    18. pen.setColor(QColor(Qt::black));
    19. pen.setWidth(1);
    20. if(option->state & QStyle::State_Selected)
    21. {
    22. pen.setStyle(Qt::DashLine);
    23. pen.setColor(QColor(Qt::green));
    24. }
    25. painter->setPen(pen);
    26. painter->drawRect(this->boundingRect());
    27. widget->style()->drawControl(QStyle::CE_SizeGrip, &opt, painter);
    28. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  6. #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: Custom QGraphicsRectItem with size grip

    I don't think setting corner and rect is enough. Did you look into QSizeGrip source code? You need to do whatever QStyleOption::init() does. You need to set (proper!) rect, corner, direction and palette, At least that's what CommonStyle uses for drawing the size grip.
    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.


  7. #7
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom QGraphicsRectItem with size grip

    Hi,
    Thanks for the help - yes, I had a look at the source of QStyleOption and QSizeGrip... In QStyleOption the init() method gets the different style settings from a widget (rect, direction, font metrics etc.). I also tried adding the QWidget as base class to MyRectangle, but it didn't change anything...

    Qt Code:
    1. void MyRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    2. {
    3. opt.corner = Qt::BottomLeftCorner;
    4. opt.rect = boundingRect().toRect();
    5. opt.direction = QApplication::layoutDirection();
    6. opt.palette = QApplication::palette();
    7. opt.fontMetrics = QApplication::fontMetrics();
    8. opt.state = option->state;
    9. pen.setStyle(Qt::SolidLine);
    10. pen.setColor(QColor(Qt::black));
    11. pen.setWidth(1);
    12. if(option->state & QStyle::State_Selected)
    13. {
    14. pen.setStyle(Qt::DashLine);
    15. pen.setColor(QColor(Qt::green));
    16. }
    17. painter->setPen(pen);
    18. QApplication::style()->drawControl(QStyle::CE_SizeGrip, &opt, painter);
    19. painter->drawRect(this->boundingRect());
    20. }
    To copy to clipboard, switch view to plain text mode 

    As you see I'm taking the settings from QApplication. The only problem is with the bounding rectangle. MyRectangles are created with another QGraphicsPixmapItem as a parent (rectangles on an image). The size grip tends to begin inside every rectangle and finish at the top of the parent image... There must be something wrong with the coordinates...

    If you have any other suggestions, please reply.
    Any help appreciated.

    Regards,
    Bill

  8. #8
    Join Date
    Jul 2009
    Location
    Dundee, United Kingdom
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Custom QGraphicsRectItem with size grip

    Ok,
    I can consider this topic closed... I found here http://www.qtcentre.org/forum/f-qt-p...pes-18238.html exactly what I needed
    Still - if someone finds a way to draw the QSizeGrip on QGraphicItems - I will be more than happy and grateful to know the solution.

    Cheers,
    Bill

Similar Threads

  1. resizing widgets depending on a main widget size
    By luf in forum Qt Programming
    Replies: 6
    Last Post: 10th October 2009, 16:13
  2. Replies: 7
    Last Post: 28th May 2009, 23:49
  3. QLabel size policy
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2007, 17:57
  4. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  5. size issues for custom QWidget in QScrollArea
    By anotheruser in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2006, 14:52

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.