Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, January 2007
custom widget & layout


Message 1 in thread

Hello,

got an annoying artifact when adding a custom widget into a layout. My
custom widget is a class, derived from QWidget, and in this class (to be
exact, in its paintEvent() ), a pixmap is loaded and painted. The
widgets size is the same as the size of the pixmap.
Now, I don't understand the following behaviour: I construct an instance
of my widget in the constructor of the parent (another custom widget,
derived from QWidget). The child is painted correctly, if I set no other
attributes, childs, layouts or something like that. I want to position
this child with a layout ( QHBoxLayout ). In that case, the custom child
is not visualized. It seems that the layout knows that something is
there, but my widget is not painted. (I also tried to add a QVBoxLayout
containing my child and a label. The label was moved in the right way.
Therefore, I assume that the layouts know about the size of my little
custom widget. )

that does not work:

    ParentWidget::ParentWidget( QWidget* parent, Style style, const
QColor& color ) :                     CustomWidget( parent, style, color )
    {
      m_topLayout = new QHBoxLayout( this );
      QVBoxLayout* testLayout = new QVBoxLayout();
      QLabel* testLabel = new QLabel( "Label", this );
      testLayout->addStretch( 1 );
      testLayout->addWidget( testLabel );
      CustomChild* testChild = new CustomChild( this );
      testLayout->addWidget( testChild );         // <- That seems not
to be working
      testLayout->addStretch( 1 );
      m_topLayout->addLayout( testLayout );
    }

that is working:

    ParentWidget::ParentWidget( QWidget* parent, Style style, const
QColor& color ) :                     CustomWidget( parent, style, color )
    {
      CustomChild* testChild = new CustomChild( this );
    }

in the paintEvent() of CustomChild, a pixmap is loaded and painted:
    ...
    QPainter p(this);
    QPixmap arrows.load(":/images/arrows.png");
    p.drawPixmap( QRectF( this->x(), this->y(), arrows.width(),
arrows.height() ),
                              arrows,
                             QRectF( 0, 0, arrows.width(),
arrows.height() ) );
    ...

thx in advance!
Regards, Clemens

--
 [ signature omitted ] 

Message 2 in thread

Tuesday 02 January 2007 18:27 Clemens Clausen wrote:
> got an annoying artifact when adding a custom widget into a layout. My
> custom widget is a class, derived from QWidget, and in this class (to be
> exact, in its paintEvent() ), a pixmap is loaded and painted. The
> widgets size is the same as the size of the pixmap.
> Now, I don't understand the following behaviour: I construct an instance
> of my widget in the constructor of the parent (another custom widget,
> derived from QWidget). The child is painted correctly, if I set no other
> attributes, childs, layouts or something like that. I want to position
> this child with a layout ( QHBoxLayout ). In that case, the custom child
> is not visualized. It seems that the layout knows that something is
> there, but my widget is not painted. (I also tried to add a QVBoxLayout
> containing my child and a label. The label was moved in the right way.
> Therefore, I assume that the layouts know about the size of my little
> custom widget. )

It's quite difficult to figure out what the problem is, when you don't send 
the code for the child widget. However, it sounds like you didn't implement 
sizeHint() (which should probably just return the size of the image). You 
should also set the size policy to fixed.

> that does not work:
>
>     ParentWidget::ParentWidget( QWidget* parent, Style style, const
> QColor& color ) :                     CustomWidget( parent, style, color )
>     {
>       m_topLayout = new QHBoxLayout( this );
>       QVBoxLayout* testLayout = new QVBoxLayout();
>       QLabel* testLabel = new QLabel( "Label", this );
>       testLayout->addStretch( 1 );
>       testLayout->addWidget( testLabel );
>       CustomChild* testChild = new CustomChild( this );
>       testLayout->addWidget( testChild );         // <- That seems not
> to be working
>       testLayout->addStretch( 1 );
>       m_topLayout->addLayout( testLayout );
>     }
>
> that is working:
>
>     ParentWidget::ParentWidget( QWidget* parent, Style style, const
> QColor& color ) :                     CustomWidget( parent, style, color )
>     {
>       CustomChild* testChild = new CustomChild( this );
>     }

And that supports the idea that the layout thinks your CustomChild should be 
shrunk to nothing.

