Closed reubengann closed 1 year ago
Closing this since I solved it myself. In case anybody in the future has the same question:
#include <QtNodes/GraphicsView>
#include <QtNodes/BasicGraphicsScene>
#include <QtGui/QScreen>
#include <QtWidgets/QApplication>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QVBoxLayout>
#include "SimpleGraphModel.h"
class NodeGraph {
public:
SimpleGraphModel graphModel;
QtNodes::GraphicsView* view;
QAction* createNodeAction;
NodeGraph()
{
{
NodeId id1 = graphModel.addNode();
graphModel.setNodeData(id1, NodeRole::Position, QPointF(0, 0));
NodeId id2 = graphModel.addNode();
graphModel.setNodeData(id2, NodeRole::Position, QPointF(300, 300));
graphModel.addConnection(ConnectionId{ id1, 0, id2, 0 });
}
auto scene = new QtNodes::BasicGraphicsScene(graphModel);
view = new QtNodes::GraphicsView(scene);
view->setContextMenuPolicy(Qt::ActionsContextMenu);
createNodeAction = new QAction(QStringLiteral("Create Node"), view);
QObject::connect(createNodeAction, &QAction::triggered, [&]() {
QPointF posView = view->mapToScene(view->mapFromGlobal(QCursor::pos()));
NodeId const newId = graphModel.addNode();
graphModel.setNodeData(newId, NodeRole::Position, posView);
});
view->insertAction(view->actions().front(), createNodeAction);
}
};
class MainWindow : public QWidget
{
public:
QVBoxLayout* layout;
NodeGraph* graph;
QGroupBox* groupBox;
QCheckBox* cb1;
QCheckBox* cb2;
MainWindow()
{
layout = new QVBoxLayout(this);
graph = new NodeGraph();
layout->addWidget(graph->view);
groupBox = new QGroupBox("Options");
cb1 = new QCheckBox("Nodes are locked");
cb2 = new QCheckBox("Connections detachable");
cb2->setChecked(true);
QVBoxLayout* vbl = new QVBoxLayout;
vbl->addWidget(cb1);
vbl->addWidget(cb2);
vbl->addStretch();
groupBox->setLayout(vbl);
layout->addWidget(groupBox);
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow window;
window.resize(800, 600);
window.showNormal();
return app.exec();
}
Hope someone can help me, I'm sure I'm doing something wrong. Would appreciate any help someone could lend.
I'm trying to adapt the SimpleGraphModel example. If I run all the code in main, I have no issues. However, when I try to move the setup of the graph into a MainWindow widget, I always get
Exception thrown: read access violation
in AbstractGraphModel's nodeData method.This code works:
but this does not