[Qt-interest] nesting QGraphicsview inside QWidget

Martin Schröer gordon at gordonswelt.de
Sun Mar 8 19:45:33 CET 2009


Ok, i solved the "does not resize with layout" issue with some code i 
found on the internet.
It is basically done by subclassing the GraphicsView and reimplementing 
resizeEvent():

void resizingGraphicsView::resizeEvent(QResizeEvent *event)
{
    ((QGraphicsWidget*) items().at(0))->setGeometry(0, 0, 
event->size().width(), event->size().height());
    setSceneRect(0, 0, event->size().width(), event->size().height());
}

This little piece of magic makes the view and all of the items in it 
scale along with the layout it is nested in.

Martin Schröer schrieb:
> Well, i should confess that i have a Phonon media object sitting in the 
> GraphicsView scene ...
> maybe the question should therefore read: "How do i achieve auto scaling 
> the scene containing
> a phonon rendered video according to the layout set for the containing 
> widget".
>
> Another question, different, but also concerning this is - how do i 
> render a button on top of
> the video content inside the GraphicsView object ? Like having a 'start' 
> or 'play' button sitting
> on top ...
>
>
> Martin
>
> Martin Schröer schrieb:
>   
>> Yes, adding
>>
>>     QGridLayout *mainLayout = new QGridLayout;
>>     mainLayout->addWidget(&view);
>>     setLayout(mainLayout);
>>     view.show();
>>
>> did the trick.
>>
>> Now, there's another problem:
>> while now being embedded in a widget, the view refuses to dynamically 
>> change size,
>> it displays its contents ignoring the widget size.
>>
>> view.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
>> and friends also chage nothing.
>>
>>
>>
>> Luc Devallonné schrieb:
>>   
>>     
>>> Martin,
>>>
>>> The problem is that the QGraphicsView is no added to any layout, may
>>> want to try something like
>>>
>>> layout()->addWidget(&view) before the show() in the init()
>>>
>>> Luc
>>>
>>> On Sun, Mar 8, 2009 at 4:40 PM, Martin Schröer <gordon at gordonswelt.de> wrote:
>>>   
>>>     
>>>       
>>>> Here we go:
>>>>
>>>> the GraphicsView is nested inside imemMediaWidget class, which is used in
>>>> the main app:
>>>>
>>>>   mediaWid = new imemMediaWidget();
>>>>     QGridLayout *mainLayout = new QGridLayout;
>>>>   mainLayout->addWidget(loadButton);
>>>>   mainLayout->addWidget(mediaWid, 1, 0, 1, 2);
>>>>   // mainLayout->addWidget(videoWidget, 1, 0);
>>>>   // mainLayout->addWidget(mediaWid, 1, 1);
>>>> mainLayout->addWidget(infoButton, 2, 0);
>>>>   mainLayout->addWidget(timeLabel, 2, 1);
>>>>   mainLayout->addWidget(infoLabel,3, 0, 1, 2);
>>>>   setLayout(mainLayout);
>>>>
>>>> the widget itself:
>>>>
>>>> <header>
>>>>
>>>> class imemMediaWidget : public QWidget
>>>> {
>>>>   Q_OBJECT
>>>>   QGraphicsScene scene;
>>>>   QGraphicsView view;
>>>>  public:
>>>>   imemMediaWidget(QWidget *parent = 0);
>>>>  protected:
>>>>  private:
>>>>   void init();
>>>>   CustomProxy *proxy;
>>>>  private slots:    };
>>>>
>>>> plus it's implementation ( stripped ):
>>>>
>>>> #include "imemMediaWidget.h"
>>>>
>>>>
>>>> imemMediaWidget::imemMediaWidget(QWidget *parent) : QWidget(parent)
>>>> {
>>>>   init();
>>>> }
>>>>
>>>> void imemMediaWidget::init()
>>>> {
>>>>   view.setScene(&scene);
>>>>     scene.setSceneRect(scene.itemsBoundingRect());
>>>>   view.setBackgroundBrush(QPixmap("No-Ones-Laughing-3.jpg"));
>>>>   view.show();
>>>> }
>>>>
>>>> Luc Devallonné schrieb:
>>>>     
>>>>       
>>>>         
>>>>> Strange behavior, could you post some code that demonstrates the problem ?
>>>>>
>>>>> Luc
>>>>>
>>>>> On Sun, Mar 8, 2009 at 4:14 PM, Martin Schröer <gordon at gordonswelt.de>
>>>>> wrote:
>>>>>
>>>>>       
>>>>>         
>>>>>           
>>>>>> Hmm ... when i try to do so ( i have some graphicsview code inside a
>>>>>> QWidget
>>>>>> class implementation), it shows the GraphicsView content outside of the
>>>>>> window containing the widget in a own window ... ?
>>>>>>
>>>>>> Luc Devallonné schrieb:
>>>>>>
>>>>>>         
>>>>>>           
>>>>>>             
>>>>>>> Martin,
>>>>>>>
>>>>>>> Actually QGraphicsView is a QWidget (QAbstractScrollArea -> QFrame ->
>>>>>>> QWidget), and as such can be layout as any other QWidget.
>>>>>>>
>>>>>>> Luc
>>>>>>>
>>>>>>> On Sun, Mar 8, 2009 at 3:51 PM, Martin Schröer <gordon at gordonswelt.de>
>>>>>>> wrote:
>>>>>>>
>>>>>>>
>>>>>>>           
>>>>>>>             
>>>>>>>               
>>>>>>>> Is it possible to have a QGraphicsview nested inside a QWidget, so
>>>>>>>> the resulting Widget could be placed, used and layouted like any other
>>>>>>>> widget ?
>>>>>>>>
>>>>>>>> Martin
>>>>>>>> _______________________________________________
>>>>>>>> Qt-interest mailing list
>>>>>>>> Qt-interest at trolltech.com
>>>>>>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>             
>>>>>>>>               
>>>>>>>>                 
>>>>>>> _______________________________________________
>>>>>>> Qt-interest mailing list
>>>>>>> Qt-interest at trolltech.com
>>>>>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>>>>>
>>>>>>>
>>>>>>>           
>>>>>>>             
>>>>>>>               
>>>>>>         
>>>>>>           
>>>>>>             
>>>>> _______________________________________________
>>>>> Qt-interest mailing list
>>>>> Qt-interest at trolltech.com
>>>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>>>
>>>>>       
>>>>>         
>>>>>           
>>>>     
>>>>       
>>>>         
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at trolltech.com
>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>   
>>>     
>>>       
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>   
>>     
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>   




More information about the Qt-interest-old mailing list