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
{
public:
protected:
.....
}
class GENBlock : public QGraphicsPathItem
{
public:
GraphicsItem *parent = 0, QGraphicsScene *scene = 0, QString nodeType = "");
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
.....
}
To copy to clipboard, switch view to plain text mode
proxy.cpp
#include "proxy.h"
.....
{
// THIS ITEM IS DRAWN AND DRAGGABLE AS EXPECTED //
p.addRoundedRect(-50, -15, 100, 30, 5, 5);
setPath(p);
setPen
(QPen(Qt
::darkGreen));
setBrush(Qt::green);
//////////////////////////////////////////////////////////////////
// THIS ITEM IS DRAWN BUT DOESN'T RESPOND TO MOUSE EVENTS //
QFormLayout *layout = new QFormLayout;
layout->addRow(numberLabel, numberEdit);
groupBox->setLayout(layout);
QGraphicsProxyWidget *w = scene->addWidget(groupBox);
//////////////////////////////////////////////////////////////////
}
#include "proxy.h"
.....
GENBlock::GENBlock(QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsPathItem(parent, scene)
{
// THIS ITEM IS DRAWN AND DRAGGABLE AS EXPECTED //
QPainterPath p;
p.addRoundedRect(-50, -15, 100, 30, 5, 5);
setPath(p);
setPen(QPen(Qt::darkGreen));
setBrush(Qt::green);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
//////////////////////////////////////////////////////////////////
// THIS ITEM IS DRAWN BUT DOESN'T RESPOND TO MOUSE EVENTS //
QGroupBox *groupBox = new QGroupBox("Contact Details");
QLabel *numberLabel = new QLabel("Telephone number");
QLineEdit *numberEdit = new QLineEdit;
QFormLayout *layout = new QFormLayout;
layout->addRow(numberLabel, numberEdit);
groupBox->setLayout(layout);
QGraphicsProxyWidget *w = scene->addWidget(groupBox);
w->setFlag(QGraphicsItem::ItemIsMovable,true);
w->setFlag(QGraphicsItem::ItemIsSelectable,true);
w->setFlag(QGraphicsItem::ItemIsFocusable,true);
//////////////////////////////////////////////////////////////////
}
To copy to clipboard, switch view to plain text mode
main.cpp
myView
->setRenderHint
(QPainter::Antialiasing);
myView->setSceneRect(0, 0, 800, 800);
myView->setBackgroundBrush(Qt::gray);
vlayout->addWidget(myView);
GENBlock *a = new GENBlock(0, myScene);
QGraphicsScene *myScene=new QGraphicsScene;
QGraphicsView *myView = new QGraphicsView(myScene);
myView->setRenderHint(QPainter::Antialiasing);
myView->setSceneRect(0, 0, 800, 800);
myView->setBackgroundBrush(Qt::gray);
QVBoxLayout* vlayout = new QVBoxLayout(mainClass.Interface.terraformTab);
vlayout->addWidget(myView);
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
Bookmarks