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. Keep lines drawn by drawPolyLine() function as an object?
Forum Updated to NodeBB v4.3 + New Features

Keep lines drawn by drawPolyLine() function as an object?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 618 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by
    #1

    Hi, I am making poly-lines on my QWidget using drawPolyLine() function of QPainter. I have an algorithm that detects when my mouse is over one of these poly-lines. What I want to achieve is to highlight the given poly-line (such as by changing the colour) so that the user understands that the poly-line can be selected. The problem is to change the colour of the poly-line I have to call QWidget's paintEvent() again which is highly inefficient as it redraws everything else. Is there any way I can create a poly-line object that is displayed in a QWidget and whose colour can be changed easily without calling paintEvent()? I know I can use QGraphicsItem but for that I will have to move my entire thing from QWidget to QGraphicsView-QGraphicsScene and I would like to avoid that.

    kshegunovK 1 Reply Last reply
    0
    • CJhaC CJha

      Hi, I am making poly-lines on my QWidget using drawPolyLine() function of QPainter. I have an algorithm that detects when my mouse is over one of these poly-lines. What I want to achieve is to highlight the given poly-line (such as by changing the colour) so that the user understands that the poly-line can be selected. The problem is to change the colour of the poly-line I have to call QWidget's paintEvent() again which is highly inefficient as it redraws everything else. Is there any way I can create a poly-line object that is displayed in a QWidget and whose colour can be changed easily without calling paintEvent()? I know I can use QGraphicsItem but for that I will have to move my entire thing from QWidget to QGraphicsView-QGraphicsScene and I would like to avoid that.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Is this what you're after?
      https://doc.qt.io/qt-5/qpainterpath.html

      Read and abide by the Qt Code of Conduct

      CJhaC 1 Reply Last reply
      0
      • kshegunovK kshegunov

        Is this what you're after?
        https://doc.qt.io/qt-5/qpainterpath.html

        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on last edited by
        #3

        @kshegunov Thanks, QPainterPath can work but I will still have to redraw it in paintEvent() I am looking for something that I can change the colour of without calling paintEvent() for the QWidget. Is it possible?

        kshegunovK JonBJ 2 Replies Last reply
        0
        • CJhaC CJha

          @kshegunov Thanks, QPainterPath can work but I will still have to redraw it in paintEvent() I am looking for something that I can change the colour of without calling paintEvent() for the QWidget. Is it possible?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @CJha said in Keep lines drawn by drawPolyLine() function as an object?:

          Is it possible?

          Absolutely not. How is it supposed to be drawn if you intend to not draw it? It just can't happen.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • CJhaC CJha

            @kshegunov Thanks, QPainterPath can work but I will still have to redraw it in paintEvent() I am looking for something that I can change the colour of without calling paintEvent() for the QWidget. Is it possible?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @CJha
            As you said originally, if you want graphical objects you can manipulate individually you need to move to QGraphicsItems. While you are painting on widgets, once you draw a line it's "gone", there is no object for you to access, and you need to redraw to recreate it.

            CJhaC kshegunovK 2 Replies Last reply
            0
            • JonBJ JonB

              @CJha
              As you said originally, if you want graphical objects you can manipulate individually you need to move to QGraphicsItems. While you are painting on widgets, once you draw a line it's "gone", there is no object for you to access, and you need to redraw to recreate it.

              CJhaC Offline
              CJhaC Offline
              CJha
              wrote on last edited by
              #6

              @JonB & @kshegunov Thanks for clearing it up. I just wasn't sure if it is possible or not because I could not find any reference to any such thing anywhere.

              1 Reply Last reply
              0
              • JonBJ JonB

                @CJha
                As you said originally, if you want graphical objects you can manipulate individually you need to move to QGraphicsItems. While you are painting on widgets, once you draw a line it's "gone", there is no object for you to access, and you need to redraw to recreate it.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @JonB said in Keep lines drawn by drawPolyLine() function as an object?:

                need to move to QGraphicsItems

                Or directly paint the prepared QPainterPaths, although the graphics item is going to do that for you, so there's some convenience.

                Read and abide by the Qt Code of Conduct

                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