Solved Signal from c++ for messagedialog on Qml
-
I have mi signal in to c++
Backend::Backend(QObject *parent) : QObject(parent) { //m_sSettingsFile = QApplication::applicationDirPath().left(1) + ":/demosettings.ini"; //QSettings settings(m_sSettingsFile, QSettings::NativeFormat); settings = new QSettings("$HOME/.config/MySoft.ini",QSettings::IniFormat); // settings->setValue("ipcasa","192.168.1.8"); if (settings->contains("ipcasa")) { ip = settings->value("ipcasa").toString(); }else { qDebug() << "insertr your Ip"; emit statusChanged(" You must insert to Ip"); }
but I can't activate a messagedialog to warn the user that he has to insert the ip, I'm trying to do something like that but I have no idea how I can do:
import QtQuick 2.0 import QtQuick.Controls 2.5 import QtQuick.Layouts 1.12 import io.qt.Backend 1.0 import QtQuick.Dialogs 1.3 ApplicationWindow { id: window width: 500 height: 500 visible:true Backend{ id: backend onStatusChanged: { messageDialog.setVisible(true); messageDialog.text = newStatus } } MessageDialog{ id: messageDialog title: "Attention" onAccepted: { console.log("message for insert ip start.") // Qt.quit() } // Component.onCompleted: visible = true }
Can you teach me how I could do?
-
Code looks OK. You do add
backend
to root context property of your QML engine, right?My suspicion is that you don't see the signal in QML because it is fired too early. You emit the signal in Backend constructor, so it is sent before you add it to QML engine, so also before there is
Connections
element.So, I think you should defer your initial status check for later, for example using QTimer::singleShot().
-
you're right I'm checking before the backend class is instantiated, but to instantiate it I need the user to enter the ip address of the machine first, I'm thinking how to do it, I know I can't get it done from the backend class, I should do it do to another class? I'm a bit confused
@sierdzio said in Signal from c++ for messagedialog on Qml:
Code looks OK. You do add
backend
to root context property of your QML engine, right?My suspicion is that you don't see the signal in QML because it is fired too early. You emit the signal in Backend constructor, so it is sent before you add it to QML engine, so also before there is
Connections
element.So, I think you should defer your initial status check for later, for example using QTimer::singleShot().
-
In that case it sounds like you should introduce an uninitialized state to your backend.
bool mInitialized = false;
. Then add your backend to QML as usual, but make sure to check initialization in each method which is called before your user enters the IP address, like so:void Backend::someMethod() { if (mInitialized == false) return; // Your normal code }
Then, when user does enter the IP address, call you method, create QSettings etc. and set
mInitialized = true;
. -
tanks i am following your way
@sierdzio said in Signal from c++ for messagedialog on Qml:
In that case it sounds like you should introduce an uninitialized state to your backend.
bool mInitialized = false;
. Then add your backend to QML as usual, but make sure to check initialization in each method which is called before your user enters the IP address, like so:void Backend::someMethod() { if (mInitialized == false) return; // Your normal code }
Then, when user does enter the IP address, call you method, create QSettings etc. and set
mInitialized = true;
.