QML Clip Custom Item - qt

I made my own QQuickItem, which I draw using the updatePaintNode method (using the scenegraph). In a qml File I add this item as a child to a Rectangle:
Rectangle {
anchors.centerIn: parent
width: parent.width/2.
height: parent.height/2.
border.color: "#000000"
clip: true
MyItem {
id: myitem
anchors.centerIn: parent
width: parent.width
height: parent.height
MouseArea {
anchors.fill: parent
onWheel: {
if (wheel.modifiers & Qt.ControlModifier) {
parent.scale = parent.scale+ 0.2 * wheel.angleDelta.y / 120;
if (parent.scale < 1)
parent.scale = 1;
}
}
}
}
}
As you can see, I added a zoom function and set
clip: True
for the rectangle. However, when I zoom, my item is not clipped at the Rectangle but extends beyond it. What would be the correct way to do clipping?
Edit: Here is the content of the cpp file for MyItem
MyItem::MyItem(QQuickItem *parent): QQuickItem(parent)
{
setFlag(QQuickItem::ItemHasContents,true);
// code to initialize xdata, ydata, xmax, ymax
}
QSGNode *MyItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) {
QSGGeometryNode *mypath = static_cast<QSGGeometryNode*>(oldNode);
if(!mypath) {
mypath = new QSGGeometryNode;
mypath->setFlag(QSGNode::OwnsMaterial,true);
mypath->setFlag(QSGNode::OwnsGeometry,true);
}
QSGGeometry *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(),_xdata.size());
QSGGeometry::Point2D *points = geometry->vertexDataAsPoint2D();
for(int i=0;i<_xdata.size();i++) {
points[i].x = _xdata[i]/_xmax*width();
points[i].y = _ydata[i]/_ymax*height();
}
geometry->setLineWidth(2);
geometry->setDrawingMode(GL_LINE_STRIP);
mypath->setGeometry(geometry);
QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
material->setColor(Qt::blue);
mypath->setMaterial(material);
return mypath;
}
and here is the header file:
#include <QQuickItem>
#include <vector>
class MyItem : public QQuickItem
{
Q_OBJECT
public:
MyItem(QQuickItem *parent = 0);
private:
std::vector<double> _xdata;
std::vector<double> _ydata;
double _xmax;
double _ymax;
signals:
public slots:
protected:
QSGNode * updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override;
};

Related

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:")
}
}
}
}
}
}

Animating MapQuickItem in QML on position updates

