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. Best way for draw points and lines?
QtWS25 Last Chance

Best way for draw points and lines?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 7.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.
  • B Offline
    B Offline
    blastdoman
    wrote on last edited by
    #1

    Hi everyone.

    My application allows the user to enter x and y coordinates using a table. Beside this, I have to draw these points and join them by lines. I know there are libraries like qwt that allow you to do this kind of things but for what I want, which is very simple, I would like to know if there is any other quick way to do it.

    Thank you

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! Create a new class that inherits from QWidget. Override its inherited paintEvent method. Inside that method you can use QPainter to draw your points and lines. For details see the good old Analog Clock Window Example.

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        For ultra simple function, you can also draw on pixmap and show in label :)

        QPixmap pix(500,500);
        QPainter paint(&pix);
        paint.setPen(QColor(255,34,255,255));
        // for loop draing lines...
        paint.drawRect(15,15,100,100);
        ui->Label->setPixmap(pix):;

        1 Reply Last reply
        0
        • B Offline
          B Offline
          blastdoman
          wrote on last edited by
          #4

          Thanks for your reply.

          But how would you do the following? You have the coordinates (0,0) in the top left corner of the widget and yo want to draw a point in the center. If you consider that the bottom right is the (10,10) coordinate, you want to draw the point in the (5,5). How do you establish that the bottom right corner is (10,10) in coordinate system if the widget has other dimensions?

          I hope I have explained

          mrjjM 1 Reply Last reply
          0
          • B blastdoman

            Thanks for your reply.

            But how would you do the following? You have the coordinates (0,0) in the top left corner of the widget and yo want to draw a point in the center. If you consider that the bottom right is the (10,10) coordinate, you want to draw the point in the (5,5). How do you establish that the bottom right corner is (10,10) in coordinate system if the widget has other dimensions?

            I hope I have explained

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @blastdoman
            Hi
            Im not sure what u are asking.
            If you have a widget or pixmap, its height() widdth() is the extend.
            Do you mean that your area might be smaller than the pixmap or widget?

            Update:
            http://doc.qt.io/qt-5/coordsys.html

            1 Reply Last reply
            0
            • B Offline
              B Offline
              blastdoman
              wrote on last edited by
              #6

              I'm a bit obtuse anyway hehe. I want to do something similar to what qwt graphs do, ie, for example, even if the plot has dimensions of 500x400, the x-axis goes from -10 to 10 and I paint the points according to these coordinates

              mrjjM 1 Reply Last reply
              0
              • B blastdoman

                I'm a bit obtuse anyway hehe. I want to do something similar to what qwt graphs do, ie, for example, even if the plot has dimensions of 500x400, the x-axis goes from -10 to 10 and I paint the points according to these coordinates

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @blastdoman
                Hehe obtuse can be cured with coffee often :)
                If you want a virtual coordinate system u will have to transform the painter
                http://doc.qt.io/qt-5/qtwidgets-painting-transformations-example.html

                Why not use QCustomplot ?
                unlike qwt its super easy to get going.
                Just add the 2 file to project and off u go.
                Then grab sample and tweak it a bit and be happy.

                it will be faster if u want virtual coordinates and u get zoom etc for free :)
                http://www.qcustomplot.com/index.php/tutorials/basicplotting

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  blastdoman
                  wrote on last edited by
                  #8

                  I think scaling would be a possible solution. I had tried it but I had found the problem that the pen changed size and therefore had discarded. Then I found the QPen :: setCosmetic (bool cosmetic) function and I saw that I could fix the problem.

                  Thanks mate

                  1 Reply Last reply
                  0

                  • Login

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