How to draw a horizontal line??

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

    #ifndef DRAWLINE_H
    #define DRAWLINE_H

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

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

    QPainter paint;
    QLineF line;
    QGridLayout *gLayout;

    #endif // DRAWLINE_H


    #include "drawline.h"

    DrawLine::DrawLine(QWidget *parent):QWidget(parent)
    , paint(this)
    gLayout = new QGridLayout;
    qDebug() << "Inside Constructor";
    qDebug() << "Inside Destructor";

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

    paint.setPen(QPen(Qt::black, 10));



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

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

    DrawLine *dLine = new DrawLine;
    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??

  • Moderators

    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!

Log in to reply