Results 1 to 9 of 9

Thread: Problem with QGraphicsProxyWidget implementation

  1. #1
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Problem with QGraphicsProxyWidget implementation

    Hi, I subclassed a QGraphicsProxyWidget and I can successfully add it to the scene. However, I cannot move it around, and it seems that no mouse events are triggered.

    Here is my implementation:

    proxy.h
    Qt Code:
    1. class GENProxy : public QGraphicsProxyWidget
    2. {
    3. public:
    4. GraphicsItem *parent = 0, QGraphicsScene *scene = 0);
    5. protected:
    6. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    7. void mousePressEvent(QGraphicsSceneMouseEvent * event);
    8. .....
    9. }
    To copy to clipboard, switch view to plain text mode 

    proxy.cpp
    Qt Code:
    1. #include "proxy.h"
    2. .....
    3. GENProxy::GENProxy(QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsProxyWidget(parent)
    4. {
    5. QGroupBox *groupBox = new QGroupBox("Contact Details");
    6. QLabel *numberLabel = new QLabel("Telephone number");
    7. QLineEdit *numberEdit = new QLineEdit;
    8. QFormLayout *layout = new QFormLayout;
    9. layout->addRow(numberLabel, numberEdit);
    10. groupBox->setLayout(layout);
    11. QGraphicsProxyWidget *w = scene->addWidget(groupBox);
    12. w->setFlag(QGraphicsItem::ItemIsMovable,true);
    13. w->setFlag(QGraphicsItem::ItemIsSelectable,true);
    14. w->setFlag(QGraphicsItem::ItemIsFocusable,true);
    15. w->setFocus();
    16. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. QGraphicsView *myView = new QGraphicsView(myScene);
    2. myView->setRenderHint(QPainter::Antialiasing);
    3. myView->setSceneRect(0, 0, 800, 800);
    4. myView->setBackgroundBrush(Qt::gray);
    5. QVBoxLayout* vlayout = new QVBoxLayout(mainClass.Interface.terraformTab);
    6. vlayout->addWidget(myView);
    7. GENProxy *a = new GENProxy(0, myScene);
    To copy to clipboard, switch view to plain text mode 

    As a matter of fact, the only signal showing activity is itemChange, but I can't set the focus or move the proxy around, and signals attached to the proxy embedded widgets aren't firing either (avoided to put those in the above code).
    I followed also another thread here (http://www.qtcentre.org/threads/3191...g-widget-focus) but couldn't come up with a solution.
    Thanks for any help.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsProxyWidget implementation

    I think instead of addWidget() you want to call setWidget() on your proxy class.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problem with QGraphicsProxyWidget implementation

    I modified the code using setWidget(), but still can't move the proxy (actually the proxy widget doesn't show at all).

    Also, the code I used for adding widget to a proxy comes directly from Qt documentation: http://qt-project.org/doc/qt-4.8/qgr...oxywidget.html

    I couldn't find many examples out there, please guys let me know if you can spot the issue or if somebody actually managed to do something like this. thanks
    Last edited by papillon; 24th April 2014 at 01:06.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsProxyWidget implementation

    The code you refer to is an example how to add a widget and get an automatically generated proxy item back.
    A convenience technique if all you need is the item. But you seem to want a special subclass.

    If you want to go with the inner proxy widget item, then your outer item type doesn't need to be a proxy. You can just use a normal QGraphicsItem and the parent for the proxy generated by the scene's convenience method.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problem with QGraphicsProxyWidget implementation

    Thanks for the help, but I still don't understand how to achieve this.
    I have modified the code so that there it uses a QGraphicsItem, which actually works as expected and allows me to move the item around if I use QPaint items, but doesn't as I try to use or add a QGraphicsProxyWidget (see proxy.cpp for the relevant code).

    proxy.h
    Qt Code:
    1. class GENBlock : public QGraphicsPathItem
    2. {
    3. public:
    4. GraphicsItem *parent = 0, QGraphicsScene *scene = 0, QString nodeType = "");
    5. protected:
    6. QVariant itemChange(GraphicsItemChange change, const QVariant &value);
    7. .....
    8. }
    To copy to clipboard, switch view to plain text mode 

    proxy.cpp
    Qt Code:
    1. #include "proxy.h"
    2. .....
    3. GENBlock::GENBlock(QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsPathItem(parent, scene)
    4. {
    5. // THIS ITEM IS DRAWN AND DRAGGABLE AS EXPECTED //
    6. p.addRoundedRect(-50, -15, 100, 30, 5, 5);
    7. setPath(p);
    8. setPen(QPen(Qt::darkGreen));
    9. setBrush(Qt::green);
    10. setFlag(QGraphicsItem::ItemIsMovable);
    11. setFlag(QGraphicsItem::ItemIsSelectable);
    12. //////////////////////////////////////////////////////////////////
    13.  
    14. // THIS ITEM IS DRAWN BUT DOESN'T RESPOND TO MOUSE EVENTS //
    15. QGroupBox *groupBox = new QGroupBox("Contact Details");
    16. QLabel *numberLabel = new QLabel("Telephone number");
    17. QLineEdit *numberEdit = new QLineEdit;
    18. QFormLayout *layout = new QFormLayout;
    19. layout->addRow(numberLabel, numberEdit);
    20. groupBox->setLayout(layout);
    21. QGraphicsProxyWidget *w = scene->addWidget(groupBox);
    22. w->setFlag(QGraphicsItem::ItemIsMovable,true);
    23. w->setFlag(QGraphicsItem::ItemIsSelectable,true);
    24. w->setFlag(QGraphicsItem::ItemIsFocusable,true);
    25. //////////////////////////////////////////////////////////////////
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. QGraphicsView *myView = new QGraphicsView(myScene);
    2. myView->setRenderHint(QPainter::Antialiasing);
    3. myView->setSceneRect(0, 0, 800, 800);
    4. myView->setBackgroundBrush(Qt::gray);
    5. QVBoxLayout* vlayout = new QVBoxLayout(mainClass.Interface.terraformTab);
    6. vlayout->addWidget(myView);
    7. GENBlock *a = new GENBlock(0, myScene);
    To copy to clipboard, switch view to plain text mode 


    Added after 28 minutes:


    Just a further explanation of what I'd like to accomplish, so that perhaps you guys can give me an advice on making things simpler.
    I need to create a very simple node-based interface, where the nodes are custom widgets (that obviously can be dragged around), each one with input and output ports. The container can be a simple QFrame.
    The reason why I'm after QGraphics*.* stuff is because I need to draw the connection lines between those node ports, but I understood I can actually draw lines or other QPaint items also subclassing and using QFrame::PaintEvent. So at this point, would this be a simpler and effective solution for what I need to achieve? Any pitfall?
    Thanks
    Last edited by papillon; 26th April 2014 at 13:24.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsProxyWidget implementation

    You are creating a proxy widget item "w" that is in no way connected to your item, i.e. you have two totally independent items.

    What I think you want to do is setting your item as the parent of "w"
    Qt Code:
    1. w->setParentItem(this);
    To copy to clipboard, switch view to plain text mode 

    "w" itself then probably doesn't even have to be movable or selectable, because its parent is.

    Cheers,
    _

  7. #7
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problem with QGraphicsProxyWidget implementation

    That's right, it was not parented to anything, so I added the instruction but the guy it's still stuck there and can't be moved around.
    Is that perhaps proxies need to have a custom mousemove event handler? Or you need to install and event filter of some kind?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QGraphicsProxyWidget implementation

    Hmm, could be that the widget is getting the events first and consumes them.

    Have you tried making your item the child of the proxy?

    Cheers,
    _

  9. #9
    Join Date
    Jan 2011
    Posts
    55
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Problem with QGraphicsProxyWidget implementation

    Hi, thanks for helping. After some experimenting, I've explicitly set the window flag on the addWidget to Qt::Window like this:

    Qt Code:
    1. QGraphicsProxyWidget *w = scene->addWidget(groupBox,Qt::Window);
    To copy to clipboard, switch view to plain text mode 

    This change added an invisible title bar to the widget, and now I can move it around. I've studied the window flags here: http://qt-project.org/doc/qt-4.8/qt....indowType-enum
    but still can't figure out why it shouldn't work in Qt::Widget mode.
    Also, I don't know yet what implications using Qt::Window may add. Besides that, I can now build and add a custom widget to the proxy, and drag it around.
    More questions to come for sure

Similar Threads

  1. Problem using QGraphicsProxyWidget with Xinerama
    By shobogenzo in forum Qt Programming
    Replies: 0
    Last Post: 29th September 2010, 02:24
  2. Replies: 8
    Last Post: 9th July 2010, 01:37
  3. List implementation problem
    By Kanguro in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2009, 19:38
  4. implementation of QabstractItemModel problem
    By mikelantonio in forum Newbie
    Replies: 2
    Last Post: 7th August 2009, 17:31
  5. problem with QGraphicsProxyWidget
    By dreamer in forum Qt Programming
    Replies: 5
    Last Post: 19th May 2008, 23:25

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.