PDA

View Full Version : draing plugin on parent widget in qt-designer 4.5.3



dresha48
12th January 2010, 15:40
I created plugin which draws cross on parent widget. In constructor:

mAim=new HmiAim(this->parentWidget());

In header(description of class):

...

HmiAim *mAim;
}
class HmiAim:public QWidget
{
Q_OBJECT
protected:
virtual void paintEvent(QPaintEvent *event);
};

And paint event:


void
HmiAim::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen;
pen.setColor(QColor (225,104,15));
pen.setWidth(2);
painter.setPen(pen);
painter.drawLine(0,15,30, 15);
painter.drawLine(15,0, 15, 30);
}


It works everywhere ,exept designer(works in preview,in projects). It even works in designer if I save UI-file with this plugin and open it - cross will be and in designer mode for saved plagins, but not for new added.

wysota
12th January 2010, 16:45
Did you implement sizeHint() for your widget?

dresha48
13th January 2010, 16:07
Did you implement sizeHint() for your widget?

No, but how sizeHint can help?

wysota
13th January 2010, 17:06
If your widget is of size (0,0) then it may be hard to see it. I don't know if that's the problem here but you should definitely implement sizeHint() and rule this one out.

dresha48
14th January 2010, 00:39
If your widget is of size (0,0) then it may be hard to see it. I don't know if that's the problem here but you should definitely implement sizeHint() and rule this one out.

No - I set fixed size for this widget. And there is part which always drawn, but it draws for plugin - not for parent

wysota
14th January 2010, 05:28
You are drawing on the widget not on its parent in your code. Right?

dresha48
14th January 2010, 08:36
You are drawing on the widget not on its parent in your code. Right?
One part is drawing on widget(always work) and one on parent(always, exept new plugins in QT-designer) - there is two paint events for it.

wysota
14th January 2010, 09:04
Two paint events? How come? A widget can't draw on other widgets using paint events. It can only install an event filter.

dresha48
14th January 2010, 09:19
Two paint events? How come? A widget can't draw on other widgets using paint events. It can only install an event filter.
There is 2 classes in plugin and 2 paint events for them .

wysota
14th January 2010, 09:47
Please show us the code.

dresha48
14th January 2010, 10:57
Please show us the code.

Ok - files are attached .

wysota
14th January 2010, 12:20
I still fail to understand how is this supposed to work. You create a widget (HmiRackerPosition) that creates another widget and assigns it to the parent of the first widget. Then you start moving the second widget around possibly covering area other widgets occupy. Now the stacking rules say that a child is always above its parent but relative stacking order of sibling widgets is undefined (and may change "randomly" at any moment). Furthermore you move the second widget from within the first one's paint event and you also possibly change the size of the widget you are currently drawing on. Are you sure it makes sense for all of this to be part of a paint event? Could you say what is all this construction supposed to do?

dresha48
14th January 2010, 16:12
I still fail to understand how is this supposed to work. You create a widget (HmiRackerPosition) that creates another widget and assigns it to the parent of the first widget. Then you start moving the second widget around possibly covering area other widgets occupy.
I attached screenshot - how it looks in designer for test. 2 circles - is HmiRackerPositions and 2 crosses with M - is HmiAims.

Now the stacking rules say that a child is always above its parent but relative stacking order of sibling widgets is undefined (and may change "randomly" at any moment).
To be above other widgets this widgets need to be added at the end. Is there any normal way to select which child will be painting ealier?


Furthermore you move the second widget from within the first one's paint event and you also possibly change the size of the widget you are currently drawing on.Are you sure it makes sense for all of this to be part of a paint event?
It looks strange - but I removed part which recalculate pixels to user coordinates(if make parent widget bigger - plugin also will be bigger and vise versa)

Could you say what is all this construction supposed to do?

This plugin will show where will be racker position. At start it was just plugin which draws only on it(and made it not me). But when I took it - there was only one task - to make it draw on parent - and to draw above other plugins, I did not find better solution - maybe you know better. Also I tried to give parent painter from parent - and it works, but cross was always below other plugins. If you know how to make it better - please tell me :)

wysota
14th January 2010, 17:29
I attached screenshot - how it looks in designer for test. 2 circles - is HmiRackerPositions and 2 crosses with M - is HmiAims.
How it looks in Designer is one thing. What it is supposed to do is another.


