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):
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):
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!
-
Hi
Are you using
http://doc.qt.io/qt-5/qgraphicsscene.html#drawBackground
? -
@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?
-
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