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

How to push a button on a bottom of a QWidget window from a overlayed QML window ?



  • Hello.

    How do you push a button on a bottom of a QWidget window from a overlayed QML window ? I have two windows in my project based on Widget. One is a QWidget Window and the other is a QML window. I overlay the QML window on the QWidget window. I mean a z-position of the QML window is higher than a z-position of QWidget window. There is a button on the QWidget window. And the QML Window color is transparent.
    As we can see the button, we may be able to push the button on the QML Window.
    But I don't know how to push the button. I tried to use some options like "flags:WindowTransparentForInput".But I couldn't push the button.

    The following is my code.

    code_text

    main.qml 
    ・・・・
    import QmlChart 1.0
    Window {
        property int iDefineScreenNo:0
        id:window
        x:screen.virtualX
        y:screen.virtualY
        width:1368
        height:912
        visible: true
        title: qsTr("QML Window")
        color: "transparent"
        Rectangle {
            anchors.fill: parent
            border.color: "#14191D"
            border.width: 5
            color: "transparent"
        }
        //flags: Qt.FramelessWindowHint //Qt.WindowTransparentForInput
        //flags: Qt.WA_TranslucentBackground
        screen:Qt.application.screens[iDefineScreenNo]
    
        QmlChart{} →A connection class between the main.qml and the Chart class
    }
    
    /////////////////////////////////////////
    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/qmlchart.cpp
    #include <QObject>
    #include "chart.h"
    
    class QmlChart : public QObject
    {
    public:
    	QmlChart(QObject *parent = nullptr);
    
    private:
    	Chart m_chart; /
    };
    
    //
    QmlChart::QmlChart(QObject *parent) : QObject(parent)
    {
    	//Display the chart
    	m_chart.setVisible(true);
    }
    
    /////////////////////////////////////////////////////////////
    chart.h/chart.cpp
    namespace Ui {
    class Chart;
    }
    
    class Chart : public QWidget
    {
    	Q_OBJECT
    
    public:
    	explicit Chart(QWidget *parent = nullptr);
    	~Chart();
    
    public slots:
    	void buttonClicked();
    
    private:
    	Ui::Chart *ui;
    	QScreen*         screen;
    	QPushButton *m_button;
    };
    
    //
    Chart::Chart(QWidget *parent) :
    	QWidget(parent),
    	ui(new Ui::Chart)
    {
    	ui->setupUi(this);
    	this->show();
    
    ・・・・・・
    	// signal and slot
    	connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(buttonClicked()));
    
    ・・・・
    }

  • Moderators

    @morita said in How to push a button on a bottom of a QWidget window from a overlayed QML window ?:

    I tried to use some options like "flags:WindowTransparentForInput".But I couldn't push the button.

    the operating system send input events to the active/focused widget, but never passes them on to the underlaying window if the event is not consumed.
    Pretty clear why not when thinking of the reason not to do so.

    Since you want to send an event to a different window in your application you can bypass the OS and simply send a generated event yourself directly to the window widget.

    QPoint screenPos(...,....);
    if( QWidget *receiver = windowWidget->childAt(windowWidget->mapFromGlobal(screenPos)) )
    {
        QPoint localPos = receiver->mapFromGlobal(screenPos);
        // press event
        QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress, localPos, screenPos, Qt::LeftButton, Qt::LeftButton,  Qt::NoModifier);
        QCoreApplication::postEvent(receiver, event);
       // release event
        QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonRelease, localPos, screenPos, Qt::LeftButton, Qt::LeftButton,  Qt::NoModifier);
        QCoreApplication::postEvent(receiver, event);
    }
    


  • @raven-worx

    Thank you for replying.

    First of all, Do I have to code Your code in Chart Class ? Is it correct ? And are values of arguments in ScreenPos the signal values of cordinates by mouseEvent of QML Window?


  • Moderators

    @morita said in How to push a button on a bottom of a QWidget window from a overlayed QML window ?:

    First of all, Do I have to code Your code in Chart Class ? Is it correct ?

    You can put it into a custom QObject and add a invokable method which executes this code reuseable.

    And are values of arguments in ScreenPos the signal values of cordinates by mouseEvent of QML Window?

    use the items mapToGlobal() function to map it to screen coordinates.



  • @raven-worx

    Thank you so much . I finally make it .


Log in to reply