Featured, Qt

In case you ever wanted to use nodes-based editor in Qt, I think I’ve got just the right thing for you. I tried to make sure that no similar solution already existed. Now, briefly about the software I’ve stumbled upon and what is wrong with it in my opinion:

1) http://openassembler.wordpress.com/ – requires Python and PyQt which excludes pure C++ development – this doesn’t suit my needs

2) https://github.com/nical/kiwi – originally written in D (look above); C++ version is pretty much work in progress, and it’s a lot more complicated than QNodesEditor – among other things it imposes conventions intended for nodes-based processing with regards to data containers, ready states, etc. etc. – complicates things unnecessarily IMHO. UPDATE: After taking another look at kiwi I discovered it actually contains a very simple standalone node editor called Qiwi – it’s quite all-right, but doesn’t support port names and saving/loading.

UPDATE: 3) http://nukengine.com/qt-node-editor/ – this one was actually the closest to meet my expectations although I found the necessity to create GUI elements for nodes/ports myself and then attach logical nodes/ports classes to them to be bothersome. It’s a cool project though and I really like it, especially smart connections alignment.

QNodesEditor’s hallmark is being as simple as it gets. Blocks are just blocks, I don’t decide what they should be used for, maybe you just want them to be there and look pretty – it’s your choice. If you want to implement some processing logic – it’s up to you. New types of blocks can even be created completely dynamically in runtime, you don’t have to subclass anything. QNodesEditor is primarily a GUI tool.

Blocks consist of named input and output ports. Ports can have additional flags, currently just NamePort and TypePort (ports that cannot be used for connections, they’re just there to display some extra text with special formatting on the block – yes, you can think of it as an ugly hack). QNodesEditor supports saving and loading of node diagrams. Again – all data are saved as-is (names, connections, positions) without any introspection. It’s up to you to interpret the graphical schematic, attach extra data to it, etc. Blocks and connections are saved in the same order as they’re placed in the QGraphicsScene, so it seems natural to reference them by index when saving your application-specific data.

A short example:

QGraphicsScene *s = new QGraphicsScene(); ui->graphicsView->setScene(s); ui->graphicsView->setRenderHint(QPainter::Antialiasing); QNEBlock *b = new QNEBlock(0, s); b->addPort("test", 0, QNEPort::NamePort); b->addPort("TestBlock", 0, QNEPort::TypePort); b->addInputPort("in1"); b->addInputPort("in2"); b->addInputPort("in3"); b->addOutputPort("out1"); b->addOutputPort("out2"); b->addOutputPort("out3"); b = b->clone(); b->setPos(150, 0); b = b->clone(); b->setPos(150, 150); nodesEditor = new QNodesEditor(this); nodesEditor->install(s);

As you can see, it can hardly be any easier to set up a nodes-based editor. After creating a block “template” and cloning it several times to different positions in the scene, you just install QNodesEditor onto QGraphicsScene and voila, your QGraphicsScene is now working as a fully-fledged node editor. Left click and drag on nodes to move them around. Do the same with ports to create connections. Right click nodes and connections to delete them. You can do multiple selection by left-clicking with Ctrl pressed.

If you want to save your diagram, it cannot be any simpler:

QFile f("test.nodes"); f.open(QFile::WriteOnly); QDataStream ds(&f); nodesEditor->save(ds);

Want to load them back? No sweat:

QFile f("test.nodes"); f.open(QFile::ReadOnly); QDataStream ds(&f); nodesEditor->load(ds);

The code is BSD-licensed. You can use it as-is and develop application-specific stuff completely separately or just modify the code to suit your needs, since it’s really lightweight and easy to understand. Anyway – good luck and enjoy! Don’t forget to drop some feedback in case you find it useful (or not 😉 )!

Cheers!

DEMO: qnodeseditor_demo.zip

SOURCE CODE: qnodeseditor_src.zip

Qt5-compatible SOURCE CODE (by Carter): qnodeseditor-qt5.zip