I have an QAbstractListModel object that maintains a list of items to show on a map. The position of these items changes every few seconds and it is easy to calculate a pretty accurate position 60 seconds in the future. What I am trying to do is to set the item's position when it gets a new position (that part works well) and then to immediately move the item toward the calculated future position.
The code without animation looks like this and it works fine:
Component {
id: drawTarget
MapQuickItem {
id: marker
coordinate: data.coords
sourceItem: Item {
id: item
...
The data object has a property which returns the estimated position of the item 60 seconds in the future, so I tried this:
Component {
id: drawTarget
MapQuickItem {
id: marker
coordinate: data.coords
CoordinateAnimation {
id:anim
property: "coordinate"
}
onCoordinateChanged: {
anim.stop()
anim.from = data.coords
anim.to = data.coordsIn60sec
anim.duration = 60000
anim.start()
}
sourceItem: Item {
id: item
...
But although the object's position is updated properly at each position update, the animation toward the future estimated position doesn't work at all.
How would one go about doing something like this?
In its code, it makes a binding coordinate: data.coords that states that "coordinate" takes the value of "coords" but at the same time says that "coordinate" depends on the animation, isn't it contradictory? Well, it is contradictory.
The idea is not to do the binding coordinate: data.coords but to update the property only through the animation.
The following code is a workable example:
main.qml
import QtQuick 2.14
import QtQuick.Window 2.14
import QtLocation 5.6
import QtPositioning 5.6
Window {
visible: true
width: 640
height: 480
Plugin {
id: mapPlugin
name: "osm"
}
Map {
anchors.fill: parent
plugin: mapPlugin
center: QtPositioning.coordinate(59.91, 10.75) // Oslo
zoomLevel: 10
MapItemView{
model: datamodel
delegate: MapQuickItem{
id: item
// begin configuration
property var position: model.position
property var nextposition: model.nextposition
onPositionChanged: restart();
onNextpositionChanged: restart();
function restart(){
anim.stop()
anim.from = position
anim.to = nextposition
anim.start()
}
CoordinateAnimation {
id: anim
target: item
duration: 60 * 1000
property: "coordinate"
}
// end of configuration
anchorPoint.x: rect.width/2
anchorPoint.y: rect.height/2
sourceItem: Rectangle{
id: rect
color: "green"
width: 10
height: 10
}
}
}
}
}
datamodel.h
#ifndef DATAMODEL_H
#define DATAMODEL_H
#include <QAbstractListModel>
#include <QGeoCoordinate>
#include <QTimer>
#include <random>
#include <QDebug>
struct Data{
QGeoCoordinate position;
QGeoCoordinate nextposition;
};
static QGeoCoordinate osloposition(59.91, 10.75); // Oslo;
class DataModel : public QAbstractListModel
{
Q_OBJECT
QList<Data> m_datas;
public:
enum PositionRoles {
PositionRole = Qt::UserRole + 1,
NextPositionRole
};
explicit DataModel(QObject *parent = nullptr)
: QAbstractListModel(parent)
{
init();
}
int rowCount(const QModelIndex &parent = QModelIndex()) const override{
return parent.isValid() ? 0: m_datas.count();
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override{
if (!index.isValid() || index.row() < 0 || index.row() >= m_datas.count())
return QVariant();
const Data &data = m_datas[index.row()];
if (role == PositionRole)
return QVariant::fromValue(data.position);
else if (role == NextPositionRole)
return QVariant::fromValue(data.nextposition);
return QVariant();
}
QHash<int, QByteArray> roleNames() const override{
QHash<int, QByteArray> roles;
roles[PositionRole] = "position";
roles[NextPositionRole] = "nextposition";
return roles;
}
private:
void init(){
for(int i=0; i< 10; ++i){
Data data;
data.position = osloposition;;
data.nextposition = data.position;
m_datas << data;
}
QTimer *timer = new QTimer(this);
QObject::connect(timer, &QTimer::timeout, this, &DataModel::updateData);
timer->start(60 * 1000);
updateData();
}
void updateData(){
qDebug() << __PRETTY_FUNCTION__;
static std::default_random_engine e;
static std::uniform_real_distribution<> dis(-.1, .1);
for(int i=0; i < m_datas.count(); ++i){
Data & data = m_datas[i];
QModelIndex ix = index(i);
data.position = data.nextposition;
data.nextposition = QGeoCoordinate(osloposition.latitude() + dis(e),
osloposition.longitude() + dis(e));
Q_EMIT dataChanged(ix, ix, {PositionRole, NextPositionRole});
}
}
};
#endif // DATAMODEL_H
In the following link is the complete example

Reallocating QSGGeometryNode Vertex Data

I am using Qt 5.8. I am attempting to draw circular points using QSGGeometry::DrawTriangles (using QSGGeometry::DrawPoints draws points as rectangles/squares--not desired). In order to do this I am drawing 8 triangles that gives the illusion of a circle. Each data point will have 8 triangles associated with it. The number of data points can vary at any given time. After a (user) specified amount of time as a data point is added, one data point is removed. There seems to be an error in the allocation of data when it's drawn. I used setVertexDataPattern(QSGGeometry::StreamPattern); in the construction of the QSGGeometryNode; in hopes of getting the desired output.
On each draw call, I call m_geometry.allocate(m_pts.size() * MAX_VERTICES), where MAX_VERTICES = 24 in case the number of points since the last draw call has changed. I have attempted to use GL_POLYGON (since it would require fewer vertices), but the same problem happens. There seems to be an attempt to draw a shape from one slice of the the first data point to another slice of the last data point. Is there something wrong with reallocating for every draw call? What is the proper way to handle drawing data with varying sizes?
Update I think it may deal with a size issue. I have sample code that only draws 1 triangle (instead of 8) and once you get to about 25000 (times 3 for each triangle) the odd line appears and seems to stop drawing additional triangles. In the following sample code (when using a smaller number of points) the last triangle is white.
main.qml
import QtQuick 2.8
import QtQuick.Window 2.2
import TestModule 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle {
color: "black"
width: parent.width
height: parent.height * .90
anchors.top: parent.top
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
TestItem {
id: testItem
anchors.fill: parent
ptCount: 25000
color: "green"
}
}
Rectangle {
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
height: parent.height * .10
width: parent.width
border.color: "pink"
color: "lightgray"
TextInput {
anchors.fill: parent
anchors.centerIn: parent
id: textInput
text: "enter max number of points here"
horizontalAlignment: TextInput.AlignHCenter
verticalAlignment: TextInput.AlignVCenter
color: "steelblue"
onEditingFinished: testItem.ptCount = parseInt(textInput.text)
validator: IntValidator{bottom: 1}
}
}
}
TestItem.h
#include <QQuickItem>
class QSGGeometryNode;
class TestItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(qint32 ptCount READ ptCount WRITE setPtCount NOTIFY ptCountChanged)
public:
explicit TestItem(QQuickItem *parent = 0);
QColor color();
void setColor(const QColor &color);
void setPtCount(const qint32& newVal);
qint32 ptCount();
signals:
void colorChanged();
void ptCountChanged();
protected:
QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
QColor m_color;
qint32 m_ptCount;
};
TestItem.cpp
#include "TestItem.h"
#include <QSGNode>
#include <QSGVertexColorMaterial>
TestItem::TestItem(QQuickItem *parent) : QQuickItem(parent), m_color(Qt::green), m_ptCount(25000)
{
setFlag(ItemHasContents, true);
}
QColor TestItem::color()
{
return m_color;
}
void TestItem::setColor(const QColor &color)
{
m_color = color;
update();
emit colorChanged();
}
void TestItem::setPtCount(const qint32 &newVal)
{
if (newVal < 0)
m_ptCount = 25000;
else
m_ptCount = newVal;
update();
emit ptCountChanged();
}
qint32 TestItem::ptCount()
{
return m_ptCount;
}
QSGNode *TestItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *)
{
QSGGeometryNode *node = nullptr;
QSGGeometry *geometry = nullptr;
if (!oldNode)
{
node = new QSGGeometryNode;
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), m_ptCount * 3);
geometry->setDrawingMode(GL_TRIANGLES);
node->setGeometry(geometry);
node->setFlag(QSGNode::OwnsGeometry);
QSGVertexColorMaterial *material = new QSGVertexColorMaterial;
node->setMaterial(material);
node->setFlag(QSGNode::OwnsMaterial);
}
else
{
node = static_cast<QSGGeometryNode *>(oldNode);
geometry = node->geometry();
geometry->allocate(m_ptCount * 3);
}
QSGGeometry::ColoredPoint2D *vertices = geometry->vertexDataAsColoredPoint2D();
qreal triWidth = 250/boundingRect().width() + 10;
for (int i = 0; i < m_ptCount; ++i)
{
QColor color;
if (i == m_ptCount - 1)
color = Qt::white;
else
color = m_color;
qreal x0 = (boundingRect().width() * .90/m_ptCount) * i ;
qreal y0 = 60 * sinf(x0* 3.14/180); // 60 just varies the height of the wave
qreal x1 = x0 + 0.05 * boundingRect().width(); // 0.05 so that we have 5% space on each side
qreal y1 = y0 + boundingRect().height()/2;
vertices[i * 3].set(x1, y1, color.red(), color.green(), color.blue(), color.alpha());
vertices[i * 3 + 1].set(x1 + triWidth, y1, color.red(), color.green(), color.blue(), color.alpha());
vertices[i * 3 + 2].set(x1 + triWidth, y1 + triWidth, color.red(), color.green(), color.blue(), color.alpha());
}
node->markDirty(QSGNode::DirtyGeometry);
return node;
}
void TestItem::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
update();
QQuickItem::geometryChanged(newGeometry, oldGeometry);
}
Any help with determining if this is a Qt bug or my error somewhere?
It turns out I needed to change the geometry's constructor to use UnsignedIntType; it defaults to UnsignedShortType.
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), m_ptCount * 3, 0, QSGGeometry::UnsignedIntType);

