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 use QPainter/Paint Events without subclassing QWidget?
Qt 6.11 is out! See what's new in the release blog

How to use QPainter/Paint Events without subclassing QWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 898 Views 1 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.
  • N Offline
    N Offline
    not_a_seagull
    wrote on last edited by not_a_seagull
    #1

    I am using the Ritual port of Qt5, which allows Qt5 to be used within Rust. However, due to Rust's limitations I cannot subclass QWidget. I want to create a widget that can be painted on, for instance:

    let canvas_update = unsafe {
      Slot::new(move || {
        let mut painter = QPainter::new_1a(joystick_canvas);
        let mut painter = QPainter::new_1a(joystick_canvas);
    
        let blue = QColor::from_rgb_3a(156, 179, 255);
        let mut blue_pen = QPen::from_q_color(&blue);
        blue_pen.set_width(6);
    
        painter.set_pen_q_pen(&blue_pen);
        painter.draw_ellipse_4_int(5, 5, 195, 195);
      })
    };
    
    let my_widget = QWidget::new_0a();
    my_widget.update().connect(&canvas_update);
    

    However, an "update" slot or equivalent does not exist, as far as I can tell. Is there a way to call the Painter class outside of paint events that I do not know of?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      One possible workaround could be to paint to a QImage and then use a QLabel to show it.

      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
      • N Offline
        N Offline
        not_a_seagull
        wrote on last edited by
        #3

        I used a QPicture instead of a QImage, but using "set_picture" to assign this QPicture to the QLabel seems to work. Thank you for your help!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Great !

          Since you have it working now, please mark the thread as solved using the "Topic Tools" button, or the three dotted menu beside the answer that was the good one, so that forum users may know a solution has been found :-)

          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

          • Login

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