Results 1 to 7 of 7

Thread: QML: Move a frameless window by dragging

  1. #1
    Join Date
    Apr 2013
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows

    Default QML: Move a frameless window by dragging

    I have a frameless QQuickWindow, and I want to move it with the mouse by dragging. Before trying in my big application, I have created a simple test application to try what I found here, using cursor position from C++ class to avoid problems from QML:
    http://www.tickanswer.com/solved/539...jiggles-in-qml

    But I failed with the code below. When I press over the red RECT and move the mouse, my yellow rect (root RECT) moves, but only inside the original size it had (in this case, 500x500)... What am I doing wrong?

    Thanks in advance


    In my main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QtQuickControlsApplication a(argc, argv);
    4.  
    5. QQuickView* pView = new QQuickView();
    6.  
    7. CursorPosProvider mousePosProvider;
    8. pView->rootContext()->setContextProperty("mousePosition", &mousePosProvider);
    9. pView->setSource(QUrl("qrc:/Test.qml"));
    10. pView->setFlags(Qt::FramelessWindowHint);
    11. pView->show();
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Test.qml:

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle {
    4. id: myWindow
    5. width: 500; height: 500
    6. color: "yellow"
    7.  
    8. Rectangle {
    9. anchors.centerIn: parent
    10. width: 200; height: 200
    11.  
    12. color: "red"
    13.  
    14. MouseArea {
    15. id: titleBarMouseRegion
    16. property var clickPos
    17. anchors.fill: parent
    18.  
    19. onPressed: clickPos = { x: mousePosition.cursorPos().x, y: mousePosition.cursorPos().y }
    20.  
    21. onPositionChanged: {
    22. myWindow.x = mousePosition.cursorPos().x - clickPos.x
    23. myWindow.y = mousePosition.cursorPos().y - clickPos.y
    24. }
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    cursorprovider.h:

    Qt Code:
    1. #ifndef CURSORPOSPROVIDER_H
    2. #define CURSORPOSPROVIDER_H
    3.  
    4. #include <QObject>
    5. #include <QPointF>
    6. #include <QCursor>
    7.  
    8. class CursorPosProvider : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit CursorPosProvider(QObject *parent = nullptr) : QObject(parent)
    13. {
    14. }
    15. virtual ~CursorPosProvider() = default;
    16.  
    17. Q_INVOKABLE QPointF cursorPos()
    18. {
    19. return QCursor::pos();
    20. }
    21. };
    22.  
    23. #endif // CURSORPOSPROVIDER_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML: Move a frameless window by dragging

    You are moving content, not the window.

    I.e. "myWindow" is the root content item of your QQuickView, but that view is the window.

    You also need to expose the view object to QML and set its x/y properties.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2013
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML: Move a frameless window by dragging

    I have changed the structure, I have used a QQuickWidget with a QML inside, and now I have what I wanted. Here is my code in case anyone needs something similar


    main.cpp

    ...
    MovableWidget *view = new MovableWidget;
    view->setSource(QUrl("qrc:/Test.qml"));
    view->setWindowFlags(Qt::FramelessWindowHint);
    view->show();
    ...

    Test.qml

    import QtQuick 2.0

    Rectangle {
    id: myWindow
    width: 500; height: 500
    color: "yellow"

    Rectangle {
    anchors.centerIn: parent
    width: 200; height: 200

    color: "red"
    }
    }



    MovableWidget.cpp

    #include "movableWidget.h"

    #include <QMouseEvent>

    // ************************************************** **************************
    MovableWidget::MovableWidget(QWidget *parent)
    : QQuickWidget(parent),
    m_previousPos(0,0)
    {
    installEventFilter(this);
    }

    // ************************************************** **************************
    bool MovableWidget::eventFilter(QObject *obj, QEvent *event)
    {
    if (event->type() == QEvent::MouseButtonPress)
    {
    m_previousPos = QCursor:os();
    }
    else if (event->type() == QEvent::MouseMove)
    {
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);

    if(mouseEvent->buttons() == Qt::LeftButton)
    {
    QPoint offset = m_previousPos - QCursor:os();
    m_previousPos = QCursor:os();
    move(pos() - offset);
    }
    }

    return false;
    }
    Last edited by ddonate; 22nd November 2016 at 11:07.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML: Move a frameless window by dragging

    Obviously that had nothing to do with using a QQuickWidget instead of a QQuickView.

    Just in case anyone is reading this and wondering.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2013
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML: Move a frameless window by dragging

    Thanks anda_skoa,

    I tried with QQuickView but I didn't get a good solution... So I decided to change and this works for me...

    Cheers

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML: Move a frameless window by dragging

    Quote Originally Posted by ddonate View Post
    I tried with QQuickView but I didn't get a good solution... So I decided to change and this works for me...
    Not sure I understand, tatt would be exactly the same code, wouldn't it?

    Cheers,
    _

  7. #7
    Join Date
    Apr 2013
    Posts
    58
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QML: Move a frameless window by dragging

    Quote Originally Posted by anda_skoa View Post
    Not sure I understand, tatt would be exactly the same code, wouldn't it?

    Cheers,
    _
    You are right, I tried again the first option, with QQuickView, and it also works properly... I don't know what I was doing wrong in the previous try...

    Thanks a lot!

Similar Threads

  1. Frameless Widget move
    By mouni in forum Newbie
    Replies: 2
    Last Post: 11th August 2016, 13:25
  2. Problem in dragging and move QDockWidget
    By stilgar in forum Qt Programming
    Replies: 1
    Last Post: 18th April 2012, 10:33
  3. Move Frameless Dialog
    By deepal_de in forum Qt Programming
    Replies: 7
    Last Post: 6th June 2011, 08:35
  4. Frameless Window Helper
    By nish in forum Qt-based Software
    Replies: 6
    Last Post: 20th February 2011, 07:28
  5. Resize handling of frameless window
    By Peppy in forum Qt Programming
    Replies: 5
    Last Post: 2nd June 2010, 20:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.