Important: Please read the Qt Code of Conduct -

How to create weighted graph in graphicsview

  • Hello. Please understand that I am not good at English.
    I want to make graph algorithm like this site
    I'm searching for google and this forum for one week, but I can't know how make that.
    To make that, firstly I should make weighted graph like this site, but I can't make that.

    2019-11-15 12;31;21.PNG

    I want to make node(like circle) and edge(line) that will be made at mouse pressed in graphicsview widget(not static, used mousepressevent and using it's pos). And, I should know that node's label, and edge's label and weight because I will make algorithm.
    If I know how to make that, I will can make adjacency list and kruskal's algorithm, etc..

    2019-11-15 12;37;02.PNG

    GraphicsScene should be applied in GraphicsView widget. Because I want make gui like picture.
    Thank you.

  • Lifetime Qt Champion

    Hi and welcome to the forums

    Your description reminded me of
    Which i think could be a good base to start from.
    The node can be dragged and it has a line class too.

    ps. your english seems fine. :)

  • @Kalidian If I may suggest, I'd say please consider separating the construction and handling of the graphs (i.e. adding edges, nodes, weights to edges) along with applying algorithms to them (Kruskal, shortest path, etc) from the graphical representation of the graph on screen.

    There are a bunch of proven graph theory libraries for C++ that you can use (it's a long time since I don't use them anymore, but take a look at Boost Graph, NGraph, LEMON, you can search them) that I think may save you lot of time and effort regarding the underlying graph data structure and operations. You can also have a way to save and restore graphs already built with some of them

    Once you have a data structure to support the graph in place, then you can move on to do the fancy display on the GUI.

    Good coding!

  • @mrjj Thank you for the reply!

  • Lifetime Qt Champion

    Np, and do please also consider @Pablo-J-Rogina very valid point that
    such algorithms can be had via a library so writing one yourself might not be the best
    way unless its only kruskal's you are after and the app will never need to use other algorithms.
    Or its simply for learning.

Log in to reply