How to take a screen shot of the window and display it on an image using QML?

I want to take a screen shot using the code
QImage img = currentView_->grabWindow();
and i want to display this screen shot on my GUI using QML.
How to do this ?
If you want to transfer an image from C++ to QML the best way I guess is QQuickImageProvider. The small example bellow is maybe a bit different from what you want but anyway:
QQuickImageProvider class:
#ifndef IMAGEPROVIDER_H
#define IMAGEPROVIDER_H
#include <QQuickImageProvider>
#include <QQuickView>
#include <QImage>
class ImageProvider : public QObject, public QQuickImageProvider
{
Q_OBJECT
public:
ImageProvider(QObject *parent = 0, Flags flags = Flags());
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
protected:
QMap<int, QImage> m_images;
public slots:
void makeScreenshot();
};
#endif // IMAGEPROVIDER_H
#include "imageprovider.h"
ImageProvider::ImageProvider(QObject *parent, Flags flags) :
QQuickImageProvider(QQmlImageProviderBase::Image, flags),
QObject(parent)
{
}
QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
bool ok = false;
QImage img = m_images[id.toInt(&ok)];
return img;
}
void ImageProvider::makeScreenshot()
{
QQuickWindow *view = static_cast<QQuickWindow *>(sender());
static int pos = 0;
QImage img = view->grabWindow();
m_images.insert(pos,img);
QVariant returnedValue;
QMetaObject::invokeMethod(view, "setImage",
Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, pos++));
}
Registering it in main.cpp:
QQmlApplicationEngine engine;
ImageProvider *imageProvider = new ImageProvider();
engine.addImageProvider(QLatin1String("screenshots"), imageProvider);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQuickWindow *view = dynamic_cast<QQuickWindow *>(engine.rootObjects().at(0));
QObject::connect(view, SIGNAL(getScreenshot()),
imageProvider, SLOT(makeScreenshot()));
Usage:
import QtQuick 2.7
import QtQuick.Window 2.0
Window {
id:container
width: 600
height: 600
visible: true
signal getScreenshot()
Repeater {
model: 20
delegate: Rectangle {
x: Math.random() * 500
y: Math.random() * 500
width: Math.random() * 50 + 50
height: width
radius: width / 2
color: Qt.rgba(Math.random(),Math.random(),Math.random(),1)
}
}
Text {
id: txt
anchors.centerIn: parent
text: "Click me"
}
Rectangle {
id: rect
anchors.centerIn: parent
width: 0
height: 0
border.width: 5
border.color: "black"
Image {
id: img
anchors.fill: parent
anchors.margins: 5
source: ""
fillMode: Image.PreserveAspectFit
}
ParallelAnimation {
id: anim
PropertyAnimation {
property: "width"
target: rect
from: 0
to: 200
duration: 500
}
PropertyAnimation {
property: "height"
target: rect
from: 0
to: 200
duration: 500
}
}
}
MouseArea {
anchors.fill: parent
onClicked: {
txt.visible = false;
container.getScreenshot();
}
}
function setImage(id)
{
img.source = "image://screenshots/" + id;
anim.running = true;
}
}

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