I have a simple BB10 app with a QML front end.
The GUI consists of a couple of buttons and a label
Page {
Container {
Label {
text: app.alarmCount()
}
Button {
text: qsTr("Resend Notification")
onClicked: {
app.resendNotification();
}
}
Button {
text: qsTr("Stop Service")
onClicked: {
app.stopService();
}
}
Button {
text: qsTr("Kill Service")
onClicked: {
app.killService();
}
}
}
}
And the C++ class
class ApplicationUI: public QObject
{
Q_OBJECT
Q_PROPERTY(QString alarmCount READ alarmCount NOTIFY AlarmUpdate)
public:
ApplicationUI();
virtual ~ApplicationUI() { }
Q_INVOKABLE void resendNotification();
Q_INVOKABLE void stopService();
Q_INVOKABLE void killService();
QString alarmCount() const;
void setAlamCount(int newCount);
signals:
void AlarmUpdate();
private:
bb::system::InvokeManager* m_invokeManager;
QString m_alarmCountDisplay;
};
and the hopefully relevant bit of the class
QString ApplicationUI::alarmCount() const
{
return m_alarmCountDisplay;
}
void ApplicationUI::setAlamCount(int newCount)
{
m_alarmCountDisplay = QString("%1 Alarms").arg(newCount);
emit AlarmUpdate();
}
My problem is the label never displays the alarm count string property. I have set a breakpoint on the emit and can see it's getting called and on the alarmCount() getter and can see that's returning the correct value but my front end never actually shows a value for the label.
You did not actually make a binding to the variable. Correct binding will look like:
text: app.alarmCount
But in your code it is:
text: app.alarmCount()
With your code it makes an error because you can't access any method of Q_OBJECT which is not Q_INVOKABLE or public slot. But even if you make such mark to your methods it means that you get alarmCount property only one single time and it will not be updated since you did not make a binding but just one method call.
Related
I implemented a model in c++ and want to assign to Qml, but the error message showed that:
Unable to assign a function to a property of any type other than var.
What might be the problem with my code, thanks!
.qml
ListView{
anchors.fill: parent
model:MyModel{
list:data
}
delegate: objRecursiveDelegate
}
main.cpp
qmlRegisterType<MyModel>("Model",1,0,"MyModel");
qmlRegisterUncreatableType<Data>("Model",1,0,"Data",QStringLiteral("Data should not be created in QML"));
Data data;
engine.rootContext()->setContextProperty(QStringLiteral("data"), &data);
MyModel.h(try to define using QAbstractListModel)
class Data;
class MyModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(Data *list READ list WRITE setList)
public:
Data *list() const;
void setList(Data *list);
private:
Data* mList;
}
MyModel.cpp
Data *MyModel::list() const
{
return mList;
}
void MyModel::setList(Data *list)
{
beginResetModel();
if (mList)
mList->disconnect(this);
mList = list;
/..../
endResetModel();
}
data.h
class Data:public QObject
{
Q_OBJECT
public:
explicit Data(QObject *parent = nullptr);
/..../
private:
QVector<unit> menu;
};
The error is really simple, the data() method of QAbstractListModel, so if you use data inside MyModel it is interpreting that you want to use this method and not the data that you really want to pass, the solution is to change the name:
*.cpp
Data data;
engine.rootContext()->setContextProperty(QStringLiteral("info"), &data);
*.qml
ListView{
anchors.fill: parent
model:MyModel{
list: info
}
delegate: objRecursiveDelegate
}
Plus:
On the other hand you have another error, the Data items method must return menu:
QVector<unit> Data::items() const{
return menu;
}
On the other hand in your delegate you must access access to each role using the name if the model:
Component {
id: objRecursiveDelegate
Column {
Row {
//indent
Item {
height: 1
width: level * 40 // <--
}
Text {
text: name // <--
}
Button{
x:550
width:30
text: "-"
}
}
}
}
I have made other improvements to your code so you can find the complete code in the following link.
I have some model class that inherits QAbstractListModel:
VehiclesModel.h:
class VehiclesModel : public QAbstractListModel {
Q_OBJECT
public:
enum Roles {
ImagePathRole = Qt::UserRole + 1, // QString
NameRole // QString
};
virtual int rowCount(const QModelIndex & parent = QModelIndex()) const override { ... }
virtual QVariant data(const QModelIndex & index, int role) const override { ... }
virtual QHash<int, QByteArray> roleNames() const override {
QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
roles[ImagePathRole] = "imagePath";
roles[NameRole] = "name";
return roles;
}
};
main.cpp:
#include "VehiclesModel.h"
int main(int argc, char * argv[]) {
QGuiApplication app(argc, argv);
VehiclesModel vehiclesModel;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("vehiclesModel", &vehiclesModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
And ComboBox that displays this model:
main.qml:
ComboBox {
id: control
model: vehiclesModel
delegate: ItemDelegate {
contentItem: RowLayout {
Image {
source: imagePath
}
Label {
text: name
}
}
highlighted: control.highlightedIndex == index
}
contentItem: RowLayout {
Image {
source: ??imagePath??
}
Label {
text: ??name??
}
}
}
I want to customize the ComboBox to show vehicle image and name. I can access to model data from ItemDelegate but how to access to model data outside the ItemDelegate? For example I want to access current index data (ImagePathRole and NameRole) to display vehicle image and name in contentItem.
Is it possible to do it without calling QAbstractListModel methods directly (i.e. index() and data() methods) and making them Q_INVOKABLE?
Not in any sort of a decent built-in way at the present time, unfortunately, this is something I've found to be lacking for quite a while, and I've considered implementing something for this in the QML models functionality, but I haven't yet had the time to do so.
For the time being, you can either do it yourself (like you're discussing), at the cost of type-safety and so on, or (the way I've typically tackled this before), you can create a QObject subclass to represent a single item in the model (ItemDataThing or whatever you choose to call it); provide it with a source model & index, properties, and let it represent a single instance of data from the model.
Something like:
class ImageDataThing : public QObject
{
Q_OBJECT
Q_PROPERTY(QString imagePath READ imagePath NOTIFY imagePathChanged)
Q_PROPERTY(QAbstractItemModel* model READ model WRITE setModel NOTIFY modelChanged)
Q_PROPERTY(int index READ index WRITE setIndex NOTIFY indexChanged)
public:
QString imagePath() const;
QAbstractItemModel *model() const;
void setModel(const QAbstractItemModel *newModel);
int index() const;
void setIndex(int newIndex);
signals:
void imagePathChanged(const QString &imagePath);
void modelChanged(QAbstractItemModel *model);
void indexChanged(int indexChanged);
};
... and in your implementation, whenever the model is set, hook the change signals (e.g. rowsInserted, rowsRemoved, ...) to alter the stored index (if provided) to keep it mapped to the correct place in the model.
In the model data getters (here, imagePath for instance), access the model instance (using the index) to grab the data out, and return it.
This has the obvious disadvantage of being a lot of boilerplate, but on the other hand, it's easy-to-write code if you are familiar with models, type-safe, and one could autogenerate it fairly easily.
You could create your own function to get data from the model, like the one I'm currently using,
VehiclesModel.h:
public slots:
int size() const; // to access from QML javascript
QVariant getData(int index, int role); // to access from QML javascript
VehiclesModel.cpp:
int VehiclesModel::size() const {
return m_list.size();
}
QVariant VehiclesModel::getData(int index, int role) {
if (index < 0 || index >= m_list.count())
return QVariant();
switch (role) {
case ImagePathRole:
return ...
break;
default:
break;
}
}
Shameless plug for my SortFilterProxyModel library.
The problem you are asking is actually a head-scratching scenario. I've found a way to do it somewhat correctly but it's kinda complicated and involves an external library. In my solution we filter the source model to only expose the element corresponding to the current index of the combo box and instantiate a delegate for this element and use it as the contentItem of the ComboBox.
This has the advantage of not having to modify your model and keeping in synch with your model changes.
import SortFilterProxyModel 0.2 // from https://github.com/oKcerG/SortFilterProxyModel
import QtQml 2.2
/*
...
*/
ComboBox {
id: control
model: vehiclesModel
delegate: ItemDelegate {
contentItem: RowLayout {
Image {
source: imagePath
}
Label {
text: name
}
}
highlighted: control.highlightedIndex == index
}
contentItem: { currentIndex; return selectedInstantiator.object; } // use currentIndex to force the binding reevaluation. When the model changes, the instantiator doesn't notify object has changed
Instantiator {
id: selectedInstantiator
model: SortFilterProxyModel {
sourceModel: control.model
filters: IndexFilter {
minimumIndex: control.currentIndex
maximumIndex: control.currentIndex
}
}
delegate: RowLayout {
Image {
source: imagePath
}
Label {
text: name
}
}
}
}
I strongly suggest to look at the Qt QML Tricks library made by Thomas Boutroue:
https://gitlab.com/qt-qml-libraries-4-me/qt-qml-tricks-ng
More specific the QQmlObjectListModel (from the Qt QML Models) could do the trick for you.
Expanding with using the Qt Super-Macros, it reduces overhead writing setters/getters!
These macros basically expand to a Q_PROPERTY, resulting in accessibility from QML, and add definition of a setter, getter and private variable.
Usage in your specific case this could look something like this, quickly written down, not validated (check using the correct index for referencing the model):
VehicleItem.h:
#include <QObject>
#include "QQmlVarPropertyHelpers.h" // Include library Qt Super-Macros
class VehicleItem : public QObject {
Q_OBJECT
QML_WRITABLE_VAR_PROPERTY(QString, imagePath)
QML_WRITABLE_VAR_PROPERTY(QString, name)
public:
explicit VehicleItem(QString imagePath, QString name, QObject* parent=0)
: QObject (parent)
, m_imagePath (imagePath)
, m_name (name)
{}
};
VehiclesModel.h:
#include <QObject>
#include "QQmlObjectListModel.h" // Include library Qt QML Models
#include "VehicleItem.h"
class VehiclesModel : public QObject {
Q_OBJECT
QML_OBJMODEL_PROPERTY(VehicleItem, modelList)
public:
explicit VehiclesModel(QObject *parent = 0);
};
VehiclesModel.c:
#include "VehiclesModel.h"
VehiclesModel::VehiclesModel(QObject *parent) :
QObject(parent), m_modelList(new QQmlObjectListModel<VehicleItem>())
{}
main.c (remains the same):
#include "VehiclesModel.h"
int main(int argc, char * argv[]) {
QGuiApplication app(argc, argv);
VehiclesModel vehiclesModel;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("vehiclesModel", &vehiclesModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml:
ComboBox {
id: control
model: vehiclesModel
delegate: ItemDelegate {
contentItem: RowLayout {
Image {
source: imagePath
}
Label {
text: name
}
}
highlighted: control.highlightedIndex == index
}
contentItem: RowLayout {
Image {
source: vehiclesModel.modelList.get(index).imagePath
}
Label {
text: vehiclesModel.modelList.get(index).name
}
}
}
As modelList (and also imagePath and name) is expanded by the macro to a Q_PROPERTY, it is accessible from QML side.
For the ins-and-outs of this library, be sure to check Thomas Boutroue's lightning talk at the QtWS2015: https://www.youtube.com/watch?v=96XAaH97XYo
I'm trying to make a very basic timetable app just to learn the basics of Qt. I've researched this quite a bit and cant seem to get an answer to my problem, I emit the signal when I change the value of my subject but the emitting signal does not update the GUI. It definitely is changing the value in the code but the QML GUI does not update it during run time.
I have a class called Day with the following Q_PROPERTY's:
Q_PROPERTY(Period *getPeriod READ getPeriod WRITE setPeriod NOTIFY periodChanged)
Q_PROPERTY(QString getDay READ getDay WRITE setDay NOTIFY dayChanged)
and a member to hold the periods
Period *m_period = new Period[10]; //Of type Period (my other class) which holds my subject string
I also have getters and setters for the days and periods(as seen in the Q_PROPERTY) and these two for setting/getting the subject:
Q_INVOKABLE QString getSubject(int t){
return m_period[t].getSub();
};
Q_INVOKABLE void setSubject(int t, QString subj){
m_period[t].setSub(subj);
emit periodChanged();
};
With the following signals:
void periodChanged();
void dayChanged();
I also have a class called Period with the following Q_PROPERTY:
Q_PROPERTY(QString getSub READ getSub WRITE setSub NOTIFY subChanged)
and a member to hold the subject names:
QString subject;
My Period class hold the functions called in day to actually change the QString subject:
QString getSub(){
return subject;
};
void setSub(QString sub){
subject = sub;
emit subChanged();
};
With the following signal:
void subChanged();
So surely when the subject gets changed using setSubject() in my QML, shouldn't it be emitting the periodChanged() and the subChanged() signals, which should update the GUI? I thought it would but it doesn't seem to be working.
For reference, this is basically how I implemented it in QML:
Label { text: monday.getSubject(2) } //How I display the Subject, the parameter being the period number
Button{
text: "Enter"
onClicked:{
monday.setSubject(2, "RANDOM_TEXT") //How I set the subject at run time, with the first argument being the period number and second the text I want to change it to
}
Here are the main and class files:
main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qmlRegisterType<Day>("Timetable", 1,0, "Day");
qmlRegisterType<Period>("Timetable", 1,0, "Period");
QQmlApplicationEngine engine;
Day monday;
engine.rootContext()->setContextProperty("monday", &monday);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
component.create();
return app.exec();
}
Day.h
class Day : public QObject
{
Q_OBJECT
Q_PROPERTY(Period *getPeriod READ getPeriod WRITE setPeriod NOTIFY periodChanged)
private:
Period *m_period = new Period[10];
public:
Day(QObject *parent = 0);
~Day();
Period* getPeriod();
Q_INVOKABLE void setPeriod(Period *p);
Q_INVOKABLE QString getSubject(int t){
return m_period[t].getSub();
};
Q_INVOKABLE void setSubject(int t, QString subj){
m_period[t].setSub(subj);
emit periodChanged();
};
signals:
void periodChanged();
};
Period.h
class Period: public QObject
{
Q_OBJECT
Q_PROPERTY(QString getSub READ getSub WRITE setSub NOTIFY subChanged)
private:
QString subject;
public:
Period(QObject *parent = 0);
~Period();
QString getSub(){
return subject;
};
void setSub(QString sub){
subject = sub;
emit subChanged();
};
signals:
void subChanged();
};
main.qml
ApplicationWindow {
id: mainWindow
visible: true
title: "Timetable App"
property int margin: 11
width: mainLayout.implicitWidth + 2 * margin
height: mainLayout.implicitHeight + 2 * margin
minimumWidth: 800
minimumHeight: 600
ColumnLayout {
id: mainLayout
anchors.fill: parent
anchors.margins: margin
GroupBox{
id: timetable
title: "Timetable"
Layout.fillWidth: true
Layout.fillHeight: true
GridLayout {
id: gridLayout
rows: 11
flow: GridLayout.TopToBottom
anchors.fill: parent
Layout.fillWidth: true
Layout.fillHeight: true
Label { }
Label { text: "8:00" }
Label { text: ...} // Added Labels for times from 8:00 to 17:00
Label { text: "Monday" }
Label { text: monday.getSubject(0) }
Label { text: monday.getSubject(1) }
Label { text: ...} // Added Labels to display subjects for monday at different times, also did the same for the rest of the week
}
}
RowLayout{
Button{
text: "Enter"
onClicked:{
monday.setSubject(1, "RANDOM_TEXT") // Set the second period of monday to "RANDOM_TEXT"
console.log(monday.getSubject(1)) // To check if actually set
}
}
}
}
}
Your problem is that you are retrieving values via method calls in QML, not via property bindings.
A QML code like this should update
text: monday.getPeriod.getSub
Obviously calling properties "getSomething" is a bit weird.
Now, your m_period suggests that a single Day object will have more than one Period, which would suggest that you might want a list property instead of a single Period object. Something like
Q_PROPERTY(QList<Period*> periods READ getPeriods NOTIFY periodsChanged);
Using that from QML a bit like this
text: monday.periods[0].getSub
Or even with e.g. a Repeater
Label { text: "Monday" }
Repeater {
model: monday.periods
Label {
text: modelData.getSub
}
}
Not related to your update problem but also important to consider:
NOTIFY signals trigger binding updates, so you don't want to emit them unnecessarily. I.e. setters that emit those should first check if the new value is actually different than the old value and only emit if they are.
Can I manage QSqlQueryModel's subclass at run time calling its methods from QML code and updating (changing) the current model? (The data is send to TableView ) How can I do it?
My QSqlQueryModel's subclass:
class SqlQueryModel : public QSqlQueryModel
{
Q_OBJECT
public:
explicit SqlQueryModel(QObject *parent = 0);
void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
void setQuery(const QSqlQuery &query);
QVariant data(const QModelIndex &index, int role) const;
QHash<int, QByteArray> roleNames() const { return m_roleNames; }
private:
void generateRoleNames();
QHash<int, QByteArray> m_roleNames;
};
main.cpp:
// ...
SqlQueryModel sqlQueryModel;
QQuickView view;
QQmlContext *context = view.rootContext();
context->setContextProperty("sqlQueryModel", &sqlQueryModel);
// ...
For example, I need to call Q_INVOKABLE method changeModel() at run time that changes the current model and updates it with parameterized SELECT query:
void SqlQueryModel::changeModel(const int someValue)
{
QString statement;
QSqlQuery query;
statement = "SELECT * FROM 'tablename' WHERE some_field = ?;";
query.prepare(statement);
query.addBindValue(someValue);
query.exec();
setQuery(query);
}
And as result we get update the data into TableView:
TableView {
id: view
model: sqlQueryModel
TableViewColumn {
title: "1st field"
role: "someValue"
delegate: Text {
text: styleData.value
}
}
TableViewColumn {
title: "2nd field"
role: "oneMoreValue"
delegate: Text {
text: styleData.value
}
}
}
// ...
onSomeSignal: {
// query like this:
sqlQueryModel.changeModel(someValue);
}
Is it possible to do it using QSqlQueryModel? Please help me to solve this problem.
UPD: Perhaps, it's necessary to call function like qmlRegisterType() in order to allow using Q_INVOKABLE methods of SqlQueryModel class from the outside (from QML) and then initialize SqlQueryModel as a type within QML file. Thereafter we can connect our new SqlQueryModel type as TableView's data model.
UPD: I don't need to edit the data stored in the database. I want to be able to change SELECT query from one to similar.
It's very strange but the right answer is in the question.
The implementation described there works correctly. Perhaps, it went badly because of typo or defect that sneaked into project's source code.
I want to bind a method of a C++ class to a QML component property and re-evaluate it when a dependent property changes. The following QML component does what I want:
// Converter.qml:
import QtQuick 2.0
QtObject {
property double rate: 1
function exchange(amount) { return amount * rate }
}
If I assign the result of the exchange function to a property like so,
Text { text: converter.exchange(100) }
the Text element will automatically update when rate is changed. This works with a QML component, but I do not know how to do it with a C++ class.
I would like to implement a functionally equivalent class in C++:
#include <QObject>
class Convert : public QObject
{
Q_OBJECT
Q_PROPERTY(double rate READ rate WRITE setRate NOTIFY rateChanged)
public:
explicit Convert(QObject *parent = 0)
: QObject(parent), m_rate(1.0)
{ }
signals:
void rateChanged();
public slots:
double exchange(double amount) { return m_rate * amount; }
double rate() { return m_rate; }
void setRate(double r) {
m_rate = r;
emit rateChanged();
}
private:
double m_rate;
};
The rate property is accessible from QML, but changing it does not signal to the QML engine that 'exchange' should be re-evaluated.
Here is my main.qml:
// main.qml
import QtQuick 2.1
import QtQuick.Controls 1.1
import Utils 1.0
ApplicationWindow {
width: 300; height: 200
visible: true
// Converter { id: converter; rate: rateField.text }
CppConverter { id: converter; rate: rateField.text }
Column {
TextField { id: rateInput; text: '0.41' }
TextField { id: amountInput; text: '100.00' }
Text { id: output; text: converter.exchange(amountField.text).toFixed(2) }
}
}
If I enable the CppConverter, the output is updated when I change the amountInput, but not when I change the rateInput. If I comment-in the QML Converter element, the update works fine.
With the QML Converter, the QML runtime identifies the dependency on the rate property and re-evaluates the exchange function when the rate is changed. How can I indicate to the QmlEngine to do the same in the C++ version?
There's no way to do this currently.
I don't think relying on a function being re-evaluated like this is a good practice, though. You should either explicitly call it when necessary:
Connections {
target: converter
onRateChanged: output.text = converter.exchange(amountField.text)
}
Or convert exchange() into a property, and approach it declaratively instead of imperatively (code not complete or tested):
class Convert : public QObject
{
// ...
Q_PROPERTY(double amount READ amount WRITE setAmount NOTIFY amountChanged)
Q_PROPERTY(double exchange READ exchange NOTIFY exchangeChanged)
// ...
public:
double exchange() { return m_rate * m_amount; }
private:
double m_rate;
double m_amount;
};
You'd then need to emit the various *Changed signals, etc. in the appropriate places.