Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt paintEvent Can I paint on a child widget?

Tags:

qt

I have a widget class subclass of QMainWindow, and it has a central widget(QWidget), and in the overriden paintEvent function, can I create an instance of QPainter on this central widget? Code like:

void MyMainWindow::paintEvent(QEvent *event)
{
    QPainter painter(_theCentralWidget);
    //drawing...
    return QMainWindow::paintEvent(event);
}

I don't want to create a new c++ class subclass of QWidget and override its paintEvent function and then replace the original central widget with this new one... (I did like above but an error occured saying the painter is not active...)

like image 981
tmj Avatar asked Nov 24 '25 16:11

tmj


1 Answers

Well. If you really, really, really don't want to sub-class the central widget, you can install a event filter to it and handle the paint event for it.

http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter

like image 86
Stephen Chu Avatar answered Nov 28 '25 02:11

Stephen Chu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!