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. A question about paintEvent function
QtWS25 Last Chance

A question about paintEvent function

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 7.8k 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.
  • U Offline
    U Offline
    udrnrso
    wrote on last edited by
    #1

    I know Qt draw the window in the paintEvent function. I want to draw a line out of the paintEvent, how to do that?
    I want to draw a line in a function,like:
    @
    void MyClass::someFunction()
    {
    drawLine(...);
    }
    @

    not in the paintEvent, how to do ?

    Edit: fixed up question, spelling and formatting a bit. Please use @ tags around code, and "pay some attention to your language":http://www.catb.org/~esr/faqs/smart-questions.html#writewell. It really is important; Andre

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Have a look at QPainter documentation.

      inside paint event, it looks something like this:

      @
      void MyClass::paintEvent(QPaintEvent* event)
      {
      QPainter paint(this);
      paint.drawLine(pt1, pt2);
      }
      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        [quote author="udrnrso" date="1302160348"]i know -QT- Qt draw the window in the paintEvent function.i want to draw a line out of the paintEvent,how to do that?[/quote]

        Very simple: you don't.

        Well, that's a little bit to extreme. An approach could be to do your drawing on a QPixmap, and use a QLabel to display that pixmap. That would allow you to draw the pixmap at your own time. However, you will need to re-set the pixmap on the label each time you change it. Otherwise it won't update on screen.

        Another approach would be to look into QGraphicsView instead. It allows you to draw lines by just putting a QGraphicsLineItem on the scene.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          bq. Andre wrote Very simple: you don’t.

          why? Sometimes a custom drawn widget is needed, and this is just to start on how to do it? It's possible and easy.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [quote author="Gerolf" date="1302163343"]bq. Andre wrote Very simple: you don’t.

            why? Sometimes a custom drawn widget is needed, and this is just to start on how to do it? It's possible and easy.[/quote]

            Let me repeat the original question:

            [quote author="udrnrso" date="1302160348"]I know Qt draw the window in the paintEvent function. I want to draw a line out of the paintEvent, how to do that?
            i want to draw a line in a function,like:
            @
            void MyClass::someFunction()
            {
            drawLine(...);
            }
            @
            not in the paintEvent, how to do ?[/quote]

            I have made some minor edits and added some highlights to make the question clearer. He wants to draw outside the paintEvent function on a widget. In your answer, you tell him to... use the paintEvent.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              If you are neither on Windows nor on Mac OS X, you can try Qt::WA_PaintOutsidePaintEvent

              bq. From "QWidget":http://doc.qt.nokia.com/4.7/qwidget.html docs:
              Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

              bq. From "Qt namespace":http://doc.qt.nokia.com/4.7/qt.html docs:
              Qt::WA_PaintOutsidePaintEvent - Makes it possible to use QPainter to paint on the widget outside paintEvent(). This flag is not supported on Windows, Mac OS X or Embedded Linux. We recommend that you use it only when porting Qt 3 code to Qt 4.

              http://www.catb.org/~esr/faqs/smart-questions.html

              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