Can't write to a C++ object from QML - qt

I have a program that displays images and can put different filters on these images. In the QML GUI I have a series of buttons that let you set which highlights are enabled.
Column{
id: highlightButtons
anchors {left: frameViewer.right; top: parent.top; leftMargin: 10; topMargin: 10 }
spacing: 20
Rectangle{
id: class1
color: "red"
height: 45
width: 45
radius: 12
MouseArea{
anchors.fill: parent
onClicked: {hl.red = frames[frameList.currentIndex];
loadImages(); console.log(hl.red)}
}
}
Rectangle{
id: class2
color: "green"
height: 45
width: 45
radius: 12
MouseArea{
anchors.fill: parent
onClicked: {hl.green = frames[frameList.currentIndex];
loadImages(); console.log(hl.green)}
}
}
Rectangle{
id: class3
color: "blue"
height: 45
width: 45
radius: 12
MouseArea{
anchors.fill: parent
onClicked: {hl.blue = frames[frameList.currentIndex];
loadImages(); console.log(hl.blue)}
}
}
}
Where frames is a list of strings.
hl is a class that I use to store which highlights are enabled.
class HighlightModel : public QObject
{
Q_PROPERTY(QString red READ getRed WRITE setRed NOTIFY redChanged)
Q_PROPERTY(QString blue READ getGreen WRITE setBlue NOTIFY blueChanged)
Q_PROPERTY(QString green READ getBlue WRITE setGreen NOTIFY greenChanged)
public:
void setRed(QString &frame)
{
if(redHighlights.find(frame) != redHighlights.end())
{
redHighlights[frame] = !redHighlights[frame];
}
else
{
redHighlights.insert(std::pair<QString, bool>(frame, true));
}
emit redChanged();
}
void setGreen(QString &frame)
{
if(greenHighlights.find(frame) != greenHighlights.end())
{
greenHighlights[frame] = !greenHighlights[frame];
}
else
{
greenHighlights.insert(std::pair<QString, bool>(frame, true));
}
emit greenChanged();
}
void setBlue(QString &frame)
{
if(blueHighlights.find(frame) != blueHighlights.end())
{
blueHighlights[frame] = !blueHighlights[frame];
}
else
{
blueHighlights.insert(std::pair<QString, bool>(frame, true));
}
emit blueChanged();
}
bool getRed(QString frame)
{
return redHighlights[frame];
}
bool getGreen(QString frame)
{
return greenHighlights[frame];
}
bool getBlue(QString frame)
{
return blueHighlights[frame];
}
std::map<QString, bool> redHighlights;
std::map<QString, bool> greenHighlights;
std::map<QString, bool> blueHighlights;
signals:
void redChanged();
void greenChanged();
void blueChanged();
private:
};
My understand of Q_PROPERTY is that setting WRITE to setRed, setBlue etc. should mean that when I set hl.red to something, it should call the function I associate with WRITE. However, I have put breakpoints in the code there and that code is never run. When I use console.log to print hl.blue it just prints the string that I tried to pass before. Can anyone clear this up for me? Have I misinterpreted how this works?
EDIT
Here is the code where I associate the HighlightModel with the engine:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
qDebug() << VERSION_INFO;
FrameViewerWindow engine;
InfoHolder ver;
HighlightModel highlights;
engine.rootContext()->setContextProperty("ver", &ver);
engine.rootContext()->setContextProperty("hl", &highlights);
engine.addImageProvider("images", new ImageLoader(QQuickImageProvider::Image, &highlights));
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}

Related

"Unable to open file"

This is one of my first projects using QML: I started making a simple game character and a few rooms, and to make the walls'hitboxes I stored them all into a file and included a FileIO class I saw in this site, adapted to return the entire file instead of just one line.
The program, though, tells me it isn't able to open the file (I have put it in the project's directory)
//main.qml (ignore line 5)
import QtQuick 2.15
import QtQuick.Shapes 1.12
import FileIO 1.0
// Whats this? UwU a byzantiuwm?!?!!? UwU i wove byzantiuwm, especiwwawwy in the #memes channew wewe byzantiuwm is the tawk of ze town! OwO
Rectangle {
width: 639
height: 480
id: sus // I hate myself
color: "#00ff00"
Shape {
property string d: "S"
property int room: 1
id: player
x: xPos
y: yPos
z: 1
visible: true
property int xPos: 297
property int yPos: 219
parent: sus
width: 45
height: 45
focus: true
Keys.onPressed:
{
if(event.key === Qt.Key_Down){
yPos+=3; d="S"
}
if(event.key === Qt.Key_Up){
yPos-=3; d="N"
}
if(event.key === Qt.Key_Left){
xPos-=3; d="W"
}
if(event.key === Qt.Key_Right){
xPos+=3; d="E"
}
event.accepted = true;
}
ShapePath{
strokeColor: "transparent"
fillColor: "transparent"
PathLine{x:45 ;y: 0}
PathLine{x:45 ;y:45}
PathLine{x: 0 ;y:45}
}
Image {
id: sprite
parent: player
width: parent.width
height: parent.height
smooth: false
source: "Player/Player_" + player.d + ".png"
}
}
Image {
id: background
x: 0
y: 0
z: 0
width: 639
height: 480
visible: true
smooth: false
parent: sus
source: "Rooms/" + player.room + ".png"
fillMode: Image.Stretch
}
Shape {
id: wallHitBox
x: 0
y: 0
z: 50
width: sus.width
height: sus.height
//visible: false
FileIO{
id: fileReader
source: "Rooms/Hitboxes_or_something.txt"
onError: {console.log(msg)}
}
property var the: fileReader.read()
ShapePath{
strokeColor: "transparent"
fillColor: "blue" //"transparent"
PathSvg{
path: wallHitBox.the[player.room];
}
}
}
}
//fileio.cpp
#include "fileio.h"
#include <QFile>
#include <QTextStream>
FileIO::FileIO(QObject *parent) :
QObject(parent)
{
}
QVector<QString> FileIO::read()
{
if (mSource.isEmpty()){
emit error("source is empty");
return QVector<QString>();
}
QFile file(mSource);
QVector<QString> fileContent;
if ( file.open(QIODevice::ReadOnly) ) {
QString line;
QTextStream t( &file );
do {
line = t.readLine();
fileContent.resize(fileContent.size()+1);
fileContent[fileContent.size()] = line;
} while (!line.isNull());
file.close();
} else {
emit error("Unable to open the file");
return QVector<QString>();
}
return fileContent;
}
bool FileIO::write(const QString& data)
{
if (mSource.isEmpty())
return false;
QFile file(mSource);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
return false;
QTextStream out(&file);
out << data;
file.close();
return true;
}
#define FILEIO_H
#include <QObject>
class FileIO : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QString source
READ source
WRITE setSource
NOTIFY sourceChanged)
explicit FileIO(QObject *parent = 0);
Q_INVOKABLE QVector<QString> read();
Q_INVOKABLE bool write(const QString& data);
QString source() { return mSource; };
public slots:
void setSource(const QString& source) { mSource = source; };
signals:
void sourceChanged(const QString& source);
void error(const QString& msg);
private:
QString mSource;
};
#endif // FILEIO_H
Can someone please help me?

