Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Undefined size when drawing a foreground in QGraphicsView?
Forum Updated to NodeBB v4.3 + New Features

Undefined size when drawing a foreground in QGraphicsView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 389 Views 1 Watching
  • 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.
  • johnratiusJ Offline
    johnratiusJ Offline
    johnratius
    wrote on last edited by johnratius
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      johnratiusJ 1 Reply Last reply
      0
      • johnratiusJ Offline
        johnratiusJ Offline
        johnratius
        wrote on last edited by johnratius
        #3

        @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.

        johnratiusJ 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

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

          johnratiusJ Offline
          johnratiusJ Offline
          johnratius
          wrote on last edited by johnratius
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • johnratiusJ johnratius

            @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.

            johnratiusJ Offline
            johnratiusJ Offline
            johnratius
            wrote on last edited by
            #5

            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?

            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