Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How to draw a horizontal line??

    General and Desktop
    qpainter
    2
    2
    5408
    Loading More Posts
    • 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.
    • NIXIN
      NIXIN last edited by

      I want to draw a horizontal line through code and I tried something like below

      drawline.h
      #ifndef DRAWLINE_H
      #define DRAWLINE_H

      #include <QWidget>
      #include <QLineF>
      #include <QPainter>
      #include <QDebug>
      #include <QGridLayout>
      class DrawLine : public QWidget
      {
      public:
      DrawLine(QWidget *parent = 0);
      ~DrawLine();

      void lineEvent();
      void paintEvent(QPaintEvent *);
      

      private:
      QPainter paint;
      QLineF line;
      QGridLayout *gLayout;
      };

      #endif // DRAWLINE_H

      drawline.cpp

      #include "drawline.h"

      DrawLine::DrawLine(QWidget *parent):QWidget(parent)
      , paint(this)
      {
      gLayout = new QGridLayout;
      this->setLayout(gLayout);
      qDebug() << "Inside Constructor";
      }
      DrawLine::~DrawLine()
      {
      qDebug() << "Inside Destructor";
      }

      void DrawLine::paintEvent(QPaintEvent *)
      {
      qDebug() << "Inside paintEvent()";

      paint.setPen(QPen(Qt::black, 10));
      
      line.setP1(QPointF(80,80));
      line.setAngle(45);
      line.setLength(50);
      
      paint.drawLine(line);
      

      }

      main.cpp

      #include "drawline.h"
      #include <QApplication>

      int main(int argc, char **argv)
      {
      QApplication a(argc, argv);

      DrawLine *dLine = new DrawLine;
      dLine->show();
      
      
      return a.exec();
      

      }

      This code is building well but when I run it, following errors, occurs:
      QWidget::paintEngine: Should no longer be called
      QPainter::begin: Paint device returned engine == 0, type: 1
      Inside Constructor
      setGeometry: Unable to set geometry 22x22+363+124 on QWidgetWindow/'QWidgetClassWindow'. Resulting geometry: 116x22+363+124 (frame: 8, 30, 8, 8, custom margin: 0, 0, 0, 0, minimum size: 22x22, maximum size: 16777215x16777215).
      Inside paintEvent()
      QPainter::setPen: Painter not active
      Inside paintEvent()
      QPainter::setPen: Painter not active

      Can somebody help me fix it??

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by Chris Kawa

        There are a couple of errors here and some weird unused code. Lets go over it:

        • setGeometry: Unable to set geometry (...) - this is just a warning. A widget has a 0 minimum size and minimum size hint by default. You used a layout on it so it has some margins (11 px in this case) Because of the frame of the window a window manager can't create a window that small so it resizes it to a smallest possible size and lets you know with a warning. To fix it either use setMinimumSize() or override minimumSizeHintfor your widget.

        • QPainter related messages - you're creating a painter as a class member variable. Don't do that. Create the painter locally in the paint event with a proper surface to use it on:

        void DrawLine::paintEvent(QPaintEvent *) {
            QPainter p(this);
            //paint using p
            ...
        
        • There's no such thing as a lineEvent() - what is that?

        • What's the layout for? Are you gonna put something in it? If yes then remember that it will obscure whatever you paint in your paint event.

        • You're not deleting the dLine variable in main(). It's leaking. Either create it on the stack (which is the easiest and recommended), set a Qt::WA_DeleteOnClose attribute on it or delete it manually after the a.exec() call returns (pointless manual work).

        • just a hint - when overriding virtual methods use override specifier. It will save you a lot of typo related bugs:

        void paintEvent(QPaintEvent *);          //compiles fine and overrides the base implementation
        void pAintEvent(QPaintEvent *);          //compiles fine and never gets called because of a typo
        void pAintEvent(QPaintEvent *) override; //doesn't compile and lets you know. yay!
        
        1 Reply Last reply Reply Quote 2
        • First post
          Last post