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

Connect two QQuickWidgets



  • I have two separate QQuickWidgets and I am trying to connect them but nothing seams to work:

    C++:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->quickViewA->setSource(QUrl::fromLocalFile("a.qml"));
        ui->quickViewB->setSource(QUrl::fromLocalFile("b.qml"));
    
        //QML connect
        //try 1
        connect(  ui->quickViewA, SIGNAL( foo(  QVariant )),  ui->quickViewB, SLOT( foo( QVariant )));
        //try 2
        connect(  ui->quickViewA, SIGNAL( foo(  int )),  ui->quickViewB, SLOT( foo( int )));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    a.qml:

    import QtQuick 2.4
    
    Rectangle { 
    	id: root
    	signal foo(int id)
    	MouseArea {
    		anchors.fill: parent
    		onClicked: { 
    			root.foo(42)
    		}
    	}
    	color:"red"
    }
    

    b.qml:

    import QtQuick 2.4
    
    Rectangle { 
    	function foo(id){ console.log(id) }
    }
    

    In both cases it can't find the signal.
    I am doing something wrong or is it just not possible to do it this way?



  • Well got it to work:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->quickViewA->setSource(QUrl::fromLocalFile("a.qml"));
        ui->quickViewB->setSource(QUrl::fromLocalFile("b.qml"));
    
        QObject* a = (QObject*)(ui->quickViewA->rootObject());
        QObject* b = (QObject*)(ui->quickViewB->rootObject());
    
        connect(  a, SIGNAL( foo(  QVariant )),  b, SLOT( foo( QVariant )));
    }
    

    a.qml:

    import QtQuick 2.4
    
    Rectangle { 
    	id: root
    	signal foo(var id) // int changed to var
    	MouseArea {
    		anchors.fill: parent
    		onClicked: { 
    			root.foo(42)
    			console.log(42) 
    		}
    	}
    	color:"red"
    }
    

Log in to reply