Highlighting the country on the map when you hover over

I want to highlight the country when I hover over it. To do this, I took QStandardItemModel with the polygons of the desired countries.
The problem is that the onClicked event works as expected, but onEntered and onExited fire in the rectangle, at the extreme coordinates, and not in the polygon. Is it possible for onEntered and onExited to work in the same area as onClicked? Perhaps there is some other solution for highlighting countries? I looked towards MaskedMouseArea but as a resource you need to set QImage. Thanks in advance for any help.
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QGeoPolygon>
#include <QStandardItem>
#include <QQmlContext>
class PolygonItem: public QStandardItem
{
public:
PolygonItem(QString Name, const QGeoPolygon &polygon);
QString Name() const;
QGeoPolygon polygon() const;
};
class PolygonItemModel: public QStandardItemModel{
public:
PolygonItemModel(QObject *parent=nullptr);
void addPolygonItem(PolygonItem *item);
};
enum PolygonRole{
NameRole,
GeopolygonRole,
};
PolygonItem::PolygonItem(QString name, const QGeoPolygon &polygon):
QStandardItem()
{
setData(name, NameRole);
setData(QVariant::fromValue(polygon), GeopolygonRole);
}
PolygonItemModel::PolygonItemModel(QObject *parent):
QStandardItemModel(parent)
{
setItemRoleNames({{NameRole, "name"},
{GeopolygonRole, "polygon"}});
}
void PolygonItemModel::addPolygonItem(PolygonItem *item)
{
appendRow(item);
}
const std::vector<std::pair<double, double>> Namibia =
{
{
{14.52484512329101562,-22.69206619262695312},
{14.52739906311035156,-22.6679534912109375},
{14.52458286285400391,-22.62903022766113281},
{14.51138877868652344,-22.5527801513671875},
{14.41638755798339844,-22.33444595336914062},
{14.38763809204101562,-22.28347396850585938},
{14.28583145141601562,-22.1233367919921875},
{14.127777099609375,-21.94055557250976562},
{14.10249900817871094,-21.91403007507324219},
{14.04249954223632812,-21.86222457885742188},
{13.95267200469970703,-21.77889060974121094},
{13.96798419952392578,-21.73465538024902344},
{13.95666599273681641,-21.70194625854492188},
{13.93555545806884766,-21.67444610595703125},
{13.87791538238525391,-21.59458541870117188},
{13.70111083984375,-21.296112060546875},
{13.57694435119628906,-21.12916946411132812},
{13.40388774871826172,-20.86236381530761719},
{13.38499832153320312,-20.8244476318359375},
{13.19888877868652344,-20.23833465576171875},
{13.17499923706054688,-20.18083572387695312},
{13.15972137451171875,-20.15472412109375},
{13.14027595520019531,-20.13111114501953125},
{13.11666584014892578,-20.11507225036621094},
{13.08444404602050781,-20.09944534301757812},
{13.05569267272949219,-20.07361221313476562},
{13.04194355010986328,-20.04639053344726562},
{13.03027725219726562,-20.00139236450195312},
{12.989166259765625,-19.90944671630859375},
{12.9797210693359375,-19.89139175415039062},
{12.97027587890625,-19.8733367919921875},
{12.9444427490234375,-19.82389068603515625},
{12.92777633666992188,-19.789306640625},
{12.81194305419921875,-19.59833526611328125},
{12.79138755798339844,-19.56500244140625},
{12.77333641052246094,-19.539093017578125},
{12.70722198486328125,-19.41777801513671875},
{12.69861030578613281,-19.399169921875},
{12.68805503845214844,-19.36944580078125},
{12.6399993896484375,-19.26166915893554688},
{12.58694267272949219,-19.15250015258789062},
{12.5422210693359375,-19.067779541015625},
{12.46083259582519531,-18.92805862426757812},
{12.4416656494140625,-18.899169921875},
{12.41874885559082031,-18.86652946472167969},
{12.30666542053222656,-18.71722412109375},
{12.29083251953125,-18.70083427429199219},
{12.23083305358886719,-18.6505584716796875},
{12.21360969543457031,-18.63861465454101562},
{12.19666481018066406,-18.62694549560546875},
{12.17458248138427734,-18.61277961730957031},
{12.14611053466796875,-18.59305572509765625},
{12.12555503845214844,-18.57777786254882812},
{12.08305454254150391,-18.53555679321289062},
{12.02083206176757812,-18.47111129760742188},
{11.99791622161865234,-18.41333580017089844},
{11.99749946594238281,-18.37430572509765625},
{11.95305442810058594,-18.26583480834960938},
{11.94388866424560547,-18.24777984619140625},
{11.89235973358154297,-18.1805572509765625},
{11.84708309173583984,-18.13916778564453125},
{11.80722141265869141,-18.08625221252441406},
{11.76763820648193359,-17.98819541931152344},
{11.76111030578613281,-17.9618072509765625},
{11.741943359375,-17.83028030395507812},
{11.7324981689453125,-17.7619476318359375},
{11.71666526794433594,-17.56416702270507812},
{11.71638870239257812,-17.5391693115234375},
{11.71722221374511719,-17.483612060546875},
{11.72055435180664062,-17.45722579956054688},
{11.73222160339355469,-17.372222900390625},
{11.74777603149414062,-17.32972335815429688},
{11.74972152709960938,-17.30916976928710938},
{11.75373554229736328,-17.26501083374023438},
{11.75278282165527344,-17.25483322143554688},
{11.81166553497314453,-17.2702789306640625},
{11.88458251953125,-17.22444725036621094},
{11.9022216796875,-17.20583343505859375},
{11.93305397033691406,-17.18069648742675781},
{11.97611045837402344,-17.1641693115234375},
{12.04805374145507812,-17.143890380859375},
{12.08777618408203125,-17.13652992248535156},
{12.20583152770996094,-17.19472503662109375},
{12.23805427551269531,-17.21527862548828125},
{12.2922210693359375,-17.22972488403320312},
{12.43416595458984375,-17.21277999877929688},
{12.47888755798339844,-17.241668701171875},
{12.557220458984375,-17.24333572387695312},
{12.60444259643554688,-17.22583389282226562},
{12.81791591644287109,-17.10708427429199219},
{12.85138702392578125,-17.07250213623046875},
{12.87979030609130859,-17.04014015197753906},
{12.92333221435546875,-17.01639175415039062},
{12.98583221435546875,-16.99111175537109375},
{13.01847171783447266,-16.978057861328125},
{13.14916610717773438,-16.95417022705078125},
{13.17090129852294922,-16.95631980895996094},
{13.20888710021972656,-16.97361373901367188},
{13.26083183288574219,-16.98486328125},
{13.31777763366699219,-16.978057861328125},
{13.34630966186523438,-16.97066879272460938},
{13.37604045867919922,-16.97090530395507812},
{13.47208213806152344,-17.01083564758300781},
{13.49430465698242188,-17.02555656433105469},
{13.52801895141601562,-17.0875701904296875},
{13.66069316864013672,-17.21889114379882812},
{13.69777679443359375,-17.24139022827148438},
{13.84138870239257812,-17.32361221313476562},
{13.92888832092285156,-17.40111160278320312},
{13.99321937561035156,-17.42394638061523438},
{14.01666545867919922,-17.4109039306640625},
{14.05110931396484375,-17.41944503784179688},
{14.082916259765625,-17.43000221252441406},
{14.17138767242431641,-17.42111396789550781},
{14.19472122192382812,-17.41139030456542969},
{14.21805381774902344,-17.3869476318359375},
{14.36166572570800781,-17.3872222900390625},
{14.44805526733398438,-17.38750076293945312},
{14.63472175598144531,-17.388336181640625},
{14.99388885498046875,-17.3897247314453125},
{15.49694442749023438,-17.38944625854492188},
{15.58305454254150391,-17.38916778564453125},
{15.9566650390625,-17.38666915893554688},
{16.00333023071289062,-17.38666915893554688},
{16.606109619140625,-17.39083480834960938},
{17.27883148193359375,-17.39222335815429688},
{17.51055526733398438,-17.39194488525390625},
{17.56166458129882812,-17.39083480834960938},
{17.73388671875,-17.38888931274414062},
{18.00054931640625,-17.38616943359375},
{18.11722183227539062,-17.3869476318359375},
{18.1888885498046875,-17.3872222900390625},
{18.3899993896484375,-17.3869476318359375},
{18.4515380859375,-17.38983535766601562},
{18.51333236694335938,-17.4727783203125},
{18.57833099365234375,-17.57222366333007812},
{18.64194107055664062,-17.6480560302734375},
{18.7536773681640625,-17.74527931213378906},
{18.80430412292480469,-17.768890380859375},
{18.91944313049316406,-17.81638908386230469},
{18.98388671875,-17.8259735107421875},
{19.0258331298828125,-17.8300018310546875},
{19.0558319091796875,-17.82777786254882812},
{19.11680221557617188,-17.81944656372070312},
{19.13360977172851562,-17.80437660217285156},
{19.1752777099609375,-17.80111312866210938},
{19.24555397033691406,-17.80625152587890625},
{19.29874801635742188,-17.82500076293945312},
{19.33721923828125,-17.84389114379882812},
{19.413330078125,-17.861114501953125},
{19.445831298828125,-17.86278152465820312},
{19.55916595458984375,-17.86750030517578125},
{19.701385498046875,-17.87055587768554688},
{19.79791450500488281,-17.86375236511230469},
{19.81860923767089844,-17.85805702209472656},
{19.92055511474609375,-17.8572235107421875},
{19.94624710083007812,-17.86333465576171875},
{19.97131729125976562,-17.88014030456542969},
{20.01694107055664062,-17.89083480834960938},
{20.09888839721679688,-17.89638900756835938},
{20.12888717651367188,-17.89444732666015625},
{20.244720458984375,-17.88014030456542969},
{20.27083206176757812,-17.8612518310546875},
{20.32131767272949219,-17.8572235107421875},
{20.44520378112792969,-17.90187644958496094},
{20.4925537109375,-17.94189071655273438},
{20.55367851257324219,-17.98305702209472656},
{20.59555435180664062,-17.98139190673828125},
{20.63846969604492188,-17.97847366333007812},
{20.75194168090820312,-17.99611282348632812},
{20.781585693359375,-18.01020431518554688},
{20.85416412353515625,-18.01639175415039062},
{20.88972091674804688,-17.99444580078125},
{20.95333099365234375,-17.96826553344726562},
{21.02138519287109375,-17.95333480834960938},
{21.13861083984375,-17.9344482421875},
{21.16586685180664062,-17.93146705627441406},
{21.23687362670898438,-17.93889045715332031},
{21.26333045959472656,-17.95639228820800781},
{21.37935638427734375,-18.01488876342773438},
{21.75540542602539062,-17.94734573364257812},
{22.29027557373046875,-17.85000228881835938},
{22.5641632080078125,-17.79888916015625},
{22.88110733032226562,-17.73944473266601562},
{22.90999984741210938,-17.73333358764648438},
{22.99638748168945312,-17.71694564819335938},
{23.28472137451171875,-17.66250228881835938},
{23.28916549682617188,-17.66111373901367188},
{23.47610855102539062,-17.62583541870117188},
{23.820831298828125,-17.56027984619140625},
{24.02902603149414062,-17.51958465576171875},
{24.23908615112304688,-17.47843170166015625},
{24.38124847412109375,-17.47347259521484375},
{24.44013786315917969,-17.48236465454101562},
{24.50249862670898438,-17.508056640625},
{24.5422210693359375,-17.52500152587890625},
{24.5654144287109375,-17.53347396850585938},
{24.59027481079101562,-17.5338897705078125},
{24.61916542053222656,-17.50583648681640625},
{24.63819313049316406,-17.49666976928710938},
{24.66388702392578125,-17.49361419677734375},
{24.70861053466796875,-17.4983367919921875},
{24.81777572631835938,-17.51555633544921875},
{24.91249847412109375,-17.54083633422851562},
{24.96999740600585938,-17.559722900390625},
{24.98430442810058594,-17.58389091491699219},
{25.029998779296875,-17.6100006103515625},
{25.065277099609375,-17.624725341796875},
{25.25194168090820312,-17.78305816650390625},
{25.26443099975585938,-17.80224990844726562},
{25.25237846374511719,-17.79570960998535156},
{25.17937469482421875,-17.78208541870117188},
{25.13638687133789062,-17.79656410217285156},
{25.09909439086914062,-17.82784843444824219},
{25.06638717651367188,-17.83222579956054688},
{25,-17.8260498046875},
{24.97277450561523438,-17.82111358642578125},
{24.93610763549804688,-17.81389236450195312},
{24.83277511596679688,-17.83770942687988281},
{24.67916488647460938,-17.9441680908203125},
{24.56631660461425781,-18.05423736572265625},
{24.52416610717773438,-18.06000137329101562},
{24.49791336059570312,-18.05930709838867188},
{24.47360992431640625,-18.03277969360351562},
{24.45194244384765625,-17.99916839599609375},
{24.439788818359375,-17.97805595397949219},
{24.41694259643554688,-17.9537506103515625},
{24.38833236694335938,-17.94583511352539062},
{24.36305427551269531,-17.94916725158691406},
{24.26888656616210938,-18.013336181640625},
{24.00666427612304688,-18.16777801513671875},
{23.96722030639648438,-18.18500137329101562},
{23.91222000122070312,-18.23694610595703125},
{23.88694000244140625,-18.26476287841796875},
{23.87041473388671875,-18.26305770874023438},
{23.84402656555175781,-18.29000282287597656},
{23.39805221557617188,-18.17611312866210938},
{23.3630523681640625,-18.12722396850585938},
{23.32999801635742188,-18.07638931274414062},
{23.32583236694335938,-18.04500198364257812},
{23.31638717651367188,-18.01541900634765625},
{23.29710769653320312,-17.99594879150390625},
{23.28711318969726562,-17.99633598327636719},
{23.0777740478515625,-18.00444793701171875},
{22.63721847534179688,-18.086669921875},
{22.5,-18.11126708984375},
{22.26221847534179688,-18.15389251708984375},
{21.81694412231445312,-18.23833465576171875},
{21.46249771118164062,-18.30444717407226562},
{21.23555374145507812,-18.31194686889648438},
{20.9932861328125,-18.31841659545898438},
{20.99361038208007812,-18.33861160278320312},
{20.99416351318359375,-18.47555923461914062},
{20.99416351318359375,-18.69527816772460938},
{20.9935302734375,-19.17194747924804688},
{20.99307632446289062,-19.99822235107421875},
{20.9933319091796875,-20.179168701171875},
{20.99280929565429688,-20.82427215576171875},
{20.99303054809570312,-20.99245071411132812},
{20.99249649047851562,-21.018890380859375},
{20.99249649047851562,-21.32222366333007812},
{20.99277496337890625,-21.59722518920898438},
{20.99277496337890625,-21.94194793701171875},
{20.99277496337890625,-21.96944808959960938},
{20.991943359375,-21.9969482421875},
{20.91777420043945312,-21.9983367919921875},
{20.64971923828125,-22},
{20.30833053588867188,-22.00139236450195312},
{20.0004730224609375,-22.00156402587890625},
{19.99666595458984375,-22.00500106811523438},
{19.9961090087890625,-22.08750152587890625},
{19.99721908569335938,-22.26694488525390625},
{19.99759483337402344,-22.5},
{19.99749755859375,-22.722503662109375},
{19.99777603149414062,-22.94333648681640625},
{19.99827766418457031,-22.95992088317871094},
{19.99860763549804688,-22.97083663940429688},
{19.9988861083984375,-23.12277984619140625},
{19.9989013671875,-23.30869674682617188},
{19.99944305419921875,-23.45417022705078125},
{20,-23.64750289916992188},
{20.000274658203125,-23.75722503662109375},
{20.00053787231445312,-23.864410400390625},
{20,-23.950836181640625},
{20.00055313110351562,-24.28222274780273438},
{20.00111007690429688,-24.40639114379882812},
{20.00194168090820312,-24.57222366333007812},
{20.00222015380859375,-24.7241668701171875},
{20.0016632080078125,-24.76555633544921875},
{20.00094223022460938,-24.76540756225585938},
{20.0016632080078125,-24.79361343383789062},
{20.0016632080078125,-24.86278152465820312},
{20.0013885498046875,-25.08361434936523438},
{20.00055313110351562,-25.22166824340820312},
{20.000274658203125,-25.41500091552734375},
{20.00043487548828125,-25.61648941040039062},
{20,-25.8708343505859375},
{20,-26.40916824340820312},
{19.99944305419921875,-27.03028106689453125},
{19.99916458129882812,-27.32027816772460938},
{19.9987640380859375,-27.36949920654296875},
{19.99833297729492188,-27.48583602905273438},
{19.99805450439453125,-27.58222579956054688},
{19.99777603149414062,-27.8719482421875},
{19.99833297729492188,-28.06500244140625},
{19.99805450439453125,-28.35472488403320312},
{19.9969482421875,-28.41558647155761719},
{19.81221961975097656,-28.49250221252441406},
{19.78972053527832031,-28.49555778503417969},
{19.74360847473144531,-28.48555564880371094},
{19.56745719909667969,-28.52833175659179688},
{19.51333236694335938,-28.595001220703125},
{19.49388885498046875,-28.633056640625},
{19.490692138671875,-28.66902923583984375},
{19.46638870239257812,-28.6997222900390625},
{19.44777679443359375,-28.71083450317382812},
{19.41833114624023438,-28.71889114379882812},
{19.34902572631835938,-28.73583602905273438},
{19.32249832153320312,-28.72750282287597656},
{19.30138587951660156,-28.72541999816894531},
{19.27721977233886719,-28.73125267028808594},
{19.26124763488769531,-28.74486160278320312},
{19.23978996276855469,-28.80243110656738281},
{19.24805450439453125,-28.83166885375976562},
{19.25999832153320312,-28.85541915893554688},
{19.25388717651367188,-28.899444580078125},
{19.18388748168945312,-28.93722343444824219},
{19.14583015441894531,-28.9550018310546875},
{19.10833168029785156,-28.96187591552734375},
{19.00249671936035156,-28.92791938781738281},
{18.98569107055664062,-28.90500068664550781},
{18.97041511535644531,-28.8776397705078125},
{18.95152473449707031,-28.86639022827148438},
{18.71916389465332031,-28.83653068542480469},
{18.3383331298828125,-28.8844451904296875},
{18.18131637573242188,-28.90847206115722656},
{18.04444313049316406,-28.86812591552734375},
{18.02610969543457031,-28.84986305236816406},
{18.0013885498046875,-28.82694625854492188},
{17.96992874145507812,-28.80409812927246094},
{17.91249847412109375,-28.77916717529296875},
{17.74972152709960938,-28.74639129638671875},
{17.7108306884765625,-28.7566680908203125},
{17.67930412292480469,-28.76986312866210938},
{17.64340019226074219,-28.77396202087402344},
{17.60527610778808594,-28.75312614440917969},
{17.6022186279296875,-28.72069549560546875},
{17.58975791931152344,-28.68954277038574219},
{17.49777603149414062,-28.69472503662109375},
{17.46110916137695312,-28.70277786254882812},
{17.27881622314453125,-28.23826408386230469},
{17.21881294250488281,-28.24207305908203125},
{17.1986083984375,-28.22361373901367188},
{17.18437004089355469,-28.20049667358398438},
{17.18833160400390625,-28.17165756225585938},
{17.18902015686035156,-28.11410903930664062},
{17.17165756225585938,-28.102264404296875},
{17.15413856506347656,-28.09749794006347656},
{17.13430404663085938,-28.08708572387695312},
{17.12041473388671875,-28.06541824340820312},
{17.10791397094726562,-28.04583549499511719},
{17.07576179504394531,-28.0303497314453125},
{16.91187286376953125,-28.06750297546386719},
{16.88694190979003906,-28.09236335754394531},
{16.82249832153320312,-28.23778152465820312},
{16.80159568786621094,-28.3627777099609375},
{16.77666473388671875,-28.44166946411132812},
{16.71951103210449219,-28.49902915954589844},
{16.70062255859375,-28.49034881591796875},
{16.67711639404296875,-28.46430587768554688},
{16.62499618530273438,-28.49611282348632812},
{16.48958969116210938,-28.57817840576171875},
{16.451629638671875,-28.61462020874023438},
{16.39332962036132812,-28.59111404418945312},
{16.35444259643554688,-28.56277847290039062},
{16.17194366455078125,-28.399169921875},
{16.15805435180664062,-28.38389205932617188},
{16.11722183227539062,-28.33750152587890625},
{16.04972076416015625,-28.26639175415039062},
{16.02319145202636719,-28.241668701171875},
{15.99749946594238281,-28.22916793823242188},
{15.97333145141601562,-28.21500015258789062},
{15.89861106872558594,-28.16278076171875},
{15.75638771057128906,-28.03416824340820312},
{15.73611068725585938,-28.01111221313476562},
{15.68972206115722656,-27.95611190795898438},
{15.67652606964111328,-27.929168701171875},
{15.68270683288574219,-27.90909957885742188},
{15.67138767242431641,-27.87347412109375},
{15.64694404602050781,-27.84389114379882812},
{15.60555458068847656,-27.79750251770019531},
{15.57027626037597656,-27.76722335815429688},
{15.55249977111816406,-27.75444793701171875},
{15.53104114532470703,-27.73041915893554688},
{15.52194309234619141,-27.6818084716796875},
{15.53110980987548828,-27.66146087646484375},
{15.5191650390625,-27.62694549560546875},
{15.39916610717773438,-27.4550018310546875},
{15.36055374145507812,-27.40722274780273438},
{15.29416656494140625,-27.32250213623046875},
{15.28722190856933594,-27.30222320556640625},
{15.264373779296875,-27.21472358703613281},
{15.26999855041503906,-27.16222381591796875},
{15.23527717590332031,-26.96944427490234375},
{15.21861076354980469,-26.93666839599609375},
{15.20221996307373047,-26.92333412170410156},
{15.17916488647460938,-26.9183349609375},
{15.15777587890625,-26.90277862548828125},
{15.11249923706054688,-26.7838897705078125},
{15.09499931335449219,-26.73527908325195312},
{15.08402633666992188,-26.69687652587890625},
{15.07972145080566406,-26.65000152587890625},
{15.09402751922607422,-26.63416862487792969},
{15.13173484802246094,-26.63170242309570312},
{15.13680458068847656,-26.67465400695800781},
{15.16666603088378906,-26.6233367919921875},
{15.17180442810058594,-26.60166740417480469},
{15.13110923767089844,-26.47861480712890625},
{15.11736011505126953,-26.44708633422851562},
{15.0880889892578125,-26.40316390991210938},
{15.04749870300292969,-26.375},
{15.02319431304931641,-26.36486244201660156},
{14.991943359375,-26.35361480712890625},
{14.96986007690429688,-26.34014129638671875},
{14.95916557312011719,-26.30805587768554688},
{14.94736003875732422,-26.13736343383789062},
{14.97430419921875,-26.13020896911621094},
{14.98166656494140625,-26.08889007568359375},
{14.98083305358886719,-26.06347465515136719},
{14.96944427490234375,-26.01416778564453125},
{14.95749855041503906,-25.98777961730957031},
{14.93555545806884766,-25.9630584716796875},
{14.91527652740478516,-25.93680763244628906},
{14.83777618408203125,-25.7619476318359375},
{14.8343048095703125,-25.74194717407226562},
{14.84916496276855469,-25.63361167907714844},
{14.86472129821777344,-25.59028053283691406},
{14.88090133666992188,-25.56729316711425781},
{14.84097099304199219,-25.41861343383789062},
{14.82999897003173828,-25.40139198303222656},
{14.81708240509033203,-25.37916946411132812},
{14.80097103118896484,-25.28042030334472656},
{14.81166553497314453,-25.25333404541015625},
{14.83416557312011719,-25.1875},
{14.85777664184570312,-25.08722305297851562},
{14.85666561126708984,-25.05888938903808594},
{14.83888816833496094,-25.01139068603515625},
{14.83207321166992188,-24.99943923950195312},
{14.467498779296875,-23.15513992309570312},
{14.68944358825683594,-23.20444488525390625},
{14.71277618408203125,-23.16055679321289062},
{14.690277099609375,-23.07305908203125},
{14.66805458068847656,-22.98416900634765625},
{14.66944313049316406,-22.93194580078125},
{14.6659698486328125,-22.63843727111816406},
{14.64430522918701172,-22.63916778564453125},
{14.53888702392578125,-22.6833343505859375},
{14.52484512329101562,-22.69206619262695312}
}
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
PolygonItemModel model;
engine.rootContext()->setContextProperty("polygonmodel", &model);
const QUrl url(QStringLiteral("qrc:/main.qml"));
engine.load(url);
QGeoPolygon polygon;
for (int i = 0; i < Namibia.size(); i++)
{
polygon.addCoordinate(QGeoCoordinate(Namibia[i].second, Namibia[i].first));
}
PolygonItem *item = new PolygonItem("Namibia", polygon);
model.addPolygonItem(item);
return app.exec();
}
qml
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Map {
id: map
anchors.fill: parent
center: QtPositioning.coordinate(45.137451890638886, -68.13734351262877)
plugin: Plugin {
name: "osm"
}
zoomLevel: 1
MapItemView{
model: polygonmodel
delegate: MapPolygon {
color: "blue"
border.color: "green"
border.width: 1
smooth: true
opacity: 0.25
geoShape: model.polygon
MouseArea{
anchors.fill: parent
hoverEnabled : true
onEntered:
{
border.color = "red"
border.width =3
}
onExited:
{
border.color = "white"
border.width = 1
}
onClicked:
{
console.log("onClicked:")
}
}
}
}
}
}

