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. How to create a line between 2 points with QLabel?
Servers for Qt installer are currently down

How to create a line between 2 points with QLabel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 3 Posters 3.5k Views 2 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.
  • F Offline
    F Offline
    frnklu20
    wrote on 7 May 2019, 13:58 last edited by frnklu20 5 Jul 2019, 14:03
    #1

    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);
        child->setGeometry(rect);
        child->show();
    

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 7 May 2019, 14:03 last edited by
      #2

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

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frnklu20
        wrote on 7 May 2019, 14:05 last edited by
        #3

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

        M 1 Reply Last reply 7 May 2019, 14:07
        0
        • F frnklu20
          7 May 2019, 14:05

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 7 May 2019, 14:07 last edited by mrjj 5 Jul 2019, 14:11
          #4

          @frnklu20
          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));
            paint.drawLine(xxx) 
            ui->label->setPixmap(pix);
          

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

          1 Reply Last reply
          1
          • F Offline
            F Offline
            frnklu20
            wrote on 7 May 2019, 14:20 last edited by
            #5

            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

            M 1 Reply Last reply 7 May 2019, 14:23
            0
            • F frnklu20
              7 May 2019, 14:20

              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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 7 May 2019, 14:23 last edited by
              #6

              @frnklu20
              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 ?

              1 Reply Last reply
              0
              • F Offline
                F Offline
                frnklu20
                wrote on 7 May 2019, 14:32 last edited by
                #7

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

                0_1557239421701_daede2d6-eb37-4671-887c-e4d02e0c4430-image.png

                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

                M 1 Reply Last reply 7 May 2019, 14:36
                0
                • F frnklu20
                  7 May 2019, 14:32

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

                  0_1557239421701_daede2d6-eb37-4671-887c-e4d02e0c4430-image.png

                  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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 7 May 2019, 14:36 last edited by mrjj 5 Jul 2019, 14:37
                  #8

                  @frnklu20
                  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
                  http://www.jeffreythompson.org/collision-detection/line-point.php

                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 7 May 2019, 19:47 last edited by
                    #9

                    Hi,

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

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

                    1 Reply Last reply
                    1
                    • F Offline
                      F Offline
                      frnklu20
                      wrote on 10 May 2019, 19:38 last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 10 May 2019, 20:09 last edited by
                        #11

                        Did you check the Qt documentation and related examples ?

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

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          frnklu20
                          wrote on 13 May 2019, 19:31 last edited by
                          #12

                          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:

                          dialog.h

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

                          dialog.cpp

                              QDialog(parent),
                              ui(new Ui::Dialog)
                          {
                              ui->setupUi(this);
                          
                              scene= new QGraphicsScene(this);
                              ui->graphicsView->setScene(scene);////the problem is here!
                              QBrush redBrush(Qt::red);
                              QPen blackpen(Qt::black);
                          
                              ellipse=scene->addEllipse(10,10,100,100,blackpen,redBrush);
                          
                          }
                          

                          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

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 13 May 2019, 19:54 last edited by
                            #13

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

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

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              frnklu20
                              wrote on 13 May 2019, 19:57 last edited by
                              #14

                              no, how can i do this?

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 13 May 2019, 20:04 last edited by
                                #15

                                Well, use designer as explained here.

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

                                1 Reply Last reply
                                0

                                1/15

                                7 May 2019, 13:58

                                • Login

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