I'm facing this problem while trying to switch from a ApplicationWindow to another.
I have this first Window called login.qml which opens the main.qml through the following signal:
onIdAutenticadoChanged: {
if(idAutenticado > 0){
console.log("login.qml: Autenticado ID: " + idAutenticado);
LoginController.abrirMain();
close();
} else if(idAutenticado == 0){
senhaInput.text = "";
console.log("Falha na autenticação: Usuário e/ou senha inválidos.");
lblMsgErro.text = "Usuário e/ou senha inválidos.";
lblMsgErro.visible = true;
loginInput.focus = true;
}
}
The slot LoginController.abrirMain() will load and show the main.qml
void QLoginController::abrirMain()
{
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("MainController", new QMainController(0,m_autenticado));
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
QQuickWindow* window = qobject_cast<QQuickWindow*>(engine.rootObjects().at(0));
window->showFullScreen();
}
The problem is: main.qml is not being shown.
//main.qml
ApplicationWindow {
id: mainWindow
visible: true
modality: "WindowModal"
visibility: "FullScreen"
color: "#09435b"
}
Question: Am I using the right approach to open this new window? If not, how should I do this?
Because you create the engine on stack :), which will be deleted automatically before abrirMain() returns and hence destroying the window too.
You should create engine on heap. Something like this:
// 'this' will become engine's parent and will automaticlaly delete engine when 'this' is deleted
QQmlApplicationEngine *engine = new QQmlApplicationEngine(this);
engine->rootContext()->setContextProperty("MainController", new QMainController(0,m_autenticado));
engine->load(QUrl(QStringLiteral("qrc:///main.qml")));
QQuickWindow* window = qobject_cast<QQuickWindow*>(engine->rootObjects().at(0));
window->showFullScreen();
Related
I have a timer in qml (view in StackView) which I try to start from C++ code, calling javascript function. But my timer is never triggered. What Am I doing wrong? My flow is a.qml -> b.qml (on Button clicked)
File b.qml :
Item {
function connectionConfirmed() {
myTimer.start()
console.log("started timer")
}
Timer {
interval: 1000; running: false; repeat: false
id: myTimer
onTriggered: {
console.log("timer triggered")
}
}
}
file main.qml:
ApplicationWindow {
id: root
visible: true
width: 320
height: 530
StackView {
id: stackView
initialItem: "qrc:/a.qml"
anchors.fill: parent
}
}
file a.qml
MouseArea{
anchors.fill: parent
onClicked: {
stackView.push("qrc:/b.qml")
}
}
C++ part:
connect(&myObjectInstance, &X::somethingHappend, this, [this](){
QQmlComponent component(&engine_, "qrc:/b.qml");
QObject *obj = component.create();
QVariant returnedValue;
QMetaObject::invokeMethod(obj, "connectionConfirmed",
Q_RETURN_ARG(QVariant, returnedValue));
delete obj;
});
Output is:
started timer
When I tried to set running: true timer is triggered, does it mean that I am not able to start the timer from JS function?
The problem is caused because you are assuming that the component created on the C++ side is the same that is created on the QML side. The .qml file is the source code, when you invoke it, they generate a different object each time.
Explanation of the behavior:
connect(&myObjectInstance, &X::somethingHappend, this, [this](){
QQmlComponent component(&engine_, "qrc:/b.qml");
QObject *obj = component.create();
QVariant returnedValue;
QMetaObject::invokeMethod(obj, "connectionConfirmed",
Q_RETURN_ARG(QVariant, returnedValue));
delete obj;
});
In this code you are first creating the component, and then you create the object, call the function written in js, that function has just been executed and print the message, and after that you delete it, so when you delete it, it will never fire. new Timer, for that reason it seemed that the one that printed the first message was the item created in QML, but it is not, the new item printed it.
Calling a C ++ QML function brings these problems because many times you do not access the object you want.
The recommendation indicated by Qt is, on the contrary, to make the connection on the QML side, for this you must export the C ++ object using setContextProperty().
In the following example using its implementation of QML, we create a class that has a signal called mysignal, it is triggered every 5 seconds for the purpose of testing. An object of that class is exported to QML and connected so that it invokes the js function from QML
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTimer>
class Helper: public QObject{
Q_OBJECT
public:
using QObject::QObject;
signals:
void mysignal();
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
Helper helper;
engine.rootContext()->setContextProperty("helper", &helper);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&helper](){
emit helper.mysignal();
});
timer.start(5000);
return app.exec();
}
#include "main.moc"
b.qml
import QtQuick 2.0
Item {
id: it
function connectionConfirmed() {
myTimer.start()
console.log("started timer")
}
Timer {
interval: 1000; running: false; repeat: false
id: myTimer
onTriggered: {
console.log("timer triggered")
}
}
Connections{
target: helper
onMysignal: it.connectionConfirmed()
}
}
Finally, in a StackView, items are created and destroyed each time a page is changed.
In my Qt5.9 widget application project (Windows), I added a QQuickWidget in the ui and set the source file to a QML file.
My itention is to display open street maps in the QQuickWidget. By clicking a button, the center location of the map should change to specific lat/long coordinates.
The map gets displayed in the QQuickWidget as expected, however, I can't get the location change by button click to work.
I am using this QML file content to display the map:
//================================
// map.qml
//================================
import QtQuick 2.0
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6
Item {
id: qmlMap
Plugin {
id: osmPlugin
name: "osm"
}
Map {
id: map
anchors.fill: parent
plugin: osmPlugin
center: QtPositioning.coordinate(59.91, 10.75)
zoomLevel: 10
objectName: "mainMap"
MapQuickItem {
id: marker
coordinate {latitude: 59.91
longitude: 10.75}
anchorPoint.x: image.width * 0.5
anchorPoint.y: image.height
sourceItem: Image {
id: image
height: 35
width: 35
source: "geotag.png"
}
function recenter(lat,lng) {
map.clearMapItems();
marker.coordinate.latitude = lat;
marker.coordinate.longitude = lng;
map.addMapItem(marker);
map.center.latitude = lat;
map.center.longitude = lng;
map.update();
}
}
}
}
On application start up, I can see the OSM centered on my specified location and I also can see the marker at the right location.
Loaded map on start up
However, when I click my button to call the function recenter(lat,lng) from C++, nothing seems to happen (no location change on map visible).
My C++ button code for location change is:
void mapproject::on_btnUpdatePos_clicked()
{
QQmlEngine engine;
QQmlComponent component(&engine, "qrc:/map.qml");
QObject *object = component.create();
QVariant returnedValue;
QVariant pos = QVariant(0);
if(object != NULL){
QMetaObject::invokeMethod(object, "recenter",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, pos),
Q_ARG(QVariant, pos));
}
}
Why does the location change not work? Is there a mistake in my QML file or in my C++ code?
Assuming that the QQuickWidget has been added through Qt Designer and it is called quickWidget, so you can access it using ui->quickWidget.
To do a simple search you can set an objectName in the MapQuickItem:
MapQuickItem {
id: marker
objectName: "mapItem"
coordinate {latitude: 59.91
[...]
You should not create a new component, you should use the QQuickWidget, the first thing is to get the item that shows the QQuickWidget through the rootObject() method, then look for the child named mapItem and invoke the recenter method:
void MainWindow::on_btnUpdatePos_clicked()
{
QQuickItem *item = ui->quickWidget->rootObject();
QObject *object = item->findChild<QObject*>("mapItem");
QVariant posx = QVariant(-12.0464);
QVariant posy = QVariant(-77.0428);
if(object != NULL){
QMetaObject::invokeMethod(object, "recenter",
Q_ARG(QVariant, posx),
Q_ARG(QVariant, posy));
}
}
The complete example can be found in the following link
So I could change the property of a certain QML object via C++ code, but I couldn't see the result on screen.
I have an item repeated 64 times, and I want a certain image to be displayed only for the 32nd item (from C++) so I used invokeMethod to access the object via C++ then I used setProperty to change the visibility, if I view it with qDebug the property "visible" did change, but I notice no difference on the screen I still cannot see the image, but if I change the visibility from qml, I can see it.
This is the C++ code:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl("qrc:///main.qml"));
view.show();
QQuickItem* child;
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///Board.qml")));
QObject *rootObject = engine.rootObjects().first();
QQuickItem *qmlObject = rootObject->findChild<QQuickItem*>("grid")->findChild<QQuickItem*>("repeter");
QMetaObject::invokeMethod(qmlObject,"itemAt",Qt::DirectConnection, Q_RETURN_ARG (QQuickItem*,child), Q_ARG(int,32));
child=child->findChild<QQuickItem*>("pleaseWork");
qDebug() << child->property("visible");
child->setProperty("visible","true");
qDebug() << child->property("visible");
return app.exec();
}
I used qDebug to verify the property changed
This is the QML code :
Item
{
id: root
width: 8*45
height: 8*45
Grid
{
id: grid
objectName: "grid"
rows: 8
Repeater
{
objectName: "repeter"
model: 64
Image
{
objectName: "test"
width: 45; height: 45
source: "images/dark_square.png"
Image
{
id: isit
objectName: "pleaseWork"
visible: false
source: "images/avail_dark.png"
}
}
}
}
}
QQuickView and QQmlApplicationEngine are alternative ways to load and show QML views. What you are loading into QQmlApplicationEngine has nothing to do with the visible output of QQuickView.
In order to get things running, you need to change the top element of the QML file from Item to Window and show it on screen:
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///Board.qml")));
// end of your code
QObject *rootObject = engine.rootObjects().first();
QQuickWindow *window = qobject_cast<QQuickWindow *>(rootObject);
if (!window) {
qDebug() << "Error: Your root item has to be a window.";
return -1;
}
window->show();
// continue with your code
QQuickItem *qmlObject = rootObject->findChild<QQuickItem*>("grid")->findChild<QQuickItem*>("repeter");
I'm trying to do a simple task as changing a property (text: ) of some QML object from C++ yet I'm failing miserably. Any help appreciated.
I'm not getting any errors, the window shows up, just the text property doesn't change as (at least I think) it should.
Is even anything I'm NOT doing wrong here?!!
What I was trying is this:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQuickItem>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QString>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
QObject *object = component.create();
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QString thisString = "Dr. Perry Cox";
object->setProperty("text", thisString); //<--- tried instead of thisString putting "Dr. ..." but nope.
delete object;
return app.exec();
}
main.qml
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
visible: true
width: 360
height: 360
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
id: whot
text: ""
anchors.centerIn: parent
font.pixelSize: 20
color: "green"
}
}
When you call QObject *object = component.create(); you get access to the root context, which is the Window component and its properties.
To get access to Text properties, you can create property alias like this:
Window {
property alias text: whot.text
...
Text {
id: whot
text: ""
...
}
}
That will give you access to whot's text property from within the Window's context.
There is another slightly more round-about way. Assign objectName property instead of id (or both if you still need id) to whot:
Text {
id: whot // <--- optional
objectName: "whot" // <--- required
text: ""
...
}
Now you can do this in code:
QObject *whot = object->findChild<QObject*>("whot");
if (whot)
whot->setProperty("text", thisString);
On a side note: I don't think you are supposed to delete the object until after calling app.exec(). Otherwise, it will ... well, be deleted. :)
#include <QQmlContext>
#include <qquickview.h>
#include <qdir.h>
QQmlApplicationEngine engine;
QString root = QCoreApplication::applicationDirPath();
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow*>(topLevel);
window->setProperty("root", root);
for qml
ApplicationWindow {
property string root
onRootChanged: {
console.log("root : "+ root);
}
}
For QML properties you should use QQmlProperty instead:
QQmlProperty::write(whot, "text", thisString);
I've got following code:
main.cpp
QDeclarativeView *qmlView = new QDeclarativeView();
qmlView->setSource(QUrl("qrc:/nav.qml"));
ui->nav->addWidget(qmlView);
Blockschaltbild bild;
QObject *value = qmlView->rootObject();
QObject::connect(value, SIGNAL(testSig()), &bild, SLOT(BlockSlot()));
The signals and slots connect correctly. (QObject::connect returns "true")
qml file:
Rectangle {
id: rectangle1
....
signal testSig()
MouseArea{
id: mousearea
anchors.fill: parent
onEntered: parent.color = onHoverColor
onExited: parent.color = parent.buttonColor
onClicked: {
rectangle1.testSig()
console.log("Button clicked")
}
}
}
This is where the slot is located:
Blockschaltbild.h
class Blockschaltbild: public QObject
{
Q_OBJECT
public slots:
void BlockSlot(){
cout << "Slot is working" << endl;
}
public:
....
}
If I click on the mouse area, the console shows "Button clicked" but not "Slot is working".
I use Qt 4.8.4 with QtQuick 1.1. Where is my mistake?
If you simply require to work with your Blockschaltbild object in QML, you can also decide against a loose coupling with signal and slots and simply pass your object as a context parameter, to make it available in QML.
QDeclarativeView *qmlView = new QDeclarativeView();
qmlView->setSource(QUrl("qrc:/nav.qml"));
ui->nav->addWidget(qmlView);
Blockschaltbild* bild;
QObject *value = qmlView->engine().rootContext()->setContextProperty("bild", bild);
You can then call the BlockSlot() slot of your object from QML with:
Rectangle {
id: rectangle1
....
MouseArea{
id: mousearea
anchors.fill: parent
onEntered: parent.color = onHoverColor
onExited: parent.color = parent.buttonColor
onClicked: {
bild.BlockSlot() // call BlockSlot of "bild" context property
console.log("Button clicked")
}
}
}
It is also possible to use qmlRegisterType, which allows you to create instances of your Blockschaltbild class with QML. See here for more information.