Important: Please read the Qt Code of Conduct -

How to create a line between 2 points with QLabel?

  • With the program that i'm developing i can draw a line between 2 points with QPainter, but now i need to do some stuff with this line that it has to be a label

    But how can i do it?
    For example:

        QLabel *child = new QLabel(this);
        child->setPixmap(QPixmap(url_linhal));##it is an image that i made on paint that is all black, to act like a line made from QPainter
        QRect rect(10,20,100,20);

    but there is a problem, with this code appears a rect between 2 points not a line, so how can i do this?

    i know that it is a crazy ideia, and i wanna know if there is a way to do it

  • Lifetime Qt Champion

    Just to understand.
    You draw a line with QPainter and you want to show this line as pixmap on
    a QLabel ?

  • yes! instead of using QPainter, represent this line as a QLabel

  • Lifetime Qt Champion

    Well QLabel can show a pixmap. It can not show line by itself.
    So the idea to draw on pixmap and show in label is fine enough.
    you can do that directly

      int h =100;
      int w = 100;
      QPixmap pix(w, h);
      QPainter paint(&pix);
      pix.fill( Qt::white );
      paint.setPen(QColor(0, 0, 0, 255));

    But what are you actually doing?
    Maybe a custom widget would work far better.

  • I guess a custom widget will be better, but i don't have any ideia to do that

    I want is to deal with the line as an object and to be clickable, i want to click in any part of the line and my program recognizes it.

    What i thought was to use a CustomLabel class that it emits a signal when it is clicked and create an obect of this class between 2 points.
    So the way you said before, i create a line inside my QLabel but it becomes to big for being a rect

  • Lifetime Qt Champion

    Well the image has to be as big as the points
    like 0,0 and 100,100 , the pixmap must be 100x100
    But if you want to click on it, an image is not so good as it
    has no idea where line is. so it needs the point anyway.

    Even with a custom widget, it must still be as big as the span between the points.

    So what should happen when you click the line at any part of it ?

  • It's not pretty yet, but that's what my program does
    I add objects on the screen and draw lines between then


    but this line has to be treated as an object as well, when i click in any point of the line it should open a window with its data

  • Lifetime Qt Champion

    well you need to store the 2 points for each line and
    then use math to find out if the user hits a line on mousePressed.
    How is the lines stored now ? ( the points you use to draw it )

    This explains it well

  • Lifetime Qt Champion


    Since you seem to want to interact and have things working dynamically, did you consider the Graphics View Framework ?

  • Exactly!

    Graphics View Framework would help me a lot, i guess

    Do you know some book or site that i can learn more about it?with some examples and everything

  • Lifetime Qt Champion

    Did you check the Qt documentation and related examples ?

  • Yes!
    But i'm having a problem.

    I saw in a video a guy using the the graphic view framework in a QDialog

    That's the code:


    class Dialog : public QDialog
        explicit Dialog(QWidget *parent = nullptr);
        Ui::Dialog *ui;
        QGraphicsScene *scene;
        QGraphicsEllipseItem *ellipse;
        QGraphicsRectItem *rectangle;


        ui(new Ui::Dialog)
        scene= new QGraphicsScene(this);
        ui->graphicsView->setScene(scene);////the problem is here!
        QBrush redBrush(Qt::red);
        QPen blackpen(Qt::black);

    It says that no member named 'graphicView' in Ui::Dialog, so how can i fix it?
    i want to use the application in qwidget or qdialog class

  • Lifetime Qt Champion

    Did you add a QGraphicsView widget name graphicsView through designer to your dialog ?

  • no, how can i do this?

  • Lifetime Qt Champion

    Well, use designer as explained here.

Log in to reply