Add ListView sections based on c++ QStringList model - c++

The QStringList I'am providing to my QML ListView is displayed correctly. Now I want to reorganize the strings into sections but I'am kind of stuck on what to provide to the section.property ! Is there a minimal change I can make to my code to provide this info ?
Here is my code so far:
C++ Class:
class Foo : public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList data READ getData WRITE setData NOTIFY dataChanged)
public:
QStringList getData() const {return m_data;}
void setData(const QStringList &data);
signals:
void dataChanged();
private:
QStringList m_data;
};
main.cpp
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<Foo>("MyModule", 1, 0, "Foo");
QQuickView view;
view.setSource(QUrl("qrc:/main.qml"));
view.show();
return app.exec();
}
main.qml
import MyModule 1.0
Foo{
id: foo
}
...
ListView {
id: myView
model: foo.data
delegate: Text {text: modelData}
section {
property: what should I write in here ?
}
}

Related

QML instantiates C++ objects. How do I access their methods?

Here's what my main.cpp looks like:
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
QCoreApplication::addLibraryPath("./");
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:/myqml.qml"));
view.show();
return app.exec();
}
As you can see, it creates things from myqml. Well, myqml instantiates a C++ class MyClass.
How do I access this C++ methods from the object QQuickView view? For example, I'd like to do something of the type view.MyClassInstance.myMethod1()
You want to obtain an object created in QML by C++, here it does not matter if the target has a prototype created in C ++ or not. If you want this you must obtain the object through findChild since all the objects created in QML have a kinship relationship with the window.
main.cpp
#include <QtQuick>
#include <QtGui>
class MyClass: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void invokable(){
qDebug()<< "invokable";
}
Q_SLOT void slot(){
qDebug()<< "slot";
}
void function(){
qDebug()<< "function";
}
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
qmlRegisterType<MyClass>("foo", 1, 0, "MyClass");
QGuiApplication app(argc, argv);
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view.show();
if(MyClass* myclass_instance = view.findChild<MyClass *>("myclass_instance")){
myclass_instance->invokable();
myclass_instance->slot();
myclass_instance->function();
}
return app.exec();
}
#include "main.moc"
main.qml
import QtQuick 2.9
import foo 1.0
Rectangle {
color: "salmon"
width: 640
height: 480
MyClass{
objectName: "myclass_instance"
}
}
But this method has several drawbacks such as who manages the life cycle of the object is QML, not C ++ so the pointer could at some point point to an unreserved address. Another disadvantage is that there is a dependency of C++ to QML since if the objectName is changed in QML the code in C ++ would have to be changed.
Another approach is to create a helper class that is exported to QML with setContextProperty() and that interacts with the MyClass object.
main.cpp
#include <QtQuick>
#include <QtGui>
class MyClass: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
Q_INVOKABLE void invokable(){
qDebug()<< "invokable";
}
Q_SLOT void slot(){
qDebug()<< "slot";
}
void function(){
qDebug()<< "function";
}
};
class Helper: public QObject
{
Q_OBJECT
public:
using QObject::QObject;
void call_function(){
emit called();
}
Q_SIGNAL void called();
};
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
qmlRegisterType<MyClass>("foo", 1, 0, "MyClass");
QGuiApplication app(argc, argv);
Helper helper;
QQuickView view;
view.rootContext()->setContextProperty("helper", &helper);
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
view.show();
helper.call_function();
return app.exec();
}
#include "main.moc"
main.qml
import QtQuick 2.9
import foo 1.0
Rectangle {
color: "salmon"
width: 640
height: 480
MyClass{
id: myclass
}
Connections{
target: helper
onCalled:{
myclass.invokable()
myclass.slot()
}
}
}
The advantage of this method is that there is no dependence between the logic of C++ and QML, besides the life cycle does not generate problems since the myclass objects are not handled directly in QML. The disadvantage is that you write a little more code and you can only call the Q_INVOKABLE or Q_SLOT.

Exchange data from C++ to QML

And sorry about the vague title, i don't know how to express it better, so pardon me.
I got a class to store data, inherit from QObject, and a loginSignal
class UserData : public QObject {
Q_OBJECT
public:
Q_INVOKABLE QString login(const QString p_user, const QString p_password, const bool p_remember);
UserData(QObject* parent = 0);
signals:
void userLogin();
};
Register class to QML
qmlRegisterType<UserData>("UserData",1,0,"UserData");
Connect to a QML Element
ApplicationWindow {
UserData {
id:userData
}
Rectangle {
Connections {
target: userData
onUserLogin : {
doSomething()
}
}
}
}
However if i call emit(userLogin()) signal inside login() function, nothing happend in QML
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine(QUrl("qrc:///mainControl.qml"));
QQmlContext* context = engine.rootContext();
UserData userData;
context->setContextProperty("userData",&userData);
userData.login(user, pass, true);
return app.exec();
}
Maybe QML Component hasn't complete finished yet?
And thank for dropping by
Edit : Look like i accident create a second instance in QML, and use it. Everything working fine now :D

Using a C++ class variable in QML file

How can I use a C++ class variable in QML file in Qt. I want to set a variable based on Q_OS_Android in c++ file and evaluate a condition in QML file. How will this be possible?
You have to declare the variable as property in your header file and register the class with qml in your main. Here is an example for a class Foo and a variable QString var:
class Foo : ...
{
Q_OBJECT
Q_PROPERTY(QString var READ getVar WRITE setVar NOTIFY varChanged)
public:
Foo();
~Foo();
QString getVar() const {return m_var;}
void setVar(const QString &var);
signals:
void varChanged();
public slots:
//slots can be called from QML
private:
QString m_var;
};
In the main you will have something like this:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<Foo>("MyApp", 1, 0, "Foo");
QQuickView view;
view.setSource(QUrl("qrc:/main.qml"));
view.show();
return app.exec();
}
In your Qml File you can simply import your class using:
import MyApp 1.0
And then use your class as you would any normal QML Type:
Foo{
id: myClass
var: "my c++ var"
...
}

Best way to have qml function and c++ slot and vice versa for the same item

I want to do something like this
QML app:
{
signal qmlSignal
function qmlFunction
}
And
c++ Hnadler:
{
c++ slot
c++ signal
}
Want to have two way communication with the same qml object.
I am referring http://qt-project.org/doc/qt-4.8/qtbinding.html
for changing a value in qml from C++, we can do
QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, "MyItem.qml");
QObject *object = component.create();
QVariant returnedValue;
QVariant msg = "Hello from C++";
QMetaObject::invokeMethod(object, "myQmlFunction",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, msg));
qDebug() << "QML function returned:" << returnedValue.toString();
delete object;
can be used to call qml function. But with this we cannot use QT method
And
class MyClass : public QObject
{
Q_OBJECT
public slots:
void cppSlot(const QString &msg) {
qDebug() << "Called the C++ slot with message:" << msg;
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDeclarativeView view(QUrl::fromLocalFile("MyItem.qml"));
QObject *item = view.rootObject();
MyClass myClass;
QObject::connect(item, SIGNAL(qmlSignal(QString)),
&myClass, SLOT(cppSlot(QString)));
view.show();
return app.exec();
}
This can be used for qmlsignal and c++ slot.
Is there a way both of these can be done in one object?
=== Updated question ===
You can call slots or Q_INVOKABLE methods just fine in QML to change "C++ properties" from QML. You will need to expose the C++ object as a context property though. You would need to write something like this below:
myclass.h
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QObject *parent) : QObject(parent) { ... }
Q_INVOKABLE void myInvokable();
public slots:
void mySlot();
...
}
main.cpp
...
MyClass myClassObject;
QQuickView view;
view.rootContext()->setContextProperty("myClassContextProperty", &myClassObject;
view->setSource(QUrl::fromLocalFile("main.qml"));
view->show();
...
main.qml
Button {
...
// You can replace onClicked with your own custom signal
onClicked: myClassContextProperty.myslot()
// or
onClicked: myClassContextProperty.myInvokable()
...
}
=== Further questions in the comment ===
You would have a Q_PROPERTY in C++, and you bind that property to your property in QML, or catch the onFooChanged signal handler for the property called "foo".
myclass.h
class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(foo READ foo NOTIFY fooChanged)
public:
MyClass(QObject *parent) : QObject(parent) { ... }
int foo() const;
public signals:
void fooChanged();
public slots:
void mySlot() { foo = 5; emit fooChanged(); };
private:
int foo;
...
}
main.cpp
...
MyClass myClassObject;
QQuickView view;
view.rootContext()->setContextProperty("myClassContextProperty", &myClassObject;
view->setSource(QUrl::fromLocalFile("main.qml"));
view->show();
...
main.qml
...
// Bind foo to bar
property int bar: myClassContextProperty.foo
=== Original question ===
It seems that you are trying to ask if you can declare the same both in C++ and QML simultaneously, i.e. some parts of it in QML and then the rest in QML.
I think you would need to register (qmlRegisterMetaType, etc) your C++ type with the Qt meta type system, and then you could use that class as an item in QML (maybe with a parent?) to achieve what you wish.
This is the relevant documentation for further reading:
Registering an Instantiable Object Type
For your convenience, here goes some code inline:
message.h
class Message : public QObject
{
Q_OBJECT
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
Q_PROPERTY(QDateTime creationDate READ creationDate WRITE setCreationDate NOTIFY creationDateChanged)
public:
// ...
};
qmlRegisterType<Message>("com.mycompany.messaging", 1, 0, "Message");
...
main.qml
import com.mycompany.messaging 1.0
Message {
author: "Amelie"
creationDate: new Date()
}

QML callback from C++ with custom type as parameter

I would like to be able to call a QML function from C++ with an instance of a custom class as a parameter and then manipulate the instance from QML.
Here is what I did so far :
Data.h
class Data : public QObject
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText)
public :
Data() : QObject(), _text("Foo") { }
virtual ~Data() { }
Data(const Data & other) { _text = other._text; }
QString text() const { return _text; }
void setText(const QString & text) { _text = text; }
private :
QString _text;
};
Q_DECLARE_METATYPE(Data);
Main.cpp
#include "Data.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Data callBackData;
QQmlEngine engine;
QQmlComponent rootComponent(&engine, QUrl::fromLocalFile("CallBack.qml"));
QObject * rootObj = rootComponent.create();
QMetaObject::invokeMethod(rootObj, "callMeBack",
Q_ARG(QVariant, QVariant::fromValue(callBackData)));
return app.exec();
}
CallBack.qml
import QtQuick 2.0
Item {
function callMeBack(data) {
console.log(data.text)
}
}
The console outputs "Undefined". Did I do something wrong ?
When changing the function body to console.log(data) it outputs "QVariant(Data)" so why can't I access the text property of data ?
I tried registering Data as a QML type using qmlRegisterType<Data>(); but this does not change anything.
Try pass a QObject pointer instead:
Data *callbackData = new Data;
...
QMetaObject::invokeMethod(rootObj, "callMeBack",
Q_ARG(QVariant, QVariant::fromValue(callBackData)));
Not tested, but should work (QML recognize QObject* type).