Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved] QDeclarativeItem geometry
Forum Updated to NodeBB v4.3 + New Features

[solved] QDeclarativeItem geometry

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    imihajlov
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

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

      dmcr

      1 Reply Last reply
      0
      • I Offline
        I Offline
        imihajlov
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dmcr
          wrote on last edited by
          #4

          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.

          dmcr

          1 Reply Last reply
          0
          • I Offline
            I Offline
            imihajlov
            wrote on last edited by
            #5

            [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!

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dmcr
              wrote on last edited by
              #6

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

              dmcr

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved