Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to use QPainter/Paint Events without subclassing QWidget?



  • 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?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



  • 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!


  • Lifetime Qt Champion

    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 :-)


Log in to reply