QSyntaxHighlighter rehighlight() ignores changes triggered from another thread

I have a spellchecking thread which fires spellcheck() signals from time to time which are connected to my highlighter's rehighlight() method. The latter sets the whole block to have red foreground.
This used to work in Qt 5.6.2 and ceased to work in newer versions. I hopelessly waited for it to get fixed in Qt 5.9.5, but it still does not work (neither in Windows 10, nor in OS X).
Small example which reproduces the problem could be obtained from here https://bitbucket.org/ribtoks/qt-highlighting-issue (in order to repro, type something in the input. rehighlight() will be triggered each 7 seconds from background thread)
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickTextDocument>
#include <QSyntaxHighlighter>
#include <QThread>
#include <QString>
class SpellCheckWorker : public QObject
{
Q_OBJECT
public:
explicit SpellCheckWorker(QObject *parent = 0) : QObject(parent), m_Counter(0), m_IsOK(false)
{ }
public:
bool isOK() { return m_IsOK; }
signals:
void spellcheck();
public slots:
void process() {
qInfo() << "Worker Thread is" << QThread::currentThreadId();
while (1) {
m_Counter++;
m_IsOK = m_Counter % 7 == 0;
QThread::sleep(1);
emit spellcheck();
}
}
private:
int m_Counter;
volatile bool m_IsOK;
};
class SpellCheckErrorsHighlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
SpellCheckErrorsHighlighter(SpellCheckWorker *worker, QTextDocument *document):
QSyntaxHighlighter(document),
m_Worker(worker)
{ }
virtual ~SpellCheckErrorsHighlighter() {}
protected:
virtual void highlightBlock(const QString &text) override {
if (!m_Worker->isOK()) {
qDebug() << "Worker is not OK" << text;
return;
}
qInfo() << "Reapplied formatting for" << text;
qInfo() << "Highlight thread is" << QThread::currentThreadId();
this->setFormat(0, text.length(), QColor(0xff, 0, 0));
}
private:
SpellCheckWorker *m_Worker;
};
class MainModel : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
explicit MainModel(QObject *parent = 0) : QObject(parent), m_Worker(nullptr)
{ }
public:
QString name() const { return m_name; }
void startChecking() {
qInfo() << "Main thread is" << QThread::currentThreadId();
m_Worker = new SpellCheckWorker();
QThread *thread = new QThread();
m_Worker->moveToThread(thread);
QObject::connect(thread, &QThread::started, m_Worker, &SpellCheckWorker::process);
thread->start();
}
Q_INVOKABLE void initNameHighlighting(QQuickTextDocument *document) {
SpellCheckErrorsHighlighter *highlighter = new SpellCheckErrorsHighlighter(m_Worker, document->textDocument());
QObject::connect(m_Worker, &SpellCheckWorker::spellcheck,
highlighter, &SpellCheckErrorsHighlighter::rehighlight);
}
signals:
void nameChanged();
public slots:
void setName(QString name)
{
if (m_name == name)
return;
m_name = name;
emit nameChanged();
}
private:
SpellCheckWorker *m_Worker;
QString m_name;
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
MainModel mainModel;
mainModel.startChecking();
QQmlApplicationEngine engine;
QQmlContext *rootContext = engine.rootContext();
rootContext->setContextProperty("mainModel", &mainModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
#include "main.moc"
and main.qml
import QtQuick 2.6
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.3
import QtQuick.Window 2.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
StackView {
id: mainStackView
anchors.fill: parent
focus: true
initialItem: Rectangle {
anchors.fill: parent
Rectangle {
id: titleRect
height: 30
width: 300
anchors.centerIn: parent
color: "#ffffff"
border.color: "#000000"
border.width: titleTextInput.activeFocus ? 1 : 0
clip: true
focus: false
Flickable {
id: titleFlick
contentWidth: titleTextInput.paintedWidth
contentHeight: titleTextInput.paintedHeight
height: parent.height
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: 5
anchors.rightMargin: 5
clip: true
flickableDirection: Flickable.HorizontalFlick
interactive: false
focus: false
function ensureVisible(r) {
if (contentX >= r.x)
contentX = r.x;
else if (contentX+width <= r.x+r.width)
contentX = r.x+r.width-width;
}
TextEdit {
id: titleTextInput
width: paintedWidth > titleFlick.width ? paintedWidth : titleFlick.width
height: titleFlick.height
text: mainModel.name
focus: true
onTextChanged: mainModel.name = text
Component.onCompleted: mainModel.initNameHighlighting(titleTextInput.textDocument)
onCursorRectangleChanged: titleFlick.ensureVisible(cursorRectangle)
}
}
}
}
}
}
Is there any way to get it working with workarounds? I need to keep spellchecking logic in the background thread so it's not possible to move it to main thread.
You seem to be referring to two different root causes with the following two statements:
There's NO problem in delivering slot call to Highlighter.
and
m_Worker->isOK() check is there for a reason to execute highlighting only once per 7 seconds. The bug in Qt can only be demonstrated with this.
If I just focus on the second statement, it looks like the problem is that you are not able to hit the statement if (!m_Worker->isOK()) right?
That may well be a problem due to your code or a Qt issue on a specific platform.
Can you change the code to avoid the condition i.e. emit the spellcheck signal only when 7 seconds have passed to avoid making this check from another thread?
Ok, so after messing with your code, The issue is that you are not calling this line inside of the hilightBlock directive in main.cpp near line 55 in order to keep the format from before.
Add this in to fix it (I think) The question was still fairly unclear...
if (!m_Worker->isOK()) {
qDebug() << "Worker is not OK" << text;
this->setFormat(0, text.length(), this->format(0));
return;
}
The other part of the issue is that you don't have any rules defined for the syntax highlighter... so it will always be red once it turns red.

