Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to express a process which a map is moved while pressing the mouse on a QWidget including a map from an overlyaed QML Window



  • Hello. How do you express move process which a map is moved while pressing the mouse on a QWidget including a map library from an overlayed QML Window. I have two windows in my project. One is a QML Window and the other is a QWidget window. The QML Window is overlayed on the QWidget Window. The QWidget window display a map using a map library and there are some items on a QML Window. As I set a transparent color to a QML Window, We can see map of a QWidget Window. I could make click-process in my project. When I click the QML Window, I send a signal of x-y-coordinate to QWidget.And receving x-y-coordinate in a QWidget, a QWidget send QMouseEvent including them to QcoreApplication. In General, if we don't make this process, the map of QWidget Window can't be reacted when we click.
    By the way, a map library has a function we can move a mouse while pressing. if I do the behavior, the map displays other positions like scrolling up. But I don't know how to make the process.
    I tried to make using a way like the above process. But unfortunately, it doesn't work well. Do you have any tips ? My sample is below.

    /////////////////////main.qml /////////////////////////
    ・・・・
    import QmlChart 1.0
    Window {
    id:window
    width:1368
    height:912
    visible: true
    color: "transparent"
    ItemMain {
    id:itemOnWindow
    anchors.fill: parent
    }

    QmlChart{} →A connection class between the main.qml and the Chart class
    

    }

    ////////////////////ItemMain.qml///////////////////////////
    import・・・・
    ItemMainForm {

    Connections {
        target:mouseArea
    
        onClicked: {
           //changed  to absolute coordinate  
            var point = mouseArea.mapToGlobal(mouse.x, mouse.y)
           qmlChart.fnQmlWindowClicked(point.x, point.y)
        }
        
        onPositionChanged: {
            globalPositionChanged(itemMain, mouse)
        }
    
        Component.onCompleted: {
            globalPositionChanged.connect(handlePositionChange)
        }
        function handlePositionChange(item, position) {
            var localPos = toLocalePosition(itemMain, item, position)
            if (localPos) {
                //console.log("qml press:", localPos.x, localPos.y)
               //Signals moved coordinates while pressing a mouse  to qmlChart Class
                qmlChart.fnQmlWindowHandlePositionChanged(localPos.x, localPos.y)
            }
        }
    }
    
    function toLocalePosition(toItem, fromItem, position) {
        // return the local position if inside item, or null if outside
        var localPos = toItem.mapFromItem(fromItem, position.x, position.y)
        if (localPos.x >= 0
                && localPos.y >= 0
                && localPos.x <= toItem.width
                && localPos.y <= toItem.height) {
            return localPos
        }
        return null
    }
    

    }
    //////////////ItemMainForm.ui.qml////////////////
    ・・・・
    Item {
    id: itemMain
    property alias itemMain: itemMain
    signal globalPositionChanged(var item, var position)
    width: 1368
    height: 912
    z: 0
    property alias mouseArea: mouseArea
    property alias qmlChart: qmlChart

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        z: 0
    }
    
    //a map element
    QmlChart {
        id: qmlChart
    }
    

    }
    ///////////////////////main.cpp//////////////////
    ・・・・
    #include "widget/qmlchart.h"
    int main(int argc, char *argv[])
    {
    qmlRegisterType<QmlChart>("QmlChart", 1, 0, "QmlChart");
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QApplication app(argc, argv);
    ・・・・・
    

    }

    /////////////////////////////////qmlchart.h/////////////////////////////
    #include <QObject>
    #include "chart.h"

    class QmlChart : public QObject
    {
    public:
    QmlChart(QObject *parent = nullptr);

    private:
    Chart m_chart; /
    };

    /////////////////////////////////qmlchart.cpp////////////////////////////
    QmlChart::QmlChart(QObject *parent) : QObject(parent)
    {
    //Display the chart
    m_chart.setVisible(true);
    }
    void QmlChart::fnQmlWindowClicked(int iClickedX, int iClickedY)
    {
    m_chart.setMouseClickedEvent(iClickedX,iClickedY);
    }
    void QmlChart::fnQmlWindowHandlePositionChanged(int iClickedX, int iClickedY)
    {
    m_chart.setMousePressAndHoldEvent(iClickedX,iClickedY);
    }
    /////////////////////////////////chart.h/////////////////////////////
    chart.h
    namespace Ui {
    class Chart;
    }

    class Chart : public QWidget
    {
    Q_OBJECT

    public:
    explicit Chart(QWidget *parent = nullptr);
    ~Chart();
    void setMouseClickedEvent(int iClickedX, int iClickedY);
    void setMouseHandlePositionChangedEvent(int iClickedX, int iClickedY);
    public slots:

    private:
    Ui::Chart *ui;
    ・・・・・・・
    };

    /////////////////////////////////chart.cpp/////////////////////////////
    Chart::Chart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Chart)
    {
    ui->setupUi(this);
    this->show();

    ・・・・・・

    ・・・・
    }

    //I can click !
    void Chart::setMouseClickedEvent(int iClickedX, int iClickedY) {

    qDebug() << "iClickedX:" << iClickedX << " iClickedY:" << iClickedY;
    
    QPoint screenPos(iClickedX,iClickedY) ;
    if( QWidget *receiver = this->childAt(this->mapFromGlobal(screenPos)))
    {
    	QPoint localPos = receiver->mapFromGlobal(screenPos);
    	// press event
    	QMouseEvent *pressEvent = new QMouseEvent(QEvent::MouseButtonPress, localPos, screenPos, Qt::LeftButton, Qt::LeftButton,  Qt::NoModifier);
    	QCoreApplication::postEvent(receiver, pressEvent);
    	// release event
    	QMouseEvent *ReleaseEvent = new QMouseEvent(QEvent::MouseButtonRelease, localPos, screenPos, Qt::LeftButton, Qt::LeftButton,  Qt::NoModifier);
    	QCoreApplication::postEvent(receiver, ReleaseEvent);
    }
    

    }

    //I can't move a map while pressing !
    void Chart::setMouseHandlePositionChangedEvent(int iClickedX, int iClickedY) {

    qDebug() <<  "HandlePositionChanged " << "iClickedX:" << iClickedX << " iClickedY:" << iClickedY;
    
    QPoint screenPos(iClickedX,iClickedY) ;
    if( QWidget *receiver = this->childAt(this->mapFromGlobal(screenPos)))
    {
    	QPoint localPos = receiver->mapFromGlobal(screenPos);
    	// press event
    	QMouseEvent *pressEvent = new QMouseEvent(QEvent::MouseButtonPress, localPos, screenPos, Qt::LeftButton, Qt::LeftButton,  Qt::NoModifier);
    	QCoreApplication::postEvent(receiver, pressEvent);
    	// release event
    	QMouseEvent *ReleaseEvent = new QMouseEvent(QEvent::Move, localPos, screenPos, Qt::LeftButton, Qt::LeftButton,  Qt::NoModifier);
    	QCoreApplication::postEvent(receiver, ReleaseEvent);
    }
    

    }


Log in to reply