[solved] QDeclarativeItem geometry



  • I'm trying to wrap a QWidget-based class for use in QML. I use class derived from QDeclarativeItem and override method geometryChanged in it:
    @
    class PlotWidget : public QDeclarativeItem
    {
    Q_OBJECT
    public:
    PlotWidget(QDeclarativeItem *parent = 0);
    protected:
    void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
    private:
    class Impl;
    std::auto_ptr<Impl> impl;
    };
    @

    Inside the class PlotWidget I have a QwtPlot object (QwtPlot is a QWidget-based class, I've tried QPushButton also) and QGraphicsProxyWidget:
    @
    class PlotWidget::Impl
    {
    PlotWidget *that;
    public:
    QwtPlot *plot;
    //QPushButton *button;
    QGraphicsProxyWidget *proxy;

    Impl(PlotWidget *that):
        that(that),
        plot(new QwtPlot(0)),
        //button(new QPushButton(0)),
        proxy(new QGraphicsProxyWidget(that))
    {
        proxy->setWidget(plot);
        //proxy->setWidget(button);
    }
    
    ~Impl()
    {
        delete proxy;
        //delete button;
        delete plot;
    }
    

    };

    PlotWidget::PlotWidget(QDeclarativeItem *parent) :
    QDeclarativeItem(parent),
    impl(new Impl(this))
    {
    }

    void PlotWidget::geometryChanged(const QRectF &newGeometry, const QRectF &)
    {
    qDebug() << newGeometry;
    impl->proxy->setGeometry(newGeometry);
    }
    @

    My QML code is the folliwing:
    @
    Rectangle {
    width: 640
    height: 480
    PlotWidget {
    id: plot
    //anchors.centerIn: parent
    x:10
    y:10
    width: 400
    height: 400
    }
    }
    @

    The problem is the position of the widget. I set it to (10, 10), and it comes to geometryChanged as (10, 10) too, but the widget is located at (20, 20) in the window (I've checked it with a screen ruler). The size of widget is ok - it is 400x400.
    Who multiplies position coordinates by 2? How can I make it set real coordinates?



  • Hello,
    you didn't put the code for drawing the widget thougth ?



  • [quote author="dmcr" date="1338447214"]you didn't put the code for drawing the widget thougth ?[/quote]
    Which drawing code do you mean? The plot widget is drawn correctly, but positioned incorrectly.



  • Well i've read that too fastly......
    In fact you don't need to setGeometry for your PlotWidget.
    @impl->proxy->setGeometry(newGeometry);@
    It has to have (0,0) coordinates in the DeclarativeItem which is embedding it.



  • [quote author="dmcr" date="1338463579"]Well i've read that too fastly......
    In fact you don't need to setGeometry for your PlotWidget.
    @impl->proxy->setGeometry(newGeometry);@

    It has to have (0,0) coordinates in the DeclarativeItem which is embedding it.[/quote]
    Oh thank you! Now widget positioning works fine!



  • Nope :)
    then you can put [solved] in in the title of the post :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.