C++ class exposed to QML error in fashion TypeError: Property '...' of object is not a function

I've successfully exposed a C++ class to QML. It is registered and found in Qt Creator. It's purpose is to connect to a database, as shown in following code:
#ifndef UESQLDATABASE_H
#define UESQLDATABASE_H
#include <QObject>
#include <QtSql/QSqlDatabase>
class UeSqlDatabase : public QObject
{
Q_OBJECT
Q_PROPERTY(bool m_ueConnected READ isConnected WRITE setConnected NOTIFY ueConnectedChanged)
private:
bool m_ueConneted;
inline void setConnected(const bool& ueConnected)
{ this->m_ueConneted=ueConnected; }
public:
explicit UeSqlDatabase(QObject *parent = 0);
Q_INVOKABLE inline const bool& isConnected() const
{ return this->m_ueConneted; }
~UeSqlDatabase();
signals:
void ueConnectedChanged();
public slots:
void ueConnectToDatabase (const QString& ueStrHost, const QString& ueStrDatabase,
const QString& ueStrUsername, const QString& ueStrPassword);
};
#endif // UESQLDATABASE_H
However, when I try to call method isConnected() from the following QML code
import QtQuick 2.0
Rectangle
{
id: ueMenuButton
property string ueText;
width: 192
height: 64
radius: 8
states: [
State
{
name: "ueStateSelected"
PropertyChanges
{
target: gradientStop1
color: "#000000"
}
PropertyChanges
{
target: gradientStop2
color: "#3fe400"
}
}
]
gradient: Gradient
{
GradientStop
{
id: gradientStop1
position: 0
color: "#000000"
}
GradientStop
{
position: 0.741
color: "#363636"
}
GradientStop
{
id: gradientStop2
position: 1
color: "#868686"
}
}
border.color: "#ffffff"
border.width: 2
antialiasing: true
Text
{
id: ueButtonText
color: "#ffffff"
text: qsTr(ueText)
clip: false
z: 0
scale: 1
rotation: 0
font.strikeout: false
anchors.fill: parent
font.bold: true
style: Text.Outline
textFormat: Text.RichText
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 16
}
MouseArea
{
id: ueClickArea
antialiasing: true
anchors.fill: parent
onClicked:
{
uePosDatabase.ueConnectToDatabase("127.0.0.1",
"testDb",
"testUser",
"testPassword");
if(uePosDatabase.isConnected()==true)
{
ueMenuButton.state="ueStateSelected";
}
else
{
ueMenuButton.state="base state"
}
}
}
}
I get the following error:
qrc:/UeMenuButton.qml:92: TypeError: Property 'isConnected' of object UeSqlDatabase(0x1772060) is not a function
What am I doing wrong?
You have this error because you have declared the property isConnected in C++ but you're calling it from QML in the wrong way: uePosDatabase.isConnected is the correct way, not uePosDatabase.isConnected().
If you want to call the function isConnected() you should change its name to differate it from the property, like getIsConnected(). Given your property declaration, you neither need to call this function directly nor you need to make it callable from QML with the Q_INVOKABLE macro.
You must first create the object you want in QML code and then use the function:
your QML Code:
import QtQuick 2.0
import QSqlDatabase 1.0
Rectangle {
id: ueMenuButton
QSqlDatabase {
id: uePosDatabase
}
.
.
.
MouseArea
{
id: ueClickArea
antialiasing: true
anchors.fill: parent
onClicked: {
console.log(uePosDatabase.isConnected())
}
}
}
Be aware in case of an error some lines earlier in the same .js file can lead to this problem. The following code will not be executed.

