Results 1 to 1 of 1

Thread: QGraphicsProxyWidget does not behave as child of QGraphicsWidget

  1. #1
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsProxyWidget does not behave as child of QGraphicsWidget

    Basically, what I'm trying to do, is incorporating a lot of custom drawing (and also editing the drawing, such as resizing, moving) by using a QGraphicsScene and QGraphicsWidget together with existing widgets, such as QComboBox and possibly some more 'simple' input widgets, such as LineEdit.

    At this point, my experiment of subclassing a QGraphicsWidget which holds one QComboBox has failed: the QComboBox draws itself, is accessible, responds to mouse events, but fails to move when the custom QGraphicsWidget moves.
    Qt Code:
    1. compositeGraphicsItem::compositeGraphicsItem(QGraphicsWidget *parent) :
    2. QGraphicsWidget(parent)
    3. {
    4.  
    5. }
    6.  
    7.  
    8. QRectF compositeGraphicsItem::boundingRect() const
    9. {
    10. qreal penWidth = 1;
    11. return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
    12. 50 + penWidth, 50 + penWidth);
    13. }
    14.  
    15. void compositeGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    16. QWidget *widget)
    17. {
    18. painter->setRenderHint(QPainter::Antialiasing);
    19. painter->drawRoundedRect(-10, -10, 50, 50, 5, 5);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. QGraphicsScene * gs = new QGraphicsScene(QRect(0,0,20,20));
    7. ui->graphicsView->setScene(gs);
    8. compositeGraphicsItem *c = new compositeGraphicsItem();
    9. gs->addItem(c);
    10. QGraphicsProxyWidget *proxy = gs->addWidget(new QComboBox());
    11. proxy->setParent(c);
    12. //c->setVisible(false); // when uncommented, this line should also hide the proxy, but it does not
    13. proxy->moveBy(10,10); // this works, moves the proxy
    14. c->moveBy(20,20); // this does not work as i intend it to, it moves the graphicsWidget, but not the proxy which should be a child and therefore be moved too
    15. }
    To copy to clipboard, switch view to plain text mode 

    I tried to re-implement the moveBy function, but I noticed that the foreach loop did not get executed, which means that the proxy is not registered as a child:
    Qt Code:
    1. void compositeGraphicsItem::moveBy(qreal dx, qreal dy){
    2. QGraphicsWidget::moveBy(dx,dy);
    3. foreach(QGraphicsItem * c, childItems()){
    4. c->moveBy(dx,dy);
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    I do understand that a proxy-widget is somewhat hackish, but I'd like to know how I would be able to make something useful of it, since I'd like to be scaling and perhaps even animating the scene.

    Any tips on how to solve this problem? I might be just looking at this completely wrong; there is so much to Qt that I haven't seen yet.

    Context:
    The application I want to be able to create with this is a viewer/manipulator for UML-based class diagrams like for example MS Visio is able to do. I'm trying to use a QAbstractItemModel together with a nicely structured hierarchy, so that I can always navigate it using a QTreeView.

    Partial Solution:
    Qt Code:
    1. void compositeGraphicsItem::moveBy(qreal dx, qreal dy){
    2. QGraphicsWidget::moveBy(dx,dy);
    3. foreach(QGraphicsProxyWidget* proxy, findChildren<QGraphicsProxyWidget*>()){
    4. proxy->moveBy(dx,dy);
    5. }
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    This at least makes the proxy move along with the composite widget. I don't know how elegant this solution is when using many elements though.

    Furthermore, it also doesn't solve the visibility issue.


    Added after 14 minutes:


    This solves the visibility issue I had.

    Qt Code:
    1. void compositeGraphicsItem::showEvent(QShowEvent *event){
    2. QGraphicsWidget::showEvent(event);
    3. foreach(QGraphicsProxyWidget* proxy, findChildren<QGraphicsProxyWidget*>()){
    4. proxy->setVisible(true);
    5. }
    6. }
    7. void compositeGraphicsItem::hideEvent(QHideEvent *event){
    8. QGraphicsWidget::hideEvent(event);
    9. foreach(QGraphicsProxyWidget* proxy, findChildren<QGraphicsProxyWidget*>()){
    10. proxy->setVisible(false);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    CAUTION: I realize why this is unwanted behaviour in the default case! For example, if the proxy is hidden, the actual widget is also hidden. This means that there would be some strange behaviour when this widget is also used in another location!


    Added after 55 minutes:


    When working with just the above solution, the default flags for ItemIsMovable allow moving of the QGraphicsWidget, but not the proxies inside; i.e. they won't move along again....

    Solution:
    Qt Code:
    1. void compositeGraphicsItem::moveEvent(QGraphicsSceneMoveEvent *event){
    2. foreach(QGraphicsProxyWidget* proxy, findChildren<QGraphicsProxyWidget*>()){
    3. QPointF p = event->newPos()-event->oldPos();
    4. proxy->moveBy(p.x(), p.y());
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    I'll keep adding all modifications I needed to work around all the issues I come by.


    Added after 19 minutes:


    Another update: if you are following this line of thought, and want to be able to create compositie graphicwidgets, it is necessary to have a pointer to *some* QGraphicsScene object, or it isn't really possible to create a proxy, AFAIK. So, just pass that scene along in the constructor, use it there and just add itself to the scene. For your convenience.
    Last edited by pinkieiknip; 21st May 2011 at 19:57.

Similar Threads

  1. Qml in QGraphicsWidget
    By animagani in forum Qt Quick
    Replies: 7
    Last Post: 2nd December 2010, 17:20
  2. Positioning QGraphicsWidget
    By jasper_ferrer in forum Qt Programming
    Replies: 3
    Last Post: 22nd September 2009, 15:34
  3. QGraphicsLinearLayout does not behave like QVBoxLayout
    By jobrandt in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2009, 10:59
  4. Replies: 0
    Last Post: 26th June 2009, 18:53
  5. Replies: 2
    Last Post: 18th March 2008, 16:38

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.