2
votes

Hello I have problem with connect QML signal with Qt slot when I use QQuickView here is my main.cpp:

ModelValueReceivers *mvr;
mvr = new ModelValueReceivers();
QQuickView view;
view.setSource(QUrl(("qrc:///Main.qml")));
QQuickView loginScreenView;
loginScreenView.setSource(QUrl(("qrc:///LoginScreen.qml")));
QObject *loginScreen = loginScreenView.rootObject();
QObject::connect(loginScreen, SIGNAL(qmlSignal(QString, int)), mvr->valuesReceivers[U1], SLOT(start(QString, int)));
view.show()

In ModelValueReceivers is slot public slots: void start(QString ipAddress, int tcpPort);

In LoginScreen.qml I have signal: signal qmlSignal(string addressIP, int portTCP)

and emit is onClicked

onClicked: {
            console.log("onClicked");
            qmlSignal(ipTextField.text , parseInt(tcpPortTextField.text))
        }

In console I can see log "onClicked" but the slot doesn't start.

in Main.qml I have:

Rectangle {
id: screen; width: 320; height: 480;
color: "#ffffff"
StackView{
    id: sv
    property StackView sv: sv 
    initialItem: Qt.resolvedUrl("qrc:///LoginScreen.qml");
} }`
2
Ok to simplier example i give this: QObject::connect(loginScreen, SIGNAL(exitApp()), &ctrl, SLOT(on_closeAppButton_clicked())); I have slot int ctrl and the same signal in qml fileSzymson
Oh, I got it. Than the sample is not sufficient. We don't know what kind of receiver is that. And no question!Alexander V
I have main.qml where InitialItem is LoginScreen in LoginScreen i have button Login which should be connected to slot in Qt class, but i don't know how in main.cpp get connect LoginScreen signal with Qt class slot.Szymson
We cannot see in ModelValueReceivers and we don't know how the slot is defined.Alexander V
Do you really have the signal qmlSignal in the root object in LoginScreen.qml or that signal is emited from some child object? Do you see some debug warnings?Orest Hera

2 Answers

1
votes

Unless LoginScreen.qml is a singleton, I think, you are using 2 different objects of LoginScreen.qml, one in main.cpp and other in your Main.qml.


Update:

May be, you could do this:

Main.qml

...
signal signalFromLogin(string, int);

property Component loginScreen : LoginScreen {
   onQmlSignal: signalFromLogin(addressIP, portTCP);
}
StackView{
  id: sv
  property StackView sv: sv 
  initialItem: loginScreen;
}

now, in your main.cpp

QQuickView view;
view.setSource(QUrl(("qrc:///Main.qml")));
QObject* mainScreen = view.rootObject();
QObject::connect(maininScreen, SIGNAL(signalFromLogin(QString, int)), mvr->valuesReceivers[U1], SLOT(start(QString, int)));

please note, I have not tested this code.

0
votes

Ok I already have done it with TabView and every view/file insert into separate tab, so now I have access to all tabs from Main. There is the example: TabView

Insert file to tab : component: Qt.createComponent("qrc:///LoginScreen.qml")

In main.cpp I add: QQmlContext* ctx = view.rootContext(); ctx->setContextProperty("controller", &ctrl); and in every qml file I can connect signal QML to Qt Slot and Qt Signal to Qml Slot:

Connections{
target: controller
onSendValue:{
    u1TextField.text=String(value)

    }
}



onClicked: {
            console.log("Button Clicked")
            controller.on_closeAppButton_clicked()
        }