To be above other widgets this widgets need to be added at the end.
The fact that this is true today doesn't mean it will be true tomorrow. Qt doesn't guarantee you such relation.


Is there any normal way to select which child will be painting ealier?
No and you're not supposed to need to do that.


It looks strange - but I removed part which recalculate pixels to user coordinates(if make parent widget bigger - plugin also will be bigger and vise versa)
It doesn't change the fact that you are placing some things into a paint event that shouldn't be there.


This plugin will show where will be racker position.
Please don't use the term "plugin" here. Your widget is definitely not a plugin.


At start it was just plugin which draws only on it(and made it not me). But when I took it - there was only one task - to make it draw on parent - and to draw above other plugins, I did not find better solution - maybe you know better. Also I tried to give parent painter from parent - and it works, but cross was always below other plugins. If you know how to make it better - please tell me :)

Let's start from the beginning... What exactly is the global effect you want to obtain? Just please refrain yourself from using terms "plugin", "parent", "child", "racker" and "above/over". A good start would be "I want to be able to paint a cross at a specified position on an arbitrary widget".

dresha48
15th January 2010, 09:17
The fact that this is true today doesn't mean it will be true tomorrow. Qt doesn't guarantee you such relation.
Is that for QT-version - then it`s good enough(tested in 4.5.3 , 4.6 - works fine).


It doesn't change the fact that you are placing some things into a paint event that shouldn't be there.
That`s not mine code, but why? And where it need to be , if it must be fixed size and it`s real size can be calculated only in paint event - rect size can be changed also in parent.


Please don't use the term "plugin" here. Your widget is definitely not a plugin.
It is a plugin .That is not mine term - it used in QT offical documentation - http://qt.nokia.com/doc/4.6/designer-worldtimeclockplugin.html . Plugin class inherited from QDesignerCustomWidgetInterface,which created this plugin was not attached , cause it have no sense for this problem .



Let's start from the beginning... What exactly is the global effect you want to obtain? Just please refrain yourself from using terms "plugin", "parent", "child", "racker" and "above/over". A good start would be "I want to be able to paint a cross at a specified position on an arbitrary widget".
Ok - I will try. I need to draw one part in widget (circle) and one part outside widget area(cross must be on "parent"). And all works, but not in designer mode(preview and projects - are fine).

wysota
15th January 2010, 10:05
Is that for QT-version - then it`s good enough(tested in 4.5.3 , 4.6 - works fine).
It might stop working in 4.6.1 or any other version as this is an undocumented behaviour.


That`s not mine code, but why?
Because a paint event can happen in an unpredictable point in time, an unpredictable number of times and it should only do painting, nothing else (speed is an issue here).


And where it need to be , if it must be fixed size and it`s real size can be calculated only in paint event
What is so special about paint event that the size can only be calculated there? The code you posted doesn't justify that in any way.


It is a plugin .That is not mine term - it used in QT offical documentation - http://qt.nokia.com/doc/4.6/designer-worldtimeclockplugin.html . Plugin class inherited from QDesignerCustomWidgetInterface,which created this plugin was not attached , cause it have no sense for this problem .
It is not a plugin. Plugins don't draw, can't be saved in Designer, can't be drawn on, can't be "above other plugins". These are all quotes from your posts. In all these situations you were talking about widgets and not plugins so don't mix the two, they are completely different entities. A Designer plugin is a class that gives Qt Designer means to instantiate custom widgets - nothing more, nothing less.


Ok - I will try. I need to draw one part in widget (circle) and one part outside widget area(cross must be on "parent"). And all works, but not in designer mode(preview and projects - are fine).

Ok, we're going in circles here... How come can I help you if you don't want to be helped? Why do I have to drag every bit of information out of you if it is in your interest and not mine?

dresha48
15th January 2010, 10:41
Ok, we're going in circles here... How come can I help you if you don't want to be helped? Why do I have to drag every bit of information out of you if it is in your interest and not mine?
I did not want to start argue, but I told this task at start - I just need to draw cross outside widget area on parent - that is the task - is that possible?

wysota
15th January 2010, 11:06
Yes, of course. You can use event filters for that or an overlay widget (you'll just have to carefully forward all events to proper widgets). Just please don't manipulate widgets in paint events...

dresha48
15th January 2010, 12:13
Big thanks - will try it at monday ....I think there will be some questions :)