PDA

View Full Version : Problem drowing in tabWidget...



dspfen
9th December 2009, 03:43
Problem drowing in tabWidget...
I'm having a problem with a simple QWidget program that draws an ellipse inside a child tabWidget (in sоme of tabs).

I have Main ui form and a tabWidget in it.

Here is part of the code:


void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(ui->tab_2);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 12, Qt::ashDotLine, Qt::RoundCap));
painter.setBrush(QBrush(Qt::green, Qt::olidPattern));
painter.drawEllipse(80, 80, 400, 240);
}

Line 3 QPainter painter(ui->tab_2); doesn't do anything. The program draws the ellipse only if I replace line 3 with QPainter painter(this); but that draws on the parent QWidget (Main form) and not on the child QWidget (tabWidget) as desired.

Any Suggestion?

Thanks!

Tanuki-no Torigava
9th December 2009, 06:27
Hi,
can you enclose the code with \[CODE\]\[\/CODE\] next time and where is the ui declaration?

wysota
9th December 2009, 09:27
Line 3 QPainter painter(ui->tab_2); doesn't do anything.
Yes it does. It issues a warning on your console stating that you can only paint on a widget from within its paint event. And currently you are in paint event of a different widget.


Any Suggestion?

Install an event filter on the child widget and intercept its paint event there. You can draw from within the intercepted event.

dspfen
9th December 2009, 10:44
Marci for your replay, but I don't know how to install an event filter on the child widget and intercept its paint event in my project.
In the link below is my project.

wysota
9th December 2009, 12:04
Open Qt Assistant, go into index tab and type in "event filter".

dspfen
10th December 2009, 03:54
Super!
Еverything is working now as I want due to the help of wysota.
Thank you very much! :)