> in the paintEvent() of CustomChild, a pixmap is loaded and painted:
>     ...
>     QPainter p(this);
>     QPixmap arrows.load(":/images/arrows.png");
>     p.drawPixmap( QRectF( this->x(), this->y(), arrows.width(),
> arrows.height() ),
>                               arrows,
>                              QRectF( 0, 0, arrows.width(),
> arrows.height() ) );

Don't use QRectF when QRect will do. Actually, just do

QPixmap arrows(":/images/arrows.png");
p.drawPixmap(0, 0, arrows);

Inside the paintEvent, you're in the coordinates of this widget, not in global 
or window coordinates.

Bo.

-- 
 [ signature omitted ] 

Message 3 in thread

Hi

I have a function that needs to load the data dynamically.

That is for ex., suppose i have 1000 records,
and i load the 100 records and show the table and use a timerevent
so that i can add the remaining table rows.

During this process, when i display the table and then append then will the
table gets no event during this table row addition. plz clarify.

That is my question is

Does the table block other events if any data is added to the table as a 
background process?

Plz respond as sson as possible.

Thanks & Regards
Deepa


DISCLAIMER

âThe information contained in this e-mail message and/or attachments to it may contain confidential or privileged information. If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachments. Thank you"
--
 [ signature omitted ] 

Message 4 in thread

Of course, I should have mentioned the size. Sorry for that.
The size of my child class is determined with setFixedSize( QSize(
arrows.width(), arrows.height() ) ). The size of the child is correct,
all my outputs were as expected. My second hint to this assumption is
that the label in the same layout is not placed in the center where I
expect it to be if my custom child would be infinitesimal or just not
existent. The label is shifted (as it would be if the layout contains my
custom widget ). Therefore, I guess, the layout knows the correct size
of my child but it is just not displaying it. I mulled over my code for
almost a whole day and can't explain that behaviour.
That's why I really appreciate all hints.

Clemens

Bo Thorsen wrote:
> Tuesday 02 January 2007 18:27 Clemens Clausen wrote:
>   
>> got an annoying artifact when adding a custom widget into a layout. My
>> custom widget is a class, derived from QWidget, and in this class (to be
>> exact, in its paintEvent() ), a pixmap is loaded and painted. The
>> widgets size is the same as the size of the pixmap.
>> Now, I don't understand the following behaviour: I construct an instance
>> of my widget in the constructor of the parent (another custom widget,
>> derived from QWidget). The child is painted correctly, if I set no other
>> attributes, childs, layouts or something like that. I want to position
>> this child with a layout ( QHBoxLayout ). In that case, the custom child
>> is not visualized. It seems that the layout knows that something is
>> there, but my widget is not painted. (I also tried to add a QVBoxLayout
>> containing my child and a label. The label was moved in the right way.
>> Therefore, I assume that the layouts know about the size of my little
>> custom widget. )
>>     
>
> It's quite difficult to figure out what the problem is, when you don't send 
> the code for the child widget. However, it sounds like you didn't implement 
> sizeHint() (which should probably just return the size of the image). You 
> should also set the size policy to fixed.
>
>   
>> that does not work:
>>
>>     ParentWidget::ParentWidget( QWidget* parent, Style style, const
>> QColor& color ) :                     CustomWidget( parent, style, color )
>>     {
>>       m_topLayout = new QHBoxLayout( this );
>>       QVBoxLayout* testLayout = new QVBoxLayout();
>>       QLabel* testLabel = new QLabel( "Label", this );
>>       testLayout->addStretch( 1 );
>>       testLayout->addWidget( testLabel );
>>       CustomChild* testChild = new CustomChild( this );
>>       testLayout->addWidget( testChild );         // <- That seems not
>> to be working
>>       testLayout->addStretch( 1 );
>>       m_topLayout->addLayout( testLayout );
>>     }
>>
>> that is working:
>>
>>     ParentWidget::ParentWidget( QWidget* parent, Style style, const
>> QColor& color ) :                     CustomWidget( parent, style, color )
>>     {
>>       CustomChild* testChild = new CustomChild( this );
>>     }
>>     
>
> And that supports the idea that the layout thinks your CustomChild should be 
> shrunk to nothing.
>
>   
>> in the paintEvent() of CustomChild, a pixmap is loaded and painted:
>>     ...
>>     QPainter p(this);
>>     QPixmap arrows.load(":/images/arrows.png");
>>     p.drawPixmap( QRectF( this->x(), this->y(), arrows.width(),
>> arrows.height() ),
>>                               arrows,
>>                              QRectF( 0, 0, arrows.width(),
>> arrows.height() ) );
>>     
>
> Don't use QRectF when QRect will do. Actually, just do
>
> QPixmap arrows(":/images/arrows.png");
> p.drawPixmap(0, 0, arrows);
>
> Inside the paintEvent, you're in the coordinates of this widget, not in global 
> or window coordinates.
>
> Bo.
>
>   

