[Qt-qml] Global mouse coordinates

Surik Sayadyan ssyrik at gmail.com
Wed Sep 15 18:42:18 CEST 2010


I did same. But I want get global coordinates in QML and forgot that
can use QWidget::mapToGlobal().
I made this:

QuickMusic::QuickMusic(QWidget *parent)
    : QWidget(parent,Qt::FramelessWindowHint)
{
    QDeclarativeView *view = new QDeclarativeView(this);
    QHBoxLayout* hl = new QHBoxLayout;
    hl->addWidget(view);
    this->setLayout(hl);

    QUrl url = QUrl::fromLocalFile("./qml/QuickMusic.qml");
    view->setSource(url);

    view->rootContext()->setContextProperty("qm",this);
}

void QuickMusic::moveWindow(int x,int y, int lx, int ly)
{
    QPoint p = mapToGlobal(QPoint(x,y));
    //QPoint p = pos();
    p.setX(p.x() - lx);
    p.setY(p.y() - ly);

    this->move(p);
}

QuickMusic::moveWindow must be Q_INVOKABLE


        Rectangle{
            anchors.top: parent.top
            color: "gray"
            height: 30
            width: parent.width
            MouseArea {
                anchors.fill: parent
                property int px
                property int py

                onPressed: {
                    px = mouse.x
                    py = mouse.y
                }

                onPositionChanged: {
                    qm.moveWindow(mouse.x, mouse.y, px, py);
                }
            }
        }



More information about the Qt-qml mailing list