Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Undefined size when drawing a foreground in QGraphicsView?



  • I am a little confused; I am trying to draw a grid, and every 4/8 lines I am supposed to get a different brush to make it more appealing.
    I create the function MyView::drawForeground(QPainter * painter, const QRectF & rect) and when I get the x & y & width & height and draw my grid in a loop, I don't see the other fancy lines. Here is what I mean:

    main.cpp

    #include <iostream>
    #include <QApplication>
    #include <QGraphicsView>
    #include <QtWidgets>
    
    class MyView : public QGraphicsView
    {
    public:
    	MyView(QWidget * parent = 0);
    	MyView(QGraphicsScene * scene, QWidget * parent = 0);
    	~MyView();
    
    protected:
    	void drawForeground(QPainter * painter, const QRectF & rect);
    };
    
    void drawGrid(QPainter * painter, const QRectF & rect);
    
    int main(int argc, char **argv)
    {
    	// Setup window and application
    	QApplication *app = new QApplication(argc, argv);
    	QMainWindow *window = new QMainWindow;
    	window->setWindowTitle("Grid Example");
    	window->resize(800, 600);
    
    	// Setup Main Widget
    	QWidget *mainWidget = new QWidget;
    	window->setCentralWidget(mainWidget);
    
    	// Setup graphics scene and view
    	MyView *view = new MyView;
    	QGraphicsScene *scene = new QGraphicsScene;
    	view->setScene(scene);
    
    	// Setup layout
    	QHBoxLayout *layout = new QHBoxLayout;
    	layout->addWidget(view);
    	mainWidget->setLayout(layout);
    
    	window->show();
    	return app->exec();
    }
    
    MyView::MyView(QWidget *parent) : QGraphicsView(parent)
    {
    }
    
    MyView::MyView(QGraphicsScene *scene, QWidget *parent) : QGraphicsView(scene, parent)
    {
    }
    
    MyView::~MyView()
    {
    }
    
    void MyView::drawForeground(QPainter *painter, const QRectF & rect)
    {
    	drawGrid(painter, rect);
    }
    
    void drawGrid(QPainter * painter, const QRectF & rect)
    {
    	int scale = 1;
    	int size = 24;
    	int startX = rect.width() / -2;
    	int endX = rect.width();
    	int x = startX;
    
    	int startY = rect.height() / -2;
    	int endY = rect.height();
    	int y = startY;
    
    
    	while (x <= endX)
    	{
    		x += size * scale;
    
    		if (x % (size * scale * 8) == 0)
    			painter->setPen(QPen(Qt::black, 2, Qt::DashLine));
    		else if (x % (size * scale * 4) == 0)
    			painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
    		else
    			painter->setPen(QPen(Qt::black, 1, Qt::DotLine));
    
    		painter->drawLine(x, startY, x, endY);
    	}
    
    	while (y <= endY)
    	{
    		y += size * scale;
    
    		if (y % (size * scale * 8) == 0)
    			painter->setPen(QPen(Qt::black, 2, Qt::DashLine));
    		else if (y % (size * scale * 4) == 0)
    			painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
    		else
    			painter->setPen(QPen(Qt::black, 1, Qt::DotLine));
    
    		painter->drawLine(startX, y, endX, y);
    	}
    }
    

    When I resize my window, the grid glitches and I see the other pen strokes for a frame but then it reverts to the else statement in the if statement. I set the startX and startY to 0 and it worked fine, but I wanted the grid to start at the top of the screen, not the middle.

    Any info why this happens, and how I can get my grid to work properly?


  • Lifetime Qt Champion

    Hi,

    Can you provide a minimal compilable example that reproduce your case ?



  • @SGaist Yes, I am referencing off of this python qt project. https://github.com/RoadrunnerWMC/Reggie-Next/blob/master/reggie.py Line 12681. This one compiles and works fine. This isn't much minimalist but it's what I got.



  • This post is deleted!


  • I found that using 0 as the start for X and Y draws it successfully, but it starts in the middle of the screen which is what I don't want. Does anyone have any suggestions?


Log in to reply