Message 5 in thread

Wednesday 03 January 2007 10:44 Clemens Clausen wrote:
> Of course, I should have mentioned the size. Sorry for that.
> The size of my child class is determined with setFixedSize( QSize(
> arrows.width(), arrows.height() ) ). The size of the child is correct,
> all my outputs were as expected. My second hint to this assumption is
> that the label in the same layout is not placed in the center where I
> expect it to be if my custom child would be infinitesimal or just not
> existent. The label is shifted (as it would be if the layout contains my
> custom widget ). Therefore, I guess, the layout knows the correct size
> of my child but it is just not displaying it. I mulled over my code for
> almost a whole day and can't explain that behaviour.
> That's why I really appreciate all hints.

Fix the paintEvent as I suggested...

Bo.

> Bo Thorsen wrote:
> > Tuesday 02 January 2007 18:27 Clemens Clausen wrote:
> >> got an annoying artifact when adding a custom widget into a layout. My
> >> custom widget is a class, derived from QWidget, and in this class (to be
> >> exact, in its paintEvent() ), a pixmap is loaded and painted. The
> >> widgets size is the same as the size of the pixmap.
> >> Now, I don't understand the following behaviour: I construct an instance
> >> of my widget in the constructor of the parent (another custom widget,
> >> derived from QWidget). The child is painted correctly, if I set no other
> >> attributes, childs, layouts or something like that. I want to position
> >> this child with a layout ( QHBoxLayout ). In that case, the custom child
> >> is not visualized. It seems that the layout knows that something is
> >> there, but my widget is not painted. (I also tried to add a QVBoxLayout
> >> containing my child and a label. The label was moved in the right way.
> >> Therefore, I assume that the layouts know about the size of my little
> >> custom widget. )
> >
> > It's quite difficult to figure out what the problem is, when you don't
> > send the code for the child widget. However, it sounds like you didn't
> > implement sizeHint() (which should probably just return the size of the
> > image). You should also set the size policy to fixed.
> >
> >> that does not work:
> >>
> >>     ParentWidget::ParentWidget( QWidget* parent, Style style, const
> >> QColor& color ) :                     CustomWidget( parent, style, color
> >> ) {
> >>       m_topLayout = new QHBoxLayout( this );
> >>       QVBoxLayout* testLayout = new QVBoxLayout();
> >>       QLabel* testLabel = new QLabel( "Label", this );
> >>       testLayout->addStretch( 1 );
> >>       testLayout->addWidget( testLabel );
> >>       CustomChild* testChild = new CustomChild( this );
> >>       testLayout->addWidget( testChild );         // <- That seems not
> >> to be working
> >>       testLayout->addStretch( 1 );
> >>       m_topLayout->addLayout( testLayout );
> >>     }
> >>
> >> that is working:
> >>
> >>     ParentWidget::ParentWidget( QWidget* parent, Style style, const
> >> QColor& color ) :                     CustomWidget( parent, style, color
> >> ) {
> >>       CustomChild* testChild = new CustomChild( this );
> >>     }
> >
> > And that supports the idea that the layout thinks your CustomChild should
> > be shrunk to nothing.
> >
> >> in the paintEvent() of CustomChild, a pixmap is loaded and painted:
> >>     ...
> >>     QPainter p(this);
> >>     QPixmap arrows.load(":/images/arrows.png");
> >>     p.drawPixmap( QRectF( this->x(), this->y(), arrows.width(),
> >> arrows.height() ),
> >>                               arrows,
> >>                              QRectF( 0, 0, arrows.width(),
> >> arrows.height() ) );
> >
> > Don't use QRectF when QRect will do. Actually, just do
> >
> > QPixmap arrows(":/images/arrows.png");
> > p.drawPixmap(0, 0, arrows);
> >
> > Inside the paintEvent, you're in the coordinates of this widget, not in
> > global or window coordinates.
> >
> > Bo.

-- 
 [ signature omitted ] 

Message 6 in thread

Finally, I got the bug, thank you for the help. It was the call of
drawPixmap() with this->x() and this->y().  Once again, I had blinders
on. That is really embarrassing...

Best Regards, Clemens

Bo Thorsen wrote:
> Wednesday 03 January 2007 10:44 Clemens Clausen wrote:
>   
>> Of course, I should have mentioned the size. Sorry for that.
>> The size of my child class is determined with setFixedSize( QSize(
>> arrows.width(), arrows.height() ) ). The size of the child is correct,
>> all my outputs were as expected. My second hint to this assumption is
>> that the label in the same layout is not placed in the center where I
>> expect it to be if my custom child would be infinitesimal or just not
>> existent. The label is shifted (as it would be if the layout contains my
>> custom widget ). Therefore, I guess, the layout knows the correct size
>> of my child but it is just not displaying it. I mulled over my code for
>> almost a whole day and can't explain that behaviour.
>> That's why I really appreciate all hints.
>>     
>
> Fix the paintEvent as I suggested...
>
> Bo.
>
>   
>> Bo Thorsen wrote:
>>     
>>> Tuesday 02 January 2007 18:27 Clemens Clausen wrote:
>>>       
>>>> got an annoying artifact when adding a custom widget into a layout. My
>>>> custom widget is a class, derived from QWidget, and in this class (to be
>>>> exact, in its paintEvent() ), a pixmap is loaded and painted. The
>>>> widgets size is the same as the size of the pixmap.
>>>> Now, I don't understand the following behaviour: I construct an instance
>>>> of my widget in the constructor of the parent (another custom widget,
>>>> derived from QWidget). The child is painted correctly, if I set no other
>>>> attributes, childs, layouts or something like that. I want to position
>>>> this child with a layout ( QHBoxLayout ). In that case, the custom child
>>>> is not visualized. It seems that the layout knows that something is
>>>> there, but my widget is not painted. (I also tried to add a QVBoxLayout
>>>> containing my child and a label. The label was moved in the right way.
>>>> Therefore, I assume that the layouts know about the size of my little
>>>> custom widget. )
>>>>         
>>> It's quite difficult to figure out what the problem is, when you don't
>>> send the code for the child widget. However, it sounds like you didn't
>>> implement sizeHint() (which should probably just return the size of the
>>> image). You should also set the size policy to fixed.
>>>
>>>       
>>>> that does not work:
>>>>
>>>>     ParentWidget::ParentWidget( QWidget* parent, Style style, const
>>>> QColor& color ) :                     CustomWidget( parent, style, color
>>>> ) {
>>>>       m_topLayout = new QHBoxLayout( this );
>>>>       QVBoxLayout* testLayout = new QVBoxLayout();
>>>>       QLabel* testLabel = new QLabel( "Label", this );
>>>>       testLayout->addStretch( 1 );
>>>>       testLayout->addWidget( testLabel );
>>>>       CustomChild* testChild = new CustomChild( this );
>>>>       testLayout->addWidget( testChild );         // <- That seems not
>>>> to be working
>>>>       testLayout->addStretch( 1 );
>>>>       m_topLayout->addLayout( testLayout );
>>>>     }
>>>>
>>>> that is working:
>>>>
>>>>     ParentWidget::ParentWidget( QWidget* parent, Style style, const
>>>> QColor& color ) :                     CustomWidget( parent, style, color
>>>> ) {
>>>>       CustomChild* testChild = new CustomChild( this );
>>>>     }
>>>>         
>>> And that supports the idea that the layout thinks your CustomChild should
>>> be shrunk to nothing.
>>>
>>>       
>>>> in the paintEvent() of CustomChild, a pixmap is loaded and painted:
>>>>     ...
>>>>     QPainter p(this);
>>>>     QPixmap arrows.load(":/images/arrows.png");
>>>>     p.drawPixmap( QRectF( this->x(), this->y(), arrows.width(),
>>>> arrows.height() ),
>>>>                               arrows,
>>>>                              QRectF( 0, 0, arrows.width(),
>>>> arrows.height() ) );
>>>>         
>>> Don't use QRectF when QRect will do. Actually, just do
>>>
>>> QPixmap arrows(":/images/arrows.png");
>>> p.drawPixmap(0, 0, arrows);
>>>
>>> Inside the paintEvent, you're in the coordinates of this widget, not in
>>> global or window coordinates.
>>>
>>> Bo.
>>>       
>
>   


-- 
 [ signature omitted ]