Highlighting the country on the map when you hover over - qt

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

Related

Save QPainter after called update

I want to draw straight lines on qml , but every time I draw a line, the previous disappears, I wonder if it has any thing to do with the update method, any ways to solve this problem.
main.qml
MouseArea{
id:fidpoint
anchors.fill: parent
onPressed: {
switch(addstate.currentText){
case 'Track':
map.setTstart(mouseX,mouseY);
draw_line.setColor("black");
draw_line.setStart(Qt.point(mouseX,mouseY));
draw_line.setEnd(Qt.point(mouseX,mouseY));
draw_line.update();
break;
}
}
onReleased: {
switch(addstate.currentText){
case 'Track':
map.addTrack(mouseX,mouseY);
draw_line.setColor("black");
draw_line.setEnd(Qt.point(mouseX,mouseY));
draw_line.update();
break;
}
}
onPositionChanged: {
switch(addstate.currentText){
case 'Track':
draw_line.setColor("black");
draw_line.setEnd(Qt.point(mouseX,mouseY));
draw_line.update();
break;
}
}
}
draw_line is the Object's id that I registered to qml from main.cpp
paint.cpp
void Paint::paint(QPainter *painter)
{
QPen pen(m_color, 2);
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing,true);
painter->drawLine(startNode,endNode);
}
paint is the class that inherits from QQuickPaintedItem
A possible option would be to create an image and save the changes there, but this would solve your problem in the short term. A better option is that draw_line draw only one line, then add it as a child of the item you want the canvas to be.
lineitem.h
#ifndef LINEITEM_H
#define LINEITEM_H
#include <QQuickPaintedItem>
class LineItem: public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QPoint startPos READ startPos WRITE setStartPos NOTIFY startPosChanged)
Q_PROPERTY(QPoint endPos READ endPos WRITE setEndPos NOTIFY endPosChanged)
Q_PROPERTY(QColor lineColor READ lineColor WRITE setLineColor NOTIFY lineColorChanged)
public:
using QQuickPaintedItem::QQuickPaintedItem;
void paint(QPainter *painter);
QPoint startPos() const;
void setStartPos(const QPoint &startPos);
QPoint endPos() const;
void setEndPos(const QPoint &endPos);
QColor lineColor() const;
void setLineColor(const QColor &lineColor);
signals:
void startPosChanged();
void endPosChanged();
void lineColorChanged();
private:
QPoint m_startPos;
QPoint m_endPos;
QColor m_lineColor;
};
#endif // LINEITEM_H
lineitem.cpp
#include "lineitem.h"
#include <QPainter>
void LineItem::paint(QPainter *painter)
{
painter->setRenderHint(QPainter::Antialiasing, true);
QPen pen(m_lineColor, 2);
painter->setPen(pen);
painter->drawLine(m_startPos, m_endPos);
}
QPoint LineItem::startPos() const
{
return m_startPos;
}
void LineItem::setStartPos(const QPoint &startPos)
{
if(m_startPos == startPos)
return;
m_startPos = startPos;
emit startPosChanged();
update();
}
QPoint LineItem::endPos() const
{
return m_endPos;
}
void LineItem::setEndPos(const QPoint &endPos)
{
if(m_endPos == endPos)
return;
m_endPos = endPos;
emit endPosChanged();
update();
}
QColor LineItem::lineColor() const
{
return m_lineColor;
}
void LineItem::setLineColor(const QColor &lineColor)
{
if(m_lineColor == lineColor)
return;
m_lineColor = lineColor;
emit lineColorChanged();
update();
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import com.eyllanesc.org 1.0
Window {
id: win
visible: true
width: 640
height: 480
title: qsTr("Hello World")
property LineItem currentItem: null
Rectangle{
id: canvas
anchors.fill: parent
MouseArea{
anchors.fill: parent
onPressed: {
currentItem = create_lineitem(canvas)
currentItem.lineColor = "green"
currentItem.anchors.fill = canvas
currentItem.startPos = Qt.point(mouseX,mouseY)
currentItem.endPos = Qt.point(mouseX,mouseY)
}
onReleased: currentItem.endPos = Qt.point(mouseX,mouseY)
onPositionChanged: currentItem.endPos = Qt.point(mouseX,mouseY)
}
}
function create_lineitem(parentItem, color) {
return Qt.createQmlObject('import com.eyllanesc.org 1.0; LineItem{}',
parentItem);
}
}
main.cpp
#include "lineitem.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<LineItem>("com.eyllanesc.org", 1, 0, "LineItem");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
The complete example can be found in the following link.

error C2280: 'QQmlPrivate::QQmlElement<T>::QQmlElement(void)': attempting to reference a deleted function

I tried to operate a part of a qt project in Qt\Examples\Qt-5.9\quick\views, I am new to qml and I am trying to open each time a different QDialog window depending on qml pathview component that has been clicked. First of all, I started with creating a class (interfacageQML) which will serve to interface the qml Mainform and the QDialog (qtinterface), the necessary files are included among which interfacageqml.h.
here is the main.cpp :
#include "interfacageqml.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<interfacageQML>("Interfacage", 1, 0,"Component:MouseArea");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
And here's interfacageqml.h :
#ifndef INTERFACAGEQML_H
#define INTERFACAGEQML_H
#include <QObject>
#include "qtinterface.h"
class interfacageQML : public QObject
{
Q_OBJECT
public:
interfacageQML(QObject *parent);
~interfacageQML();
Q_INVOKABLE void mouseClick();
signals:
void clicked();
};
#endif // INTERFACAGEQML_H
interfacageqml.cpp :
#include "interfacageqml.h"
#include <QDebug>
#include <QApplication>
interfacageQML::interfacageQML(QObject *parent)
: QObject(parent)
{
}
interfacageQML::~interfacageQML()
{
}
void interfacageQML::mouseClick()
{
qDebug() << "qmlinterface::mouseClick()";
emit clicked();
}
My project is organised this way :
the qmlinterface.qrc file contains these paths:
main.qml :
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MainForm{
anchors.fill: parent
}
}
MainForm.qml :
import QtQuick 2.6
import QtQuick.Controls 2.0 as QQC2
import Interfacage 1.0
Rectangle {
width: 800
height: 800
color: "white"
ListModel {
id: appModel
ListElement {
name: "Contacts"
icon: "pics/Resources/AddressBook_48.png"
}
ListElement {
name: "Music"
icon: "pics/Resources/AudioPlayer_48.png"
}
ListElement {
name: "Movies"
icon: "pics/Resources/VideoPlayer_48.png"
}
ListElement {
name: "Camera"
icon: "pics/Resources/Camera_48.png"
}
ListElement {
name: "Calendar"
icon: "pics/Resources/DateBook_48.png"
}
ListElement {
name: "Todo List"
icon: "pics/Resources/TodoList_48.png"
}
}
Component {
id: appDelegate
Item {
width: 100
height: 100
scale: PathView.iconScale
Image {
id: myIcon
y: 20
anchors.horizontalCenter: parent.horizontalCenter
source: icon
}
Text {
anchors {
top: myIcon.bottom
horizontalCenter: parent.horizontalCenter
}
text: name
}
MouseArea {
anchors.fill: parent
onClicked: {
view.currentIndex = index
Interfacage.mouseClick
}
}
}
}
Component {
id: appHighlight
Rectangle {
width: 100
height: 80
color: "lightsteelblue"
}
}
PathView {
id: view
anchors.fill: parent
highlight: appHighlight
preferredHighlightBegin: 0.5
preferredHighlightEnd: 0.5
focus: true
model: appModel
delegate: appDelegate
path: Path {
startX: 50
startY: 80
PathAttribute {
name: "iconScale"
value: 2.0
}
PathQuad {
x: 250
y: 200
controlX: 50
controlY: 200
}
PathAttribute {
name: "iconScale"
value: 2.0
}
PathQuad {
x: 600
y: 50
controlX: 400
controlY: 200
}
PathAttribute {
name: "iconScale"
value: 2.0
}
}
}
}
When I run this project, i got an error :
error:C2280
However, when I comment this line : qmlRegisterType<interfacageQML>("Interfacage", 1, 0, "Component:MouseArea"); the project runs and I can navigate between the pathview components in the MainForm.
When you use qmlRegisterType you are registering a new data type in QML, it is not an object, in that case the name "Component: MouseArea" is not suitable.
qmlRegisterType<interfacageQML>("Interfacage", 1, 0, "InterfacageQML");
Another error is that you must pass a parent by default, in this case 0 or nullptr since the items may not have parents.
class interfacageQML : public QObject
{
Q_OBJECT
public:
explicit interfacageQML(QObject *parent = nullptr);
[...]
As I said in the first lines, this is a new type, it is not an object so you must create it.
import QtQuick 2.6
import QtQuick.Controls 2.0 as QQC2
import Interfacage 1.0
Rectangle {
width: 800
height: 800
color: "white"
InterfacageQML{
id: myitem
}
[...]
And in the end if you want to use it you must call the function through the item.
MouseArea {
anchors.fill: parent
onClicked: {
view.currentIndex = index
myitem.mouseClick()
}
}
Since you want to connect your QDialog with the QML through that class, you can not do it since they will be different objects, one solution for this is to use a singleton, for this you must do the following:
interfacageqml.h
#ifndef INTERFACAGEQML_H
#define INTERFACAGEQML_H
#include <QObject>
#include <QQmlEngine>
class interfacageQML : public QObject
{
Q_OBJECT
static interfacageQML* instance;
explicit interfacageQML(QObject *parent = nullptr);
public:
static interfacageQML *getInstance();
~interfacageQML();
Q_INVOKABLE void mouseClick();
signals:
void clicked();
};
#endif // INTERFACAGEQML_H
interfacageqml.cpp
#include "interfacageqml.h"
#include <QDebug>
interfacageQML* interfacageQML::instance = 0;
interfacageQML *interfacageQML::getInstance()
{
if (instance == 0)
instance = new interfacageQML;
return instance;
}
interfacageQML::interfacageQML(QObject *parent) : QObject(parent)
{
}
interfacageQML::~interfacageQML()
{
}
void interfacageQML::mouseClick()
{
qDebug() << "qmlinterface::mouseClick()";
emit clicked();
}
main.cpp
#include "interfacageqml.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
static QObject *singletonTypeProvider(QQmlEngine *, QJSEngine *)
{
return interfacageQML::getInstance();
}
int main(int argc, char *argv[])
{
qmlRegisterSingletonType<interfacageQML>("Interfacage", 1, 0, "InterfacageQML", singletonTypeProvider);
// test
interfacageQML *obj = qobject_cast<interfacageQML*>(interfacageQML::getInstance());
QObject::connect(obj, &interfacageQML::clicked,[]{
qDebug()<<"clicked";
});
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
As it is a singleton it is not necessary to create an item, you can do it directly:
import Interfacage 1.0
[...]
MouseArea {
anchors.fill: parent
onClicked: {
view.currentIndex = index
InterfacageQML.mouseClick()
}
}
This last example can be found in the following link.

Drawing QQuickItem's texture in a "draw-chain" through 2 QQuickFramebufferObjects fails

First off, sorry for the long source code but I couldn't reduce it any further while still having the bug happen. Most of it is uninteresting boilerplate though, so you can mostly get away with just my human-readable summary below.
Abbreviations used in this post:
QQFBO = QQuickFramebufferObject
Observed result: Two rotating squares, that's all.
What's missing from the observed result: A third, additional, non-rotating square, which is actually a static snapshot from the second one.
What my code does, in short:
class ClonerItem is a QQFBO just fetches a texture from a QQuickItem and draws it to the screen.
The ClonerItemQml is a Qml wrapper of ClonerItem that adds a Timer to it. Depending on the ClonerItem.live property, the timer either calls ClonerItem.update() constantly or not at all. (basically the same the live property of Qt's ShaderEffectSource)
In main.qml I create a "producer-consumer-chain" of 3 items in a RowLayout.
The first item is a rotating rectangle with layer.enabled: true.
The second item is a ClonerItemQml (with live: true) that has the previous one as a texture source.
The third item is a ClonerItemQml (called "snapshotter", with live: false) that has the the previous one as a texture source.
"snapshotter" has its update method connected to 2 signals:
A firstRedraw signal provided by item "2", which is called at the end of the first redraw of item "2"
For testing, the clicked signal for "snapshotter", so you can see that the "snapshotter" does work correctly when you force it to update later during the program's execution.
Any hints what I'm doing wrong?
Note: When I remove the Timer and instead of it, uncomment the if(m_live) update(); in render, it works fine.
My code:
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickFramebufferObject>
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions>
#include <QOpenGLFramebufferObject>
#include <QQuickWindow>
// propertyhelper.h is from http://syncor.blogspot.bg/2014/11/qt-auto-property.html
#include "propertyhelper.h"
#include <QSGTextureProvider>
class ClonerItem : public QQuickFramebufferObject {
Q_OBJECT
AUTO_PROPERTY(QQuickItem*, sourceItem)
AUTO_PROPERTY(bool, live)
public:
Renderer* createRenderer() const;
signals:
void firstRedraw();
};
class ClonerItemRenderer : public QObject, public QQuickFramebufferObject::Renderer, protected QOpenGLFunctions {
Q_OBJECT
public:
ClonerItemRenderer() {
initializeOpenGLFunctions();
m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shader.vert.glsl");
m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shader.frag.glsl");
m_program.link();
createGeometry();
}
void render() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
m_program.bind();
glActiveTexture(GL_TEXTURE0);
m_tex->bind();
m_program.setUniformValue("uTex", 0);
paintGeometry();
glFlush();
glFinish();
m_window->resetOpenGLState();
if(!m_haveRedrawnAtLeastOnce)
emit firstRedraw();
m_haveRedrawnAtLeastOnce = true;
//if(m_live)
// update();
}
void paintGeometry() {
m_program.enableAttributeArray("aPos");
m_program.setAttributeArray("aPos", m_vertices.constData());
glDrawArrays(GL_TRIANGLES, 0, m_vertices.size());
}
void createGeometry() {
m_vertices << QVector2D(-1, -1) << QVector2D(-1, +1) << QVector2D(+1, +1);
m_vertices << QVector2D(-1, -1) << QVector2D(+1, -1) << QVector2D(+1, +1);
}
QOpenGLFramebufferObject* createFramebufferObject(const QSize &size) {
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
return new QOpenGLFramebufferObject(size, format);
}
private:
QVector<QVector2D> m_vertices;
QOpenGLShaderProgram m_program;
QQuickWindow* m_window;
QSGTexture* m_tex;
bool m_haveRedrawnAtLeastOnce = false;
bool m_live;
protected:
void synchronize(QQuickFramebufferObject* qqfbo) {
ClonerItem* parentItem = (ClonerItem*)qqfbo;
m_window = parentItem->window();
m_live = parentItem->live();
// fetch texture pointer
QQuickItem* sourceItem = parentItem->sourceItem();
QSGTextureProvider* sourceTexProvider = sourceItem->textureProvider();
m_tex = sourceTexProvider->texture();
}
signals:
void firstRedraw();
};
QQuickFramebufferObject::Renderer* ClonerItem::createRenderer() const {
auto renderer = new ClonerItemRenderer();
connect(renderer, &ClonerItemRenderer::firstRedraw, this, &ClonerItem::firstRedraw);
return renderer;
}
int main(int argc, char **argv) {
QGuiApplication app(argc, argv);
qmlRegisterType<ClonerItem>("ClonerItem", 1, 0, "ClonerItem");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
#include "main.moc"
ClonerItem.qml:
import QtQuick 2.0
import ClonerItem 1.0
ClonerItem {
id: root
Timer {
id: renderTimer
running: root.live
interval: 1000/60
repeat: true
onTriggered: {
root.update();
}
}
}
main.qml:
import QtQuick 2.0
import ClonerItem 1.0
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
Window {
visible: true
width: 600
height: 200
RowLayout {
id: rowLayout
anchors.fill: parent
Item {
id: original
layer.enabled: true
Layout.fillWidth: true
Layout.fillHeight: true
RotatingSquare { }
}
ClonerItemQml {
id: liveClone
layer.enabled: true
sourceItem: original
live: true
Layout.fillWidth: true
Layout.fillHeight: true
onFirstRedraw: {
snapshotter.update();
}
}
ClonerItemQml {
id: snapshotter
sourceItem: liveClone
live: false
Layout.fillWidth: true
Layout.fillHeight: true
MouseArea {
anchors.fill: parent
onClicked: {
snapshotter.update();
}
}
}
}
}
RotatingSquare.qml:
import QtQuick 2.0
Rectangle {
color: "red"
border.color: "black"
width: parent.width / 2
height: parent.height / 2
anchors.centerIn: parent
NumberAnimation on rotation {
running: true
loops: Animation.Infinite
duration: 1000
from: 0
to: 360
}
}
shader.frag.glsl:
varying highp vec2 vTexCoord;
uniform sampler2D uTex;
void main() {
gl_FragColor = texture2D(uTex, vTexCoord);
}
shader.vert.glsl
attribute highp vec2 aPos;
varying highp vec2 vTexCoord;
void main() {
gl_Position = vec4(aPos, 0.0, 1.0);
// aPos's components are in [-1, 1], transform them to the range [0, 1] for use as texcoords.
vTexCoord = aPos * .5 + vec2(.5);
}
If you're wondering why I'm doing such an elaborate/weird setup, I can elucidate: in my real app the second item in the "chain" is not a simple "cloner" but actually uses its source texture(s) for some more-complex rendering (which is also memory-hungry). And the third item in the "chain" is necessary to snapshot the result of the second one, so the second one can be destroyed when not animated, freeing up memory.
The reason I implement my own ClonerItem component, which does basically the same as Qt's ShaderEffectSource, is this Qt bug.

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;
}
}

How to insert ListElement in a QML ListModel using C++?

Well, i'm learning to work with QML and i have one doubt. In my example, i have a ListModel with ListElements at QML and i have a QML main file with a rectangle, PathView etc.
I have a QWidget also, than is my main window. In this QWidget i include the QML UI like a component. Ok!
How can I handle QML ListElements by using C++?
Note: when I say "to handle", I want to say include an element for example.
Below are some parts of my code...
QML containing my ListElement, called "Menu1":
import QtQuick 1.0
ListModel {
id: listMovieModel
ListElement { name: "Image 1"; iconSource: "pics/image_1.jpg" }
ListElement { name: "Image 2"; iconSource: "pics/image_2.jpg" }
ListElement { name: "Image 3"; iconSource: "pics/image_3.jpg" }
ListElement { name: "Image 4"; iconSource: "pics/image_4.jpg" }
ListElement { name: "Image 5"; iconSource: "pics/image_5.jpg" }
ListElement { name: "Image 6"; iconSource: "pics/image_6.jpg" }
}
My main QML:
Rectangle {
width: 500
height: 600
color: "transparent"
PathView {
id: view
focus: true
width: parent.width
height: parent.height + y
y: -150
model: Menu1 {} //First QML showed
delegate: Image {
source: iconSource
width: 64
height: 90
scale: PathView.isCurrentItem ? 3.5 * y / parent.height : 2.0 * y / parent.height
z: y
smooth: true
}
path: MyGeometricFigure { //This a another file, but is confidential
width: view.width
height: view.height
}
preferredHighlightBegin: 0
preferredHighlightEnd: 0
highlightRangeMode: PathView.StrictlyEnforceRange
Keys.onLeftPressed: decrementCurrentIndex()
Keys.onRightPressed: incrementCurrentIndex()
}
}
And as I use QML like a component for my QWidget:
MainForm::MainForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainForm)
{
ui->setupUi(this);
this->resize(1024, 576);
QDeclarativeView *myQMLTest = new QDeclarativeView(QUrl::fromLocalFile("myMainQML.qml"));
myQMLTest->setStyleSheet(QString("background: transparent; width: 600px"));
this->ui->frameListVideoGallery->layout()->addWidget(myQMLTest);
myQMLTest->setFocus();
myQMLTest->installEventFilter(this);
}
I saw some articles about this, but I am not able to change my LisModel using C++. I saw here http://doc.qt.nokia.com/4.7/qdeclarativemodels.html#c-data-models and here in examples using PathView http://doc.qt.nokia.com/4.7/qdeclarativeexamples.html
Can someone help me?
Thanks!
Alright. I think you can do something like this:
In your main QML-file add simple function.
Rectangle {
// ...
function append(newElement) {
view.model.append(newElement)
}
PathView {
id: view
// ...
model: Menu1 {} //First QML showed
// ...
}
}
This method will just call a corresponding method from ListModel. More supported methods you can find there.
Then all you need is to call this method from C++ code. You can do this in such manner:
MainForm::MainForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainForm)
{
ui->setupUi(this);
this->resize(1024, 576);
QDeclarativeView *myQMLTest = new QDeclarativeView(QUrl::fromLocalFile ("myMainQML.qml"));
myQMLTest->setStyleSheet(QString("background: transparent; width: 600px"));
QVariantMap newElement; // QVariantMap will implicitly translates into JS-object
newElement.insert("name", "Image 13" );
newElement.insert("iconSource", "pics/image_13.jpg");
QMetaObject::invokeMethod(
myQMLTest->rootObject(), // for this object we will call method
"append", // actually, name of the method to call
Q_ARG(QVariant, QVariant::fromValue(newElement)) // method parameter
);
this->ui->frameListVideoGallery->layout()->addWidget(myQMLTest);
myQMLTest->setFocus();
myQMLTest->installEventFilter(this);
}
You should check this for more information.
Also you can choose another approach: to pass some data via qml properties (using QDeclarativeEngine and QDeclarativeContext) and then handle this data and your list-model right in JavaScript code.
I want to make the answer from #GooRoo more complete/usefull to Qt beginners. If you use the Qt Creator, you will start with a template using QmlApplicationViewer. To apply the answer from GooRoo you have to do something like this (thanks to http://www.lothlorien.com/kf6gpe/?p=309):
CPP:
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer->setMainQmlFile(QLatin1String("qml/mydemo/main.qml"));
viewer->showExpanded();
QMetaObject::invokeMethod(
(QObject *)viewer->rootObject()->findChild<QObject *>("myModel"), // QML item
"test" // name of the method to call
// without parameters
);
return app->exec();
}
QML:
PageStackWindow {
id: mainApp
...
ListModel {
id: myModel
objectName: myModel //THIS IS NEEDED BY findChild()
...
function test() {
console.log("TEST OK");
}
}
Page {
...
}
}
Here is one more complete example of filling data in QML TableView from C++. Please pardon the bad variable names. I was making it for testing purpose only.
I have used QVariantList and filled it with QVariantMap objects.
Here, multiple data has been filled with a for loop in two columns of the QML's table, namely what and session_name by putting them in a QVariantMap and then inserting that QVariantMap in QVariantList.
HH.h
#ifndef HH_H
#define HH_H
#include <QVariant>
class AA : public QObject
{
Q_OBJECT
QVariantList m_gname;
public:
Q_PROPERTY(QVariantList gname READ gname WRITE setGname NOTIFY gnameChanged)
AA()
{
QVariantList q;
for(int i = 0; i < 10; i++)
{
QVariantMap p;
p.insert("what", "qq");
p.insert("session_name", "aa");
q.push_back(p);
}
setGname(q);
}
QVariantList gname() const
{
return m_gname;
}
public slots:
void setGname(QVariantList gname)
{
if (m_gname == gname)
return;
m_gname = gname;
emit gnameChanged(m_gname);
}
signals:
void gnameChanged(QVariantList gname);
};
#endif // HH_H
Here I have use qmlRegisterType to register the class AA which I created above.
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "HH.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<AA>("ZZZ", 1, 0, "AA");
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
In QML I have created a TableView by QtQuick.Controls 1.4. It has two columns namely what and session_name. I have created an object of the class AA here and on its Component.onCompleted called <model_id> dot append( <QVariantList_name> )
like this:
mymodel.append( gname )
main.qml
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 1.4 as OldControls
import ZZZ 1.0
Window
{
visible: true
width: 1000
height: 1000
title: qsTr("Hello World")
AA
{
Component.onCompleted:
{
mymodel.append(gname)
}
}
Rectangle
{
id: head
anchors.centerIn: parent; height: 500; width: 500; border.color: "red"
OldControls.TableView
{
anchors.fill: parent
verticalScrollBarPolicy: Qt.ScrollBarAlwaysOn
horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOn
OldControls.TableViewColumn
{
role: "what"
title: "What"
}
OldControls.TableViewColumn
{
role: "session_name"
title: "Session Name"
}
model: ListModel
{
id: mymodel
}
}
}
}

Resources