QT QML import ListModel from C++ to QML

How can i change the model of a PathView with c++ code ?
i add an objectName to my pathView to find it, then i change the property like this, but when i do that, my list is empty :
QDeclarativeItem *listSynergie = myClass.itemMain->findChild<QDeclarativeItem*> ("PathViewInscription");
listSynergie->setProperty("model", QVariant::fromValue(dataList));
My data list is fill like this :
QList<QObject*> dataList;
dataList.append(new synergieListObject("Item 1", "1",false));
dataList.append(new synergieListObject("Item 2", "2",true));
dataList.append(new synergieListObject("Item 3", "3",false));
dataList.append(new synergieListObject("Item 4", "4",false));
This is the code of my PathView :
PathView {
objectName: "PathViewInscription"
Keys.onRightPressed: if (!moving) { incrementCurrentIndex(); console.log(moving) }
Keys.onLeftPressed: if (!moving) decrementCurrentIndex()
id: view
anchors.fill: parent
highlight: Image { source: "../spinner_selecter.png"; width: view.width; height: itemHeight+4; opacity:0.3}
preferredHighlightBegin: 0.5
preferredHighlightEnd: 0.5
focus: true
model: appModel
delegate: appDelegate
dragMargin: view.width/2
pathItemCount: height/itemHeight
path: Path {
startX: view.width/2; startY: -itemHeight/2
PathLine { x: view.width/2; y: view.pathItemCount*itemHeight + itemHeight }
}
}
and the code of ListModel :
ListModel {
id: appModel
ListElement { label: "syn1"; value: "1"; selected:false}
ListElement { label: "syn2"; value: "2" ; selected:false}
ListElement { label: "syn3"; value: "3" ; selected:false}
}
what's wrong ?
Thanks !
EDIT :
the code of the appDelegate :
Component {
id: appDelegate
Item {
width: 100; height: 100
Text {
anchors { horizontalCenter: parent.horizontalCenter }
text: label
smooth: true
}
MouseArea {
anchors.fill: parent
onClicked: view.currentIndex = index
}
}
}
the code of my object :
#ifndef SYNERGIELISTOBJECT_H
#define SYNERGIELISTOBJECT_H
#include <QObject>
class synergieListObject : public QObject
{
Q_OBJECT
Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectedChanged)
public:
synergieListObject(QObject *parent=0);
synergieListObject(const QString &label,const QString &value,bool selected, QObject *parent=0);
QString label() const;
void setLabel(const QString &label);
QString value() const;
void setValue(const QString &value);
bool selected() const;
void setSelected(const bool &selected);
signals:
void labelChanged();
void valueChanged();
void selectedChanged();
private:
QString m_label;
QString m_value;
bool m_selected;
};
#endif // SYNERGIELISTOBJECT_H
c++ my object :
#include "synergielistobject.h"
synergieListObject::synergieListObject(QObject *parent): QObject(parent)
{
}
synergieListObject::synergieListObject(const QString &label,const QString &value,bool selected, QObject *parent): QObject(parent), m_label(label), m_value(value), m_selected(selected)
{
}
QString synergieListObject::label() const
{
return m_label;
}
void synergieListObject::setLabel(const QString &label)
{
if (label != m_label) {
m_label = label;
emit labelChanged();
}
}
QString synergieListObject::value() const
{
return m_value;
}
void synergieListObject::setValue(const QString &value)
{
if (value != m_value) {
m_value = value;
emit valueChanged();
}
}
bool synergieListObject::selected() const
{
return m_selected;
}
void synergieListObject::setSelected(const bool &selected)
{
if (selected != m_selected) {
m_selected = selected;
emit selectedChanged();
}
}
I have never used QdeclarativeItem to set model in QML . Try this instead
QDeclarativeContext *ctxt = view.rootContext(); ctxt->setContextProperty("model", QVariant::fromValue(dataList));
Declare the model as a property of the root. This way you can set model.Or add a function that takes model as argument and sets the model for the view. Then you can call this function from c++.

Resources