Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Is there any method for drawing grid on QWidget ?

Is there any method for drawing grid on QWidget ?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
7 Posts 5 Posters 4.6k 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.
  • M Offline
    M Offline
    Munkhtamir
    wrote on 15 Nov 2018, 02:25 last edited by
    #1

    Hello, Everyone. I want to draw grid on widget that is my main display widget. In other hand, i found any example, they using QGraphicsScene and QGraphicsView. My case is QWidget ? help me

    J 1 Reply Last reply 15 Nov 2018, 03:52
    0
    • M Munkhtamir
      15 Nov 2018, 02:25

      Hello, Everyone. I want to draw grid on widget that is my main display widget. In other hand, i found any example, they using QGraphicsScene and QGraphicsView. My case is QWidget ? help me

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 15 Nov 2018, 03:52 last edited by
      #2

      @Munkhtamir You can reimplement QWidget::paintEvent(). See http://doc.qt.io/qt-5/qtwidgets-painting-basicdrawing-example.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      M 1 Reply Last reply 15 Nov 2018, 10:03
      1
      • J JKSH
        15 Nov 2018, 03:52

        @Munkhtamir You can reimplement QWidget::paintEvent(). See http://doc.qt.io/qt-5/qtwidgets-painting-basicdrawing-example.html

        M Offline
        M Offline
        Munkhtamir
        wrote on 15 Nov 2018, 10:03 last edited by
        #3

        @JKSH But my drawing Widget is constructed to layout in Mainwindow. So i can't activate my painter into QWidget.

        J 1 Reply Last reply 15 Nov 2018, 11:29
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 15 Nov 2018, 10:17 last edited by
          #4

          The widget which you are adding to layout has to be custom widget. In the custom widget reimplement the paintEvent(..) function. You create the object of this custom widget & add to layout.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • M Munkhtamir
            15 Nov 2018, 10:03

            @JKSH But my drawing Widget is constructed to layout in Mainwindow. So i can't activate my painter into QWidget.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 15 Nov 2018, 11:29 last edited by
            #5

            @Munkhtamir Take a look at this: http://doc.qt.io/qt-5/designer-using-custom-widgets.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Munkhtamir
              wrote on 15 Nov 2018, 14:10 last edited by
              #6

              I tried to write custom widget. but it doesn't work.
              /////Widget.cpp///
              myCustomWidget = new QWidget;
              myCustomWidget->resize(800,600);
              QVBoxLayout *myBoxLayout = new QVBoxLayout;
              setLayout(myBoxLayout);
              scene = new MapGraphicsScene(this);
              view = new MapGraphicsView(scene,myCustomWidget);
              myBoxLayout->addWidget(view);
              ////anotherWidget.cpp//
              void anotherWidget::paintEvent(QPaintEvent *event)
              {
              QPainter painter(this);

                int step = GRID_STEP;
              
                painter.setPen(Qt::black);
                const QRectF rect = event->rect();
                qreal start = round(rect.top(), step);
                if (start > rect.top())
                {
                   start -= step;
                }
                for (qreal y = start - step; y < rect.bottom(); )
                {
                   y += step;
                   painter.drawLine(rect.left(), y, rect.right(), y);
                }
                // now draw vertical grid
                start = round(rect.left(), step);
                if (start > rect.left())
                {
                   start -= step;
                }
                for (qreal x = start - step; x < rect.right(); )
                {
                   x += step;
                   painter.drawLine(x, rect.top(), x, rect.bottom());
                }
                painter.end();
              

              }

              Result like this:
              0_1542290941686_Ubuntu 64-bit-2018-11-15-22-55-22.png

              Behind of Array has to be front of widget.

              J 1 Reply Last reply 15 Nov 2018, 14:20
              0
              • M Munkhtamir
                15 Nov 2018, 14:10

                I tried to write custom widget. but it doesn't work.
                /////Widget.cpp///
                myCustomWidget = new QWidget;
                myCustomWidget->resize(800,600);
                QVBoxLayout *myBoxLayout = new QVBoxLayout;
                setLayout(myBoxLayout);
                scene = new MapGraphicsScene(this);
                view = new MapGraphicsView(scene,myCustomWidget);
                myBoxLayout->addWidget(view);
                ////anotherWidget.cpp//
                void anotherWidget::paintEvent(QPaintEvent *event)
                {
                QPainter painter(this);

                  int step = GRID_STEP;
                
                  painter.setPen(Qt::black);
                  const QRectF rect = event->rect();
                  qreal start = round(rect.top(), step);
                  if (start > rect.top())
                  {
                     start -= step;
                  }
                  for (qreal y = start - step; y < rect.bottom(); )
                  {
                     y += step;
                     painter.drawLine(rect.left(), y, rect.right(), y);
                  }
                  // now draw vertical grid
                  start = round(rect.left(), step);
                  if (start > rect.left())
                  {
                     start -= step;
                  }
                  for (qreal x = start - step; x < rect.right(); )
                  {
                     x += step;
                     painter.drawLine(x, rect.top(), x, rect.bottom());
                  }
                  painter.end();
                

                }

                Result like this:
                0_1542290941686_Ubuntu 64-bit-2018-11-15-22-55-22.png

                Behind of Array has to be front of widget.

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 15 Nov 2018, 14:20 last edited by J.Hilk
                #7

                @Munkhtamir
                you have to create it after the other Widget, for it to be on top by default, otherwise you can call raise() on your grid-widget to make it the top most item.

                But I force a problem for after this, and thatone is because of transparency/opacity

                inside paintEvent you should call the base implementation

                QWidget::paintEvent(event);
                QPainter painter(this);
                ....

                That way your widget will react to style sheets, and you can set a simple stylesheet like

                setStyleSheet(background-color:transparent;);


                Edit: example:

                //Custom class .h
                #include <QWidget>
                
                class GridWidget : public QWidget
                {
                    Q_OBJECT
                public:
                    explicit GridWidget(QWidget *parent = nullptr);
                
                    void setGridDistance(int distance);
                
                protected:
                    void paintEvent(QPaintEvent *event);
                
                    int m_GridDistance = 20;
                };
                
                
                //Customclass .cpp
                #include "gridwidget.h"
                
                #include <QPainter>
                
                GridWidget::GridWidget(QWidget *parent) : QWidget(parent)
                {
                    setStyleSheet("background-color:transparent;");
                }
                
                void GridWidget::setGridDistance(int distance)
                {
                    m_GridDistance = distance;
                    update();
                }
                
                void GridWidget::paintEvent(QPaintEvent *event)
                {
                    QWidget::paintEvent(event);
                
                    QPainter p(this);
                    p.setPen(QPen(Qt::white,3));
                    for(int i(0); i < width(); i += m_GridDistance){
                        p.drawLine(i,0,i,height());
                    }
                
                    for(int i(0); i < height(); i += m_GridDistance){
                        p.drawLine(0,i,width(),i);
                    }
                }
                
                //Example main.cpp black widget with GridWidget ontop;
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                    QWidget *w = new QWidget();
                    w->setStyleSheet("background-color:black");
                    w->resize(200,200);
                    w->show();
                
                    QHBoxLayout *layout = new QHBoxLayout(w);
                    layout->setMargin(0);
                    layout->addWidget(new GridWidget());
                
                    return a.exec();
                } 
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1

                1/7

                15 Nov 2018, 02:25

                • Login

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