Display several roles in one column of TableView - qt
I have a SQLite 3 database with 4 columns and QML code with TableView that displays it:
TableView {
id: table
...
TableViewColumn {
role: "name"
width: 275
}
TableViewColumn {
role: "surname"
width: 300
}
TableViewColumn {
role: "phone"
width: 575
}
TableViewColumn {
role: "ip_address"
width: 525
}
model: abonents
}
It works fine, but I need to display the first two roles, name and surname, as a unique column in TableView.
Here is the code for my model and the main.
abonentstable.h:
#ifndef ABONENTSTABLE
#define ABONENTSTABLE
#include <QObject>
#include <QSqlQueryModel>
class AbonentsSqlModel : public QSqlQueryModel
{
Q_OBJECT
public:
explicit AbonentsSqlModel(QObject *parent = 0);
void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
void setQuery(const QSqlQuery &query);
QVariant data(const QModelIndex &index, int role) const;
QHash<int, QByteArray> roleNames() const { return m_roleNames; }
signals:
public slots:
private:
void generateRoleNames();
QHash<int, QByteArray> m_roleNames;
};
#endif // ABONENTSTABLE
dbconnection.cpp:
#include "abonentstable.h"
#include <QSqlRecord>
#include <QSqlField>
AbonentsSqlModel::AbonentsSqlModel(QObject *parent) :
QSqlQueryModel(parent)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("data_base.sqlite");
db.open();
}
void AbonentsSqlModel::setQuery(const QString &query, const QSqlDatabase &db)
{
QSqlQueryModel::setQuery(query, db);
generateRoleNames();
}
void AbonentsSqlModel::setQuery(const QSqlQuery & query)
{
QSqlQueryModel::setQuery(query);
generateRoleNames();
}
void AbonentsSqlModel::generateRoleNames()
{
m_roleNames.clear();
for( int i = 0; i < record().count(); i ++) {
m_roleNames.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
}
}
QVariant AbonentsSqlModel::data(const QModelIndex &index, int role) const
{
QVariant value;
if(role < Qt::UserRole) {
value = QSqlQueryModel::data(index, role);
}
else {
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
return value;
}
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QVariant>
#include <QSql>
#include <QSqlQueryModel>
#include <QObject>
#include "abonentstable.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
AbonentsSqlModel *abonentsSqlModel = new AbonentsSqlModel(0);
abonentsSqlModel->setQuery("SELECT * FROM abonents");
QQmlContext *context = engine.rootContext();
context->setContextProperty("abonents", abonentsSqlModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
I think you could join both values using the delegate component.
In your case:
TableView {
id: table
...
TableViewColumn {
width: 575
delegate: Text { text: model.name + " " + model.surname }
}
TableViewColumn {
role: "phone"
width: 575
}
TableViewColumn {
role: "ip_address"
width: 525
}
model: abonents
}
Here you have another example, just for testing if you want to work with it. The example is based on this Qt example.
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.2
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
ApplicationWindow {
id: window
visible: true
title: "Table View Example"
TableView {
TableViewColumn {
role: "title"
title: "Title"
width: 100
}
TableViewColumn {
role: "author"
title: "Author"
width: 100
}
TableViewColumn{
width: 300
delegate: Text { text: model.title + " " + model.author }
}
TableViewColumn{
width: 300
delegate: Text {
text: model.title + " " + model.author
font.family: "Courier New"
font.pixelSize: 18
color: "red"
}
}
model: libraryModel
ListModel {
id: libraryModel
ListElement {
title: "A Masterpiece"
author: "Gabriel"
}
ListElement {
title: "Brilliance"
author: "Jens"
}
ListElement {
title: "Outstanding"
author: "Frederik"
}
}
}
}
Related
Text gets missaligned when using Qt TextTable with TextArea using QtQuick.Controls 1.12
I have a problem where I am using a TextArea in Qml. A C++ Model holds a reference to that TextArea. When I insert a QTextTable in the C++ model it all fine until the user enters some text. After the user manually edits a few cells and writes some text in it, it gets all messed up. Does anyone know anything on how to solve it? I also have other functions with are working perfectly. So I would guess there is nothing wrong with the connection between the c++ model and the textarea. Here is the Documenthandler.h #include <QQuickTextDocument> #include <QtGui/QTextCharFormat> #include <QtCore/QTextCodec> #include <qqmlfile.h> QT_BEGIN_NAMESPACE class QTextDocument; QT_END_NAMESPACE class DocumentHandler : public QObject { Q_OBJECT Q_ENUMS(HAlignment) Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) public: DocumentHandler(); Q_INVOKABLE void createTable(int columns ,int rows); QQuickItem *target() { return m_target; } void setTarget(QQuickItem *target); QString text() const; public Q_SLOTS: void setText(const QString &arg); Q_SIGNALS: void targetChanged(); void textChanged(); void error(QString message); private: QTextCursor textCursor() const; QQuickItem *m_target; QTextDocument *m_doc; QString m_text; }; Here is the documenthandler.cpp in the "createTable" function I create the table #include "documenthandler.h" #include <QtGui/QTextDocument> #include <QtGui/QTextList> #include <QtGui/QTextTable> #include <QtGui/QTextCursor> #include <QtGui/QFontDatabase> #include <QtCore/QFileInfo> DocumentHandler::DocumentHandler() : m_target(0) , m_doc(0) { } void DocumentHandler::setTarget(QQuickItem *target) { m_doc = 0; m_target = target; if (!m_target) return; QVariant doc = m_target->property("textDocument"); if (doc.canConvert<QQuickTextDocument*>()) { QQuickTextDocument *qqdoc = doc.value<QQuickTextDocument*>(); if (qqdoc) m_doc = qqdoc->textDocument(); } emit targetChanged(); } void DocumentHandler::setText(const QString &arg) { if (m_text != arg) { m_text = arg; emit textChanged(); } } QString DocumentHandler::text() const { return m_text; } QTextCursor DocumentHandler::textCursor() const { if (!m_doc) return QTextCursor(); QTextCursor cursor = QTextCursor(m_doc); return cursor; } void DocumentHandler::createTable(int columns , int rows) { QTextCursor cursor = textCursor(); if (cursor.isNull()) return; cursor.insertTable(rows,columns); } Here is the main qml import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 1.6 import DocumentHandler 1.0 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Button { id:btn text:"test" onClicked: document.createTable(5,5); } TextArea { Accessible.name: "document" id: tooltip_area selectByMouse: true anchors.left:parent.left anchors.right:parent.right anchors.top: btn.bottom anchors.bottom: parent.bottom baseUrl: "qrc:/" text: document.text textFormat: Qt.RichText wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere Component.onCompleted: forceActiveFocus() } DocumentHandler{ id: document target: tooltip_area } } here is the main function, there is nothing special except I register the QML Type #include <QGuiApplication> #include <QQmlApplicationEngine> #include "documenthandler.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; const QUrl url(QStringLiteral("qrc:/main.qml")); qmlRegisterType<DocumentHandler>("DocumentHandler",1,0,"DocumentHandler"); 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(); } This is how it looks after filling up some cells
So I solved it by just selecting everything and deselecting when the text changes. Theoretically I only need to do it when I am in a Table Block. onTextChanged: { if(recoursionlock) { recoursionlock =false; var curpos = tooltip_area.cursorPosition; var select_start = tooltip_area.selectionStart; var select_end = tooltip_area.selectionEnd; selectAll(); deselect(); if(curpos !== -1 && curpos >= tooltip_area.text.lenght) tooltip_area.cursorPosition = curpos; else tooltip_area.select(select_start,select_end) recoursionlock = true; } }
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:") } } } } } }
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.
How to assign SQL query output model from Qt to QML's TableView?
From C++ (Qt): int main(int argc, char *argv[]) { QApplication app(argc, argv); /* * I have omitted the code about connection with the database and all for ease of * viewing the code. */ // My own function for filling in the data in the `QSqlQueryModel` QSqlQueryModel* model = new QSqlQueryModel; QString binid = "B1"; QString query = "SELECT t1.BinId, t1.PartitionId, t2.UnitId, t2.ItemCount FROM Bin_Partitions AS t1 " "INNER JOIN Partition_Units AS t2 ON t1.PartitionId = t2.PartitionId " "WHERE t1.BinId ='" + binid + "'"; model->setQuery(query); /* * I can see that the query runs successfully because the following * QTableView DOES get populated properly. */ // Use QTableView to visualize QTableView *view = new QTableView; view->setModel(model); view->show(); QQmlApplicationEngine engine; // Passing the same model to QML for displaying in the TableView. engine.rootContext()->setContextProperty ("SQQL", model); engine.load(QUrl(QStringLiteral("/home/.../main.qml"))); QObject *topLevel = engine.rootObjects ().value (0); QQuickWindow *window = qobject_cast <QQuickWindow *> (topLevel); return app.exec(); } From QML: import QtQuick 2.2 import QtQuick.Window 2.1 import QtQuick.Controls 1.2 Window { visible: true width: 360 height: 360 color: "blue" TableView { TableViewColumn{ role: "col1" ; title: "BinId" ; visible: true} TableViewColumn{ role: "col2" ; title: "PartitionId" } TableViewColumn{ role: "col3" ; title: "UnitId" } TableViewColumn{ role: "col4" ; title: "ItemCount" } model: SQQL } } The TableView of QML shows up EMPTY! I need help.
You have to subclass QSqlQueryModel and reimplement roleNames and data methods MySqlModel.h class MySqlModel: public QSqlQueryModel { Q_OBJECT public: MySqlModel(QObject *parent = 0) : QSqlQueryModel(parent) {} enum Roles { BinId = Qt::UserRole + 1, PartitionId, UnitId, ItemCount }; QHash<int, QByteArray> roleNames() const { QHash<int, QByteArray> roles; roles[BinId] = "binIdRole"; roles[PartitionId] = "partitionIdRole"; roles[UnitId] = "unitIdRole"; roles[ItemCount] = "itemCountRole"; return roles; } QVariant data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); QString fieldName; switch (role) { case BinId: fieldName = QStringLiteral("t1.BinId"); break; case PartitionId: fieldName = QStringLiteral("t1.PartitionId"); break; case UnitId: fieldName = QStringLiteral("t2.UnitId"); break; case ItemCount: fieldName = QStringLiteral("t2.ItemCount"); break; } if (!this->record().isGenerated(fieldName)) return QVariant(); else { QModelIndex item = indexInQuery(index); if ( !this->query().seek(item.row()) ) return QVariant(); return this->query().value(fieldName); } return QVariant(); } }; main.qml Window { visible: true width: 640 height: 480 TableView { anchors.fill: parent model: SQQL TableViewColumn{ role: "binIdRole" ; title: "BinId" ; visible: true} TableViewColumn{ role: "partitionIdRole" ; title: "PartitionId" } TableViewColumn{ role: "unitIdRole" ; title: "UnitId" } TableViewColumn{ role: "itemCountRole" ; title: "ItemCount" } } }
Remove rows from QAbstractListModel
I have a custom model which derives from QAbstractListModel which is exposed to QML. I need to support operations to add new items and remove existing items. While insertion operation works without any problems, removal operation causes the application to crash while calling endRemoveRows() function. void GPageModel::addNewPage() { if(m_pageList.count()<9) { beginInsertRows(QModelIndex(),rowCount(),rowCount()); GPage * page = new GPage(); QQmlEngine::setObjectOwnership(page,QQmlEngine::CppOwnership); page->setParent(this); page->setNumber(m_pageList.count()); page->setName("Page " + QString::number(m_pageList.count()+1)); m_pageList.append(page); endInsertRows(); } } void GPageModel::removePage(const int index) { if(index>=0 && index<m_pageList.count()) { beginRemoveRows(QModelIndex(),index,index); qDebug()<<QString("beginRemoveRows(QModelIndex(),%1,%1)").arg(index); GPage * page = m_pageList.at(index); m_pageList.removeAt(index); delete page; endRemoveRows(); } } The class GPage derives from QObject. I am struck trying to figure out what is causing the app to crash while trying to call endRemoveRows(). I get "ASSERT failure in QList::at: "index out of range"" when endRemoveRows() is called.How do I remove the rows from a QAbstracListModel? Is there any other way? I am using Qt 5.1.0 on a Windows 7 64 bit machine.
The code below works fine for me. Your problem is probably elsewhere. This is for Qt 5 due to use of Qt Quick Controls. There are two views accessing the same model, this visually confirms that the model emits proper signals to inform the views of the changes. The page additions and removals are done via the standard insertRows and removeRows methods, exported through Q_INVOKABLE. There's no need for any custom methods on this model, so far. The Q_INVOKABLE is a workaround for some missing functionality for the interface between QML and QAbstractItemModel. main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickWindow> #include <QAbstractListModel> #include <QQmlContext> #include <QtQml> class GPage : public QObject { Q_OBJECT Q_PROPERTY(QString name NOTIFY nameChanged MEMBER m_name) Q_PROPERTY(int number NOTIFY numberChanged MEMBER m_number) QString m_name; int m_number; public: GPage(QObject * parent = 0) : QObject(parent), m_number(0) {} GPage(QString name, int number, QObject * parent = 0) : QObject(parent), m_name(name), m_number(number) {} Q_SIGNAL void nameChanged(const QString &); Q_SIGNAL void numberChanged(int); }; class PageModel : public QAbstractListModel { Q_OBJECT QList<GPage*> m_pageList; public: PageModel(QObject * parent = 0) : QAbstractListModel(parent) {} ~PageModel() { qDeleteAll(m_pageList); } int rowCount(const QModelIndex &) const Q_DECL_OVERRIDE { return m_pageList.count(); } QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE { if (role == Qt::DisplayRole || role == Qt::EditRole) { return QVariant::fromValue<QObject*>(m_pageList.at(index.row())); } return QVariant(); } bool setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE { Q_UNUSED(role); GPage* page = value.value<GPage*>(); if (!page) return false; if (page == m_pageList.at(index.row())) return true; delete m_pageList.at(index.row()); m_pageList[index.row()] = page; QVector<int> roles; roles << role; emit dataChanged(index, index, roles); return true; } Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE { Q_UNUSED(parent); beginInsertRows(QModelIndex(), row, row + count - 1); for (int i = row; i < row + count; ++ i) { QString const name = QString("Page %1").arg(i + 1); GPage * page = new GPage(name, i + 1, this); m_pageList.insert(i, page); QQmlEngine::setObjectOwnership(page, QQmlEngine::CppOwnership); } endInsertRows(); return true; } Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE { Q_UNUSED(parent); beginRemoveRows(QModelIndex(), row, row + count - 1); while (count--) delete m_pageList.takeAt(row); endRemoveRows(); return true; } }; int main(int argc, char *argv[]) { PageModel model1; QGuiApplication app(argc, argv); QQmlApplicationEngine engine; model1.insertRows(0, 1); engine.rootContext()->setContextProperty("model1", &model1); qmlRegisterType<GPage>(); engine.load(QUrl("qrc:/main.qml")); QObject *topLevel = engine.rootObjects().value(0); QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); window->show(); return app.exec(); } #include "main.moc" main.qml import QtQuick 2.0 import QtQml.Models 2.1 import QtQuick.Controls 1.0 ApplicationWindow { width: 300; height: 300 Row { width: parent.width anchors.top: parent.top anchors.bottom: column.top Component { id: commonDelegate Rectangle { width: view.width implicitHeight: editor.implicitHeight + 10 color: "transparent" border.color: "red" border.width: 2 radius: 5 TextInput { id: editor anchors.margins: 1.5 * parent.border.width anchors.fill: parent text: edit.name // "edit" role of the model, to break the binding loop onTextChanged: { display.name = text; model.display = display } } } } ListView { id: view width: parent.width / 2 height: parent.height model: DelegateModel { id: delegateModel1 model: model1 delegate: commonDelegate } spacing: 2 } ListView { width: parent.width / 2 height: parent.height model: DelegateModel { model: model1 delegate: commonDelegate } spacing: 2 } } Column { id: column; anchors.bottom: parent.bottom Row { Button { text: "Add Page"; onClicked: model1.insertRows(delegateModel1.count, 1) } Button { text: "Remove Page"; onClicked: model1.removeRows(pageNo.value - 1, 1) } SpinBox { id: pageNo minimumValue: 1 maximumValue: delegateModel1.count; } } } } main.qrc <RCC> <qresource prefix="/"> <file>main.qml</file> </qresource> </RCC>