Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QGraphicsScene dynamically adding shapes in specific coordinates
QtWS25 Last Chance

QGraphicsScene dynamically adding shapes in specific coordinates

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qgraphicsscenecoordinatelayoutnetwork
1 Posts 1 Posters 987 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AthanDA Offline
    AthanDA Offline
    AthanD
    wrote on last edited by
    #1

    I want to draw a network graph in QT using ellipsis as nodes and lines as edges. The amount of nodes that while be placed as well as the spot that they will be placed on is user defined. For example: The user gives an vector with the elements 1,4,3,5 this means that there are going to be 4 columns of nodes and the first column will have 1 node, the second 4 nodes, e.t.c...

    The problem with QGraphicsScene is that every time I add a node it changes the whole arrangement of the nodes and the coordinate system and I end up seeing only the nodes that should be placed in the 4rth quadrant of the plane, these nodes are in the top left corner though and nothing else is displayed.

    There is also a difference in the colour of the nodes depending on their position but this doesn't matter. (Just making it clear because it affects the code.)

    The way I draw them is shown below:

    void drawNetworkGraphics(std::vector<int> topology){
    QBrush blackBrush(Qt::black);
    QBrush blueBrush(Qt::blue);
    QPen blackPen(Qt::black);
    blackPen.setWidth(2);
    
    int middleH = graphicScene->width()/2;
    int middleV = graphicScene->height()/2;
    
    for(unsigned i = 0; i< topology.size(); i++){
        if(i < topology.size()-1){
            if(i < topology.size()/2){
                for(unsigned k = 0; k<=topology[i]; k++){
                    if(k < topology[i]/2){
                        graphicScene->addEllipse(-(40*((topology.size()/2) - i)),-30*((topology[i]/2) -k),20,20,blackPen,blackBrush);
                    }
                    else{
                        if(k == topology[i]){
                            graphicScene->addEllipse(-40*((topology.size()/2) - i),(30*k)+1,20,20,blackPen,blueBrush);
                        }
                        else
                            graphicScene->addEllipse(-40*((topology.size()/2) - i),(30*k)+1,20,20,blackPen,blackBrush);
                    }
                }
            }
            else{
                for(unsigned k = 0; k<=topology[i]; k++){
                    if(k < topology[i]/2){
                        graphicScene->addEllipse((40*i),-30*((topology[i]/2) -k),20,20,blackPen,blackBrush);
                    }
                    else{
                        if(k == topology[i]){
                            graphicScene->addEllipse((40*i)+1,(30*k),20,20,blackPen,blueBrush);
                        }
                        else
                            graphicScene->addEllipse((40*i)+1,(30*k),20,20,blackPen,blackBrush);
                    }
                }
            }
    
        }
        else{
            for(unsigned k = 0; k<topology[i]; k++){
                if(k < topology[i]/2){
                    graphicScene->addEllipse(40*i+1,-30*(topology[i]/2 -k),20,20,blackPen,blackBrush);
                }
                else{
                    graphicScene->addEllipse(40*i+1,30*k+1,20,20,blackPen,blackBrush);
                }
            }
        }
    }
    }```
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved