I would like to change a the color of a rectangle when I click a button. They are both in the main.qml file. I'd like to send a signal to C++ backend to change the color of the rectangle. I can't seem to figure it out from the code given in the documentation
main.qml:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: true
id:root
signal mysignal()
Rectangle{
anchors.left: parent.left
anchors.top: parent.top
height : 100
width : 100
}
Button
{
id: mybutton
anchors.right:parent.right
anchors.top:parent.top
height: 30
width: 50
onClicked:root.mysignal()
}
}
main.cpp:
#include <QApplication>
#include <QQmlApplicationEngine>
#include<QtDebug>
#include <QQuickView>
class MyClass : public QObject
{
Q_OBJECT
public slots:
void cppSlot() {
qDebug() << "Called the C++ slot with message:";
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyClass myClass;
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QPushButton *mybutton = engine.findChild("mybutton");
engine.connect(mybutton, SIGNAL(mySignal()),
&myClass, SLOT(cppSlot()));
return app.exec();
}
Any help would be appreciated!
QPushButton *mybutton = engine.findChild("mybutton");
First, QObject::findChild finds QObjects by object name, not id (which is local to a context anyhow). Hence in QML you need something like:
objectName: "mybutton"
Second, I think you need to perform that findChild not on the engine itself, but on its root objects as returned from QQmlApplicationEngine::rootObjects().
// assuming there IS a first child
engine.rootObjects().at(0)->findChild<QObject*>("myButton");
Third, a Button in QML is represented by a type that you don't have available in C++. So you can't just assign the result to a QPushButton *, but you need to stick with the generic QObject *.
I had to create a seperate class and header file and then connect it to the signal in main.cpp
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include<QtDebug>
#include <QQuickView>
#include<QPushButton>
#include<myclass.h>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject *topLevel = engine.rootObjects().at(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
MyClass myClass;
QObject::connect(window,
SIGNAL(mysignal()),
&myClass,
SLOT(cppSlot())
);
return app.exec();
}
myclass.h
#ifndef MYCLASS
#define MYCLASS
#include <QObject>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public slots:
void cppSlot();
};
#endif // MYCLASS
myclass.cpp
#include<myclass.h>
void MyClass::cppSlot(){
qDebug()<<"Trying";
}
Related
I am working on a program which uses an user interface created in QML. The program at it's core have two threads: the main thread in which UI is running and the second thread which will handle all the rest of the work. So program have once class for interfacing with UI and a second class which it's the backend process. The problem is with connecting the slots/signals from UI class to the second class on the second thread.
The code is the following:
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "QmlInterface.h"
#include "MainClass.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<QmlInterface>("CppInterface",1,0,"CppInterface");
/* Ui Stuff */
QmlInterface qml;
qml.SetupUI();
/* Main class */
MainClass work;
QObject::connect(&qml, SIGNAL(onButtonClicked()), &work, SLOT(on_ButtonClicked()) );
return app.exec();
}
QmlInterface.h
#ifndef QMLINTERFACE_H
#define QMLINTERFACE_H
#include <QObject>
#include <QDebug>
#include <QQmlApplicationEngine>
class QmlInterface : public QObject
{
Q_OBJECT
public:
explicit QmlInterface();
virtual ~QmlInterface();
void SetupUI();
public slots:
Q_INVOKABLE void buttonClicked();
signals:
void onButtonClicked();
private:
QQmlApplicationEngine *engine;
};
#endif // QMLINTERFACE_H
QmlInterface.cpp
#include "QmlInterface.h"
QmlInterface::QmlInterface()
{
}
QmlInterface::~QmlInterface()
{
}
void QmlInterface::SetupUI()
{
engine = new QQmlApplicationEngine;
engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine->rootObjects().isEmpty())
{
qDebug() << "Failed to load UI";
}
}
void QmlInterface::buttonClicked()
{
qDebug() << "Button clicked! Signal to be emited!";
emit onButtonClicked();
}
MainClass.h
#ifndef MAINCLASS_H
#define MAINCLASS_H
#include <QThread>
#include <QtDebug>
class MainClass : public QThread
{
Q_OBJECT
public:
MainClass();
virtual ~MainClass() {}
public slots:
void on_ButtonClicked();
private:
void run();
};
#endif // MAINCLASS_H
MainClass.cpp
#include "MainClass.h"
MainClass::MainClass()
{
}
void MainClass::on_ButtonClicked()
{
qDebug() << "Button click received in main class!";
}
void MainClass::run()
{
while(1)
{
QThread::sleep(1);
}
}
And finally main.qml
import QtQuick 2.11
import QtQuick 2.8
import QtQuick.Controls 2.1
import QtQuick.Window 2.1
import CppInterface 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
CppInterface
{
id: cpp
}
Button
{
text: "Click me"
onPressed: {
cpp.buttonClicked();
}
}
}
The connection between QML and QmlInterface works fine! The problem is with connection between QmlInterface and MainClass.
To be more specific, the problem is that connect() function called in main.cpp seems not to be able to link given signal with given slot from MainClass:
QObject::connect(&qml, SIGNAL(onButtonClicked()), &work, SLOT(on_ButtonClicked()) );
The problem is that you have created 2 instances of QmlInterface:
QmlInterface qml;
qml.SetupUI();
and
CppInterface
{
id: cpp
}
And you have connected the signal of the first instance, and you are emitting the signal using the second instance.
So instead of creating 2 QmlInterface just create one, for convenience you do not create the object in QML and only export the object created in C++ using setContextProperty():
// ...
#include <QQmlContext>
// ...
void QmlInterface::SetupUI()
{
engine = new QQmlApplicationEngine;
engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine->rootObjects().isEmpty())
qDebug() << "Failed to load UI";
else
// export
engine->rootContext()->setContextProperty("cpp", this);
}
*.qml
import QtQuick 2.11
import QtQuick.Controls 2.1
import QtQuick.Window 2.1
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button
{
text: "Click me"
onPressed: cpp.buttonClicked();
}
}
On the other hand it is not necessary to register as a type to QmlInterface so you can remove it:
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
/* Ui Stuff */
QmlInterface qml;
qml.SetupUI();
/* Main class */
MainClass work;
QObject::connect(&qml, &QmlInterface::onButtonClicked, &work, &MainClass::on_ButtonClicked );
return app.exec();
}
I have 2 classes, MyApp and MyAppView. MyApp class will hold other classes and the implementation will be here. (you can call it Manager class or System class). MyAppView class only interacts with main.qml like it'll have lots of "Q_PROPERTY"ies. I think you understand the point. I don't want MyApp will have "Q_PROPERTY"ies.
The scenerio is as the following;
//------------------------------------
//---------------------------main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "myapp.h"
#include "myappview.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<MyAppView>("org.myappview", 1, 0, "MyAppView");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
MyApp myApp;
return app.exec();
}
//------------------------------------
//---------------------------myappview.h
#include <QObject>
class MyAppView : QObject
{
Q_OBJECT
Q_PROPERTY(QString myString READ getMyString NOTIFY myStringChanged)
public:
MyAppView();
QString getMyString() { return m_myString; }
void setMyString(QString newString)
{
m_myString = newString;
emit myStringChanged;
}
signals:
void myStringChanged();
private:
QString m_myString;
}
//------------------------------------
//---------------------------main.qml
import QtQuick 2.0
import QtQuick.Window 2.0
import org.myappview 1.0
Window {
visible: true
MyAppView {
id: backend
}
Text {
text: qsTr(backend.myString)
}
}
//------------------------------------
//---------------------------myapp.h
#include <QObject>
#include "myappview.h"
class MyApp : QObject
{
Q_OBJECT
public:
MyApp();
private:
MyAppView appView;
void changeMyStringInAppView()
{
// This will automatically update main.qml
appView.setMyString("This is new string");
}
}
Also it is okay to reaching existing QML instance from MyApp, instead of instantiating QML from MyApp. So the main point is instantiating or reaching View class from Manager class so that I can control it easily. Maybe at some part, my logic is wrong. Please tell me if I am. I'm okay with all the suggestions.
The problem in your code is that the MyAppView of MyApp is different from the one created in QML, so if you update the appView text it will not be reflected in the backend text, so the solution is to expose an object from MyApp to QML with setContextProperty() and will call a function to establish the MyAppView created in QML (Keep in mind to create only one MyApp but you will have the same problem)
// myappview.h
#ifndef MYAPPVIEW_H
#define MYAPPVIEW_H
#include <QObject>
class MyAppView : public QObject
{
Q_OBJECT
Q_PROPERTY(QString myString READ getMyString NOTIFY myStringChanged)
public:
explicit MyAppView(QObject *parent = nullptr) : QObject(parent)
{}
QString getMyString() const { return m_myString; }
void setMyString(const QString & newString)
{
if(m_myString != newString){
m_myString = newString;
emit myStringChanged();
}
}
signals:
void myStringChanged();
private:
QString m_myString;
};
#endif // MYAPPVIEW_H
// myapp.h
#ifndef MYAPP_H
#define MYAPP_H
#include "myappview.h"
#include <QObject>
class MyApp : public QObject
{
Q_OBJECT
public:
explicit MyApp(QObject *parent = nullptr) : QObject(parent),
appView(nullptr)
{}
Q_INVOKABLE void setAppView(MyAppView *value){
appView = value;
}
void changeMyStringInAppView()
{
if(appView)
appView->setMyString("This is new string");
}
private:
MyAppView *appView;
};
#endif // MYAPP_H
// main.cpp
#include "myapp.h"
#include "myappview.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<MyAppView>("org.myappview", 1, 0, "MyAppView");
MyApp myapp;
QTimer::singleShot(1000, &myapp, &MyApp::changeMyStringInAppView);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("myapp", &myapp);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
// main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import org.myappview 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MyAppView {
id: backend
}
Text {
text: qsTr(backend.myString)
}
Component.onCompleted: myapp.setAppView(backend)
}
I am trying to send a list from QML to c++. I've tried with a string and an integer with success but when I try with a QVariant I get the error:
QObject::connect: No such slot MyClass::cppSlot(QVariant) in ../test_2206/main.cpp:31
My main.qml
import QtQuick 2.4
import QtQuick.Layouts 1.1
import Material.ListItems 0.1 as ListItem
import Material.Extras 0.1
import QtQuick.Controls 1.3 as QuickControls
import QtQuick.Controls 1.4
import Material 0.2
Window {
visible: true
property var listCloud: ["nuage1", "nuage2", "nuage3", "nuage4"]
id: item
width: 100; height: 100
signal qmlSignal(variant msg)
/* MouseArea {
anchors.fill: parent
onClicked: item.qmlSignal("Hello from QML")
}*/
Rectangle{
id:sousRect
color:"red"
width:100; height:100
Button{
id:buttonTest
onClicked: {
item.qmlSignal(listCloud)
}
}
}
}
My main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlProperty>
#include <QQmlComponent>
#include <QDebug>
#include "myclass.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
QObject *object = component.create();
MyClass myClass;
QObject::connect(object, SIGNAL(qmlSignal(QVariant)),
&myClass, SLOT(cppSlot(QVariant)));
return app.exec();
}
My myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDebug>
#include <QString>
#include <QList>
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QObject *parent = 0);
signals:
public slots:
void cppSlot(QVariant &msg);
};
#endif // MYCLASS_H
My myclass.cpp
#include "myclass.h"
MyClass::MyClass(QObject *parent):
QObject(parent)
{
}
void MyClass::cppSlot(QVariant &msg){
qDebug() << "c++: bien reçu chef " << msg;
}
I don't understand why I can't put a QVariant parameter in this signal.
Any help is welcome :)
Change your slot to receive his parameter by value, rather than by reference. Like this:
void cppSlot(QVariant msg);
I think it has something to do with QML refusing to pass Window's property by reference. Anyway, Qt often sends signal by-value even if your signal/slot signature[s] declare arguments by-reference. For more on the topic see this and that
Don't use references between QML and C++, they won't work.
void cppSlot(QVariant & msg);
You can also create Q_INVOKABLE function in C++ and call it directly from QML. Here is how this looks like:
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlProperty>
#include <QQmlComponent>
#include <QDebug>
#include "myclass.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
auto myobject = new MyClass(&app);
engine.rootContext()->setContextProperty(QStringLiteral("myobject"), myobject);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
{
return -1;
}
return app.exec();
}
mycass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDebug>
#include <QString>
#include <QList>
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QObject *parent = 0);
Q_INVOKABLE void cppSlot(QVariant msg);
};
#endif // MYCLASS_H
myclass.cpp:
#include "myclass.h"
MyClass::MyClass(QObject *parent):QObject(parent){}
void MyClass::cppSlot(QVariant msg)
{
qDebug() << "c++: bien reçu chef " << msg;
}
main.qml:
import QtQuick 2.4
import QtQuick.Layouts 1.1
import Material.ListItems 0.1 as ListItem
import Material.Extras 0.1
import QtQuick.Controls 1.3 as QuickControls
import QtQuick.Controls 1.4
import Material 0.2
Window
{
visible: true
property var listCloud: ["nuage1", "nuage2", "nuage3", "nuage4"]
id: item
width: 100; height: 100
Rectangle
{
id:sousRect
color:"red"
width:100; height:100
Button
{
id:buttonTest
onClicked: myobject.cppSlot(listCloud)
}
}
}
But if you like to use SIGNAL SLOTS there is Connections Object in QML. Your main.qml will look then like that:
Window
{
visible: true
property var listCloud: ["nuage1", "nuage2", "nuage3", "nuage4"]
id: item
width: 100; height: 100
Connections
{
target: buttonTest
onClicked: myobject.cppSlot(listCloud)
}
Rectangle
{
id:sousRect
color:"red"
width:100; height:100
Button
{
id:buttonTest
}
}
}
Q_INVOKABLE is nothing else as an public SLOT, it will be called through meta-object system.
I just began learning about Qt programming and found myself struggling with the signals and slot mechanism while making a simple test application.
I have two QML files: main.qml and grocery.qml.
I have a header file that manages the signals and slots of the application:
applicationamanger.h
#ifndef APPLICATIONMANAGER_H
#define APPLICATIONMANAGER_H
#include <QObject>
#include <QDebug>
class ApplicationManager : public QObject
{
Q_OBJECT
public:
explicit ApplicationManager(QObject *parent = 0);
signals:
void newGroceryItem();
public slots:
void addGroceryItem();
};
#endif // APPLICATIONMANAGER_H
The user starts the application on main.qml and when pressing a button loads grocery.qml
On grocery.qml I have an image that can be clicked by the user and emits the signal newGroceryItem()
MouseArea {
id: okBtnClkd
anchors.fill: parent
Connections {
onClicked: {
newGroceryItem()
}
}
}
And the main application main.cpp connects the signals but says it can't find the signal newGroceryItem() as it doesn't belong to main.qml. So my question is, how can I establish the connection to the slot addGroceryItem() from the secondary qml file grocery.qml?
EDIT:
As requested here're the contents of main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQmlContext>
#include "applicationmanager.h"
int main(int argc, char *argv[]){
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
ApplicationManager applicationManager;
QObject::connect(window, SIGNAL(newGroceryItem()), &applicationManager, SLOT(addGroceryItem()));
return app.exec();
}
And the contents of grocery.qml
import QtQuick 2.5
import QtQuick.Controls 1.4
ApplicationWindow {
//Declaration of signals.
signal newGroceryItem()
width: 300
height: 400
visible: true
TextField {
id: product
x: 14
y: 111
width: 270
height: 22
}
Text {
id: text1
x: 14
y: 81
width: 124
height: 24
text: qsTr("Product")
font.pixelSize: 20
}
Image {
id: okBtn
x: 99
y: 290
width: 100
height: 100
source: "img/main/okBtn.png"
MouseArea {
id: okBtnClkd
anchors.fill: parent
Connections {
onClicked: {
newGroceryItem()
}
}
}
}
}
Any method of a QObject-derived type is accessible from QML code if it is:
a public method flagged with the Q_INVOKABLE macro
a method that is a public Qt SLOT
So there are two ways to access addGroceryItem(). The first one is to use Q_INVOKABLE macro as follows:
class ApplicationManager : public QObject
{
Q_OBJECT
public:
explicit ApplicationManager(QObject *parent = 0);
Q_INVOKABLE void addGroceryItem();
};
The second way is to use public slots as follows:
class ApplicationManager : public QObject
{
Q_OBJECT
public:
explicit ApplicationManager(QObject *parent = 0);
public slots:
void addGroceryItem();
};
If you are creating your class object in C++ then you should set the object as the context data for main.qml as follows:
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
QQmlEngine engine;
ApplicationManager app;
engine.rootContext()->setContextProperty("applicationManager", &app);
QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
component.create();
return app.exec();
}
Now you can access the ApplicationManager object created in main.cpp from main.qml as follows:
MouseArea {
id: okBtnClkd
anchors.fill: parent
onClicked: {
applicationManager.addGroceryItem();
}
}
For more information see here.
I am trying to make a simple program that allow user to connect to the specific websites via clicking the image.
Here is my code:
account.h:
#ifndef ACCOUNTS_H
#define ACCOUNTS_H
#include <QObject>
#include <QUrl>
#include <QDesktopServices>
class accounts : public QObject
{
Q_OBJECT
public:
explicit accounts(QObject* parent = 0) : QObject(parent){}
public slots:
void gmailOpen(const QString &msg)
{
QUrl gmailUrl(msg);
QDesktopServices::openUrl(gmailUrl);
}
};
#endif // ACCOUNTS_H
main.cpp:
#include <QtGui/QGuiApplication>
#include <QtQuick/QQuickView>
#include <QtQml>
#include "accounts.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView *view = new QQuickView;
QObject *gmail = view->rootObject().findChild<QObject*>("gmailLink");
accounts *gmailAccount = new accounts;
QObject::connect(gmail, SIGNAL(gmailSignal(QString)),gmailAccount,SLOT(gmailOpen(QString)));
view->setSource(QUrl::fromLocalFile("/Users/yudelin/Documents/MyCrazyProjects/Managers4ManyAccounts/main.qml"));
view->show();
return app.exec();
}
main.qml:
import QtQuick 2.0
Rectangle {
width: 360
height: 360
color: "silver"
Image {
id:gmailLink
objectName: "gmailLink"
width: 102
height: 199
fillMode: Image.PreserveAspectFit
source: "...." //the url is too long so I omit it.
anchors.centerIn: parent
signal gmaiSignal (string msg)
MouseArea {
anchors.fill: parent
onClicked:
gmailLink.gmailSignal("http://mail.google.com")
}
}
}
As you can see, I am trying to connect Qml Image with c++ object accounts.
So I use QObject *gmail = view->rootObject().findChild<QObject*>("gmailLink"); to fetch the qml object.
But it does not work. I guess it is suit for the older Qt version.
How could I fix this problem?
You want gmailOpen slot to be called on clicking on the Image. There are simpler and preferred ways of doing it other than doing this.
QObject *gmail = view->rootObject().findChild("gmailLink");
As #Kunal pointed out, you can use Qt.openUrlExternally(string).
If you want to open the Url by using QDesktopServices::openUrl from the gmailOpen slot, you can call the slot directly by setting the context property.
4, QObject *gmail = view->rootObject().findChild<QObject*>("gmailLink");... Here before setting the qml file, you are trying to get a reference for the gmailLink object. Its not created yet.
Following code explains my take on the problem
5a. accounts.h file
#ifndef ACCOUNTS_H
#define ACCOUNTS_H
#include <QObject>
#include <QUrl>
#include <QDesktopServices>
class accounts : public QObject
{
Q_OBJECT
public:
explicit accounts(QObject* parent = 0) : QObject(parent){}
public slots:
void gmailOpen(const QString &msg)
{
QUrl gmailUrl(msg);
QDesktopServices::openUrl(gmailUrl);
}
};
#endif // ACCOUNTS_H
5b. main.cpp file
#include <QtGui/QGuiApplication>
#include <QtQuick/QQuickView>
#include <QtQml>
#include "accounts.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
accounts *gmailAccount = new accounts;
QQuickView *view = new QQuickView;
view->engine()->rootContext()->setContextProperty("accounts",gmailAccount);
view->setSource(QUrl::fromLocalFile("qml/SO_OpenExternalLink/main.qml"));
view->show();
return app.exec();
}
5c. main.qml file
import QtQuick 2.1
import QtQuick.Dialogs 1.0
Rectangle {
width: 360
height: 360
color: "silver"
Image {
id:gmailLink
objectName: "gmailLink"
width: 102
height: 199
fillMode: Image.PreserveAspectFit
source: "http://upload.wikimedia.org/wikipedia/commons/4/41/Flag_of_India.svg"
anchors.centerIn: parent
MouseArea {
anchors.fill: parent
onClicked:Qt.openUrlExternally("http://mail.google.com")
}
}
Image{
id:secondImage
width:102
height:199
fillMode:Image.PreserveAspectFit
source: "http://upload.wikimedia.org/wikipedia/commons/5/55/Emblem_of_India.svg"
anchors.left:gmailLink.right
anchors.top: gmailLink.top
MouseArea {
anchors.fill: parent
onClicked:fileDialog.visible = true
}
}
FileDialog {
id: fileDialog
title: "Please choose a file"
nameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]
visible:false;
selectMultiple:false
onAccepted:secondImage.source = fileDialog.fileUrl
}
}
5d. You need to change the following from the above code
i. Image source
ii. QML file path in the main.cpp
iii. Possibly import QtQuick 2.0 if you don't have QtQuick 2.1 yet.
If you just want to open URL then you can use Qt.openUrlExternally(string) directly from QML. Here is documentation. No need to create a QObject for this.
And for your code. I don't see any method named gmailSignal.
You should be calling gmailOpen.
like
gmailLink.gmailOpen("http://mail.google.com")