Creating a coordinate system in Qt



  • Hello,

    so this is less a question about a specific programming error I'm getting, but more about how to design the concept I want to implement best. So what I want to create is a coordinate system with basic functionalities (similar to the one in the program Geogebra, in case you have heard of it):0_1521825119318_699a0f2c-9bd9-4f44-aa1d-0d19b1e0d914-image.png

    What is important that one can indefinitely drag (I'm emphasising this because I know that you could do this with a scroll bar, but I havent found out how I can do this with a simple click-and-drag feature) up/down and sideways. And this is what I have been having issues with. This is what I have so far (as you can see, it's far from perfect yet haha):0_1521825317680_78fc63d5-947d-41f1-8e4b-1082111e85a6-image.png

    My main issue is that I don't know how I can implement the "scroll indefinitely" feature. So basically what I'm doing right now is using a GraphicsScene + GraphicsView and a bunch of lines to draw the grid. But I am sure that this is not very efficient and there is a better way to do this. Does anybody have some tips? Note that I am not asking for someone to write the code for me, I just want to know some possible approaches to this. I hope someone can help me!


  • Qt Champions 2017



  • @mrjj Oh, no am I not. Right now I am kind of... drawing them manually, like this:

    void CoordinateGrid::drawAxis()
    {
        QPen axisPen(Qt::black);
        axisPen.setWidth(2);
    
        xAxis = graphicsScene->addLine(0, winSize.height()/2, winSize.width(), winSize.height()/2, axisPen);
        yAxis = graphicsScene->addLine(winSize.width() / 2, 0, winSize.width() / 2, winSize.height(), axisPen);
    }
    
    void CoordinateGrid::drawGrid()
    {
        QPen gridPen(Qt::gray);
        gridPen.setWidth(1);
    
        int boxSize = 40;
    
        for(int i = 1; i*boxSize <= winSize.height()/2; ++i)
        {
            graphicsScene->addLine(0, winSize.height()/2 - boxSize*(i), winSize.width(), winSize.height()/2 - boxSize*(i), gridPen);
            graphicsScene->addLine(0, winSize.height()/2 + boxSize*(i), winSize.width(), winSize.height()/2 + boxSize*(i), gridPen);
        }
    
        for(int i = 1; i*boxSize <= winSize.width()/2; ++i)
        {
           graphicsScene->addLine(winSize.width()/2 - boxSize*(i), 0, winSize.width()/2 - boxSize*(i), winSize.height(), gridPen);
           graphicsScene->addLine(winSize.width()/2 + boxSize*(i), 0, winSize.width()/2 + boxSize*(i), winSize.height(), gridPen);
        }
    }
    

    Should I use the BackgroundDrawer instead?


  • Qt Champions 2017

    Hi
    Well the grids i have seen uses that. If its more effective than just plain normal drawing
    i never tested but as far as i know, its easier to handle as the rect parameter always
    covers whole view and its design for such use case.

    Regarding endless scrolling. If you dont set a size - the size is the bouncing rect of all items. So in theory one should be able to drag it as far out sa one likes but not sure it will auto expand while dragging so might need some extra code.



  • I'm using a print report written in Qt (lgpl) where there is a report designer. It is very similar to what you have to do.
    You just have to look at the source code on how to draw, how to manage the mouse events...
    Please have a look
    https://github.com/fralx/LimeReport


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.