I found a QML ListView sample using a C++ QAbstractListModel. However, it took a while to fetch the list model's data and waiting popup was freezing. So, I tried to use QThread samples (a, b, c) in the cpu intensive task sample.
Then, I got the below errors, when a different thread (ThreadHandler::process()) tried to fetch the model data in the main thread (PersonModel::requestPersonData()).
QObject::connect: Cannot queue arguments of type 'QQmlChangeSet'
(Make sure 'QQmlChangeSet' is registered using qRegisterMetaType().)
My question is how to add data into the QAbstractListModel from the different QThread's function. Or is there any way to handle the time consuming list model due to the large data?
Here is the reproducible code. (Qt 5.12.10 MSVC2015 64bit, Qt Creator 4.14.2. windows 10)
Thanks in advance.
personmodel.h
#ifndef PERSONMODEL_H
#define PERSONMODEL_H
#include <QAbstractListModel>
struct Person
{
QString First;
QString Last;
};
class PersonModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged)
public:
explicit PersonModel(QObject *parent = nullptr);
virtual ~PersonModel();
enum PersonRoles {
FirstRole = Qt::UserRole + 1,
LastRole
};
Q_ENUM(PersonRoles)
void addPerson(Person *p);
Person* getPerson(int index);
void clear();
int count() const;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual QHash<int, QByteArray> roleNames() const override;
public slots:
void requestPersonData(void);
void requestDeleteAll();
bool remove(const int inIndex);
signals:
void countChanged();
private:
QHash<int, QByteArray> _roles;
QList<Person *> _people;
};
#endif // PERSONMODEL_H
personmodel.cpp
#include "personmodel.h"
#include "threadhandler.h"
#include <QThread>
#include <QWaitCondition>
#include <QDebug>
#include <time.h>
PersonModel::PersonModel(QObject *parent)
: QAbstractListModel(parent)
{
_roles[FirstRole] = "first";
_roles[LastRole] = "last";
}
PersonModel::~PersonModel()
{
_people.clear();
}
int PersonModel::count() const
{
return rowCount();
}
int PersonModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return _people.count();
}
QVariant PersonModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= _people.count())
return QVariant();
Person *p = _people.at(index.row());
switch (role) {
case FirstRole:
return QVariant::fromValue(p->First);
case LastRole:
return QVariant::fromValue(p->Last);
default:
break;
}
return QVariant();
}
QHash<int, QByteArray> PersonModel::roleNames() const
{
return _roles;
}
void PersonModel::clear()
{
qDeleteAll(_people);
_people.clear();
}
Person *PersonModel::getPerson(int index)
{
return _people.at(index);
}
void PersonModel::addPerson(Person *p)
{
int row = _people.count();
beginInsertRows(QModelIndex(), row, row);
_people.append(p);
endInsertRows();
}
bool PersonModel::remove(const int inIndex)
{
if ((rowCount() <= 0) || (inIndex < 0) || (rowCount() <= inIndex))
return false;
beginRemoveRows(QModelIndex(), inIndex, inIndex);
_people.removeAt(inIndex);
endRemoveRows();
return true;
}
void PersonModel::requestPersonData()
{
QThread* pPThread = new QThread();
ThreadHandler* pHandler = new ThreadHandler();
pHandler->setListModel(this);
pHandler->moveToThread(pPThread);
connect(pPThread, SIGNAL(started()), pHandler, SLOT(process()));
connect(pHandler, SIGNAL(finished()), this, SIGNAL(countChanged()));
// Automatically delete pPThread and pHandler after the work is done.
connect(pHandler, SIGNAL(finished()), pHandler, SLOT(deleteLater()), Qt::QueuedConnection);
connect(pPThread, SIGNAL(finished()), pPThread, SLOT(deleteLater()), Qt::QueuedConnection);
pPThread->start();
}
void PersonModel::requestDeleteAll()
{
for (int i = rowCount() - 1; i >= 0; i--)
{
remove(i);
}
}
threadhandler.h
#ifndef THREADHANDLER_H
#define THREADHANDLER_H
#include <QObject>
class PersonModel;
class ThreadHandler : public QObject
{
Q_OBJECT
public:
explicit ThreadHandler(QObject *parent = nullptr);
void setListModel(PersonModel* personModel);
public slots:
void process();
signals:
void finished();
private:
void doPrimes();
int calculatePrimes(int inRepeat);
private:
PersonModel* m_personModel;
};
#endif // THREADHANDLER_H
threadhandler.cpp
#include "threadhandler.h"
#include "personmodel.h"
#include <QDebug>
#include "time.h"
ThreadHandler::ThreadHandler(QObject *parent) : QObject(parent)
{
}
void ThreadHandler::setListModel(PersonModel *personModel)
{
m_personModel = personModel;
}
void ThreadHandler::process()
{
qDebug() << Q_FUNC_INFO << "thread handler starts";
// simulate the cpu intensive procedure such as db query or 3rd party library call
calculatePrimes(3);
int row = 5;//rowCount();
QString strLastNameIndex;
for (int i = 0; i < row; i++)
{
strLastNameIndex = QString::number(i);
Person* person3 = new Person();
person3->First = "Bob" + strLastNameIndex;
person3->Last = "LastName" + strLastNameIndex;
m_personModel->addPerson(person3);
}
qDebug() << Q_FUNC_INFO << "row count: " << row;
emit finished();
qDebug() << Q_FUNC_INFO << "thread handler ends";
}
#define MAX_PRIME 100000
void ThreadHandler::doPrimes()
{
unsigned long i, num, primes = 0;
for (num = 1; num <= MAX_PRIME; ++num) {
for (i = 2; (i <= num) && (num % i != 0); ++i);
if (i == num)
++primes;
}
printf("Calculated %ld primes.\n", primes);
}
int ThreadHandler::calculatePrimes(int inRepeat)
{
time_t start, end;
time_t run_time;
start = time(NULL);
for (int i = 0; i < inRepeat; ++i) {
doPrimes();
}
end = time(NULL);
run_time = (end - start);
printf("This machine calculated all prime numbers under %d %d times "
"in %lld seconds\n", MAX_PRIME, inRepeat, run_time);
return run_time;
}
main.qml
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQml 2.0
Window {
id: root
width: 640
height: 480
visible: true
title: qsTr("Hello World")
MouseArea {
anchors.fill: parent
onClicked: {
timer.start()
// loadingDialog.is_running = true
console.log("personListView click to request data from qml to C++")
PersonModel.requestDeleteAll();
PersonModel.requestPersonData();
}
}
ListView {
id: personListView
width: 150; height: 400
visible: loadingDialog.is_running === true ? false : true;
model: PersonModel
delegate: personDelegate
Component.onCompleted: {
console.log(PersonModel, model)
console.log(PersonModel.count, model.count)
}
onCountChanged: {
// console.log("personListView onCountChanged getting person data is finished.")
// console.log("personListView onCountChanged after search model count: " + PersonModel.count)
loadingDialog.is_running = false
}
}
Component {
id: personDelegate
Rectangle {
width: personListView.width
height: 30
color: "lightgreen"
Text {
text: model.first + " " + model.last
}
MouseArea {
anchors.fill: parent
propagateComposedEvents: true
onClicked: {
personListView.currentIndex = model.index
console.log("personListView ListView item" + model.index + " is clicked.")
PersonModel.remove(model.index);
}
}
}
}
Timer {
id: timer
interval: 100
repeat: false
running: false
triggeredOnStart: false
onTriggered: {
loadingDialog.is_running = true
timer.stop()
}
}
Rectangle {
id: loadingDialog
width: 50; height: 50
color: "red"
property bool is_running: false
visible: is_running;
NumberAnimation on x {
from: 0
to: 250;
duration: 3000
loops: Animation.Infinite
running: loadingDialog.is_running
}
}
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext> // setContextProperty()
#include "personmodel.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
PersonModel mymodel;
// Add initial data for list model
Person person1;
person1.First = "Bob";
person1.Last = "One";
Person person2;
person2.First = "Bob2";
person2.Last = "Two";
mymodel.addPerson(&person1);
mymodel.addPerson(&person2);
engine.rootContext()->setContextProperty("PersonModel", &mymodel);
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();
}
Since the model is related to the view then you cannot modify it directly from another thread. In this case it is better to create a signal that sends the information to the model:
threadhandler.h
signals:
void finished();
void sendPerson(Person *person);
threadhandler.cpp
void ThreadHandler::process()
{
qDebug() << Q_FUNC_INFO << "thread handler starts";
// simulate the cpu intensive procedure such as db query or 3rd party library call
calculatePrimes(3);
int row = 5;//rowCount();
QString strLastNameIndex;
for (int i = 0; i < row; i++)
{
strLastNameIndex = QString::number(i);
Person* person3 = new Person;
person3->First = "Bob" + strLastNameIndex;
person3->Last = "LastName" + strLastNameIndex;
Q_EMIT sendPerson(person3);
}
qDebug() << Q_FUNC_INFO << "row count: " << row;
emit finished();
qDebug() << Q_FUNC_INFO << "thread handler ends";
}
personmodel.cpp
void PersonModel::requestPersonData()
{
QThread* pPThread = new QThread();
ThreadHandler* pHandler = new ThreadHandler();
// pHandler->setListModel(this);
pHandler->moveToThread(pPThread);
connect(pPThread, &QThread::started, pHandler, &ThreadHandler::process);
connect(pHandler, &ThreadHandler::finished, this, &PersonModel::countChanged);
connect(pHandler, &ThreadHandler::sendPerson, this, &PersonModel::addPerson);
// Automatically delete pPThread and pHandler after the work is done.
connect(pHandler, &ThreadHandler::finished, pHandler, &QObject::deleteLater, Qt::QueuedConnection);
connect(pPThread, &QThread::finished, pPThread, &QObject::deleteLater, Qt::QueuedConnection);
pPThread->start();
}
Remove the setListModel method and everything related to that method.
Related
Problems with QML Connections
qrc:/main.qml:12:5: QML Connections: Detected function "onTop" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.
qrc:/main.qml:13: ReferenceError: classA is not defined
as the picture shows,I cannot link to my Qt Signal
The problem is that QML cannot recognize my Qt signal void onTop(), how can I solve it? In Qt 6
I feel that QML has a lot of problems, it is not as easy to use as traditional QWidget, and there are few related materials.
head file
#pragma once
#include <QOBject>
#include <QUdpSocket>
class CupSingleHelper : public QObject
{
Q_OBJECT
Q_PROPERTY(NOTIFY onTop)
public:
CupSingleHelper(QObject *parent = nullptr);
~CupSingleHelper();
public:
void initSocket();
void setAppPid(qint64 app_pid);
void readData();
Q_INVOKABLE void printStr2();
public:
QUdpSocket* udp_socket = nullptr;
qint64 pid;
public slots:
void printStr();
signals:
void onTop();
};
cpp file
#include "CupSingleHelper.h"
#include <QNetWorkDatagram>
#include <qmessagebox.h>
#include <QDebug>
CupSingleHelper::CupSingleHelper(QObject* parent)
{
}
void CupSingleHelper::printStr()
{
qDebug() << "1111111";
}
void CupSingleHelper::printStr2()
{
qDebug() << "22222";
}
void CupSingleHelper::setAppPid(qint64 app_pid)
{
this->pid = app_pid;
}
void CupSingleHelper::initSocket()
{
QString data = ":start";
data.insert(0, QString::number(pid));
QByteArray byte_data = data.toLatin1();
udp_socket = new QUdpSocket(this);
bool sStatus = false;
sStatus = udp_socket->bind(QHostAddress::LocalHost, 8898);
if (sStatus == false) { udp_socket->writeDatagram(byte_data, QHostAddress::LocalHost, 8889); exit(1); }
connect(udp_socket, &QUdpSocket::readyRead, this, &CupSingleHelper::readData);
udp_socket->writeDatagram(byte_data, QHostAddress::LocalHost, 8889);
}
void CupSingleHelper::readData()
{
while (udp_socket->hasPendingDatagrams())
{
QNetworkDatagram datagram = udp_socket->receiveDatagram();
QString receive_data = datagram.data().data();
QString temp = receive_data.section(":", 0, 0);
if (temp.toInt() == pid)
{
break;
}
temp = receive_data.section(":", 1, 1);
if (temp.compare("start") == 0)
{
emit onTop();
}
}
}
CupSingleHelper::~CupSingleHelper()
{
if(udp_socket != nullptr)
{
delete[]udp_socket;
}
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTranslator>
#include <QLocale>
#include "CupSingleHelper.h"
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
CupSingleHelper cup_single_helper;
qint64 app_pid = QCoreApplication::applicationPid();
cup_single_helper.setAppPid(app_pid); //set process id
cup_single_helper.initSocket();//init udpsocket to listen
QTranslator t;
QLocale ql;
//Check system language and load
if (ql.language() == QLocale::Chinese)
{
bool status = t.load(":/x64/Debug/cuptools_zh.qm");
}
if (ql.language() != QLocale::English)
{
app.installTranslator(&t);
}
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QQmlContext* rootContext = engine.rootContext();
rootContext->setContextProperty("classA",&cup_single_helper);
return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
visible: true
width: 500
height: 720
title: qsTr("CupTools")
property var strd: "22222"
Connections {
target: classA
function onTop(){
label12.text = strd
}
}
Text {
id: label12
anchors {
top: parent.top
horizontalCenter: parent.horizontalCenter
topMargin: 20
}
text:"11111"
}
Button {
id: mbutton
anchors.centerIn:parent
text: "Click"
onClicked: classA.printStr()
}
}
Your code has the following problems:
If you are going to declare a signal then you should not use Q_PROPERTY. Remove Q_PROPERTY(NOTIFY onTop).
You must establish the contexproperties before loading the .qml since otherwise at the time of loading those objects will not be defined.
If you want to connect a signal then the syntax is on<signal> where <signal> must be in camelcase format, in your case it must be onOnTop.
In Qt6 the versions of the QML modules are not necessary.
Since udp_socket is a child of the class then it is not necessary to eliminate it since it will cause a segmentation fault since you are eliminating the pointer 2 times.
#ifndef CUPSINGLEHELPER_H
#define CUPSINGLEHELPER_H
#include <QObject>
class QUdpSocket;
class CupSingleHelper : public QObject
{
Q_OBJECT
public:
CupSingleHelper(QObject *parent = nullptr);
~CupSingleHelper();
public:
void initSocket();
void setAppPid(qint64 app_pid);
void readData();
Q_INVOKABLE void printStr2();
public:
QUdpSocket* udp_socket = nullptr;
qint64 pid;
public slots:
void printStr();
signals:
void onTop();
};
#endif // CUPSINGLEHELPER_H
#include "cupsinglehelper.h"
#include <QNetworkDatagram>
#include <QUdpSocket>
CupSingleHelper::CupSingleHelper(QObject* parent):QObject(parent)
{
}
void CupSingleHelper::printStr()
{
qDebug() << "1111111";
}
void CupSingleHelper::printStr2()
{
qDebug() << "22222";
}
void CupSingleHelper::setAppPid(qint64 app_pid)
{
this->pid = app_pid;
}
void CupSingleHelper::initSocket()
{
QString data = ":start";
data.insert(0, QString::number(pid));
QByteArray byte_data = data.toLatin1();
udp_socket = new QUdpSocket(this);
bool sStatus = false;
sStatus = udp_socket->bind(QHostAddress::LocalHost, 8898);
if (sStatus == false) { udp_socket->writeDatagram(byte_data, QHostAddress::LocalHost, 8889); exit(1); }
connect(udp_socket, &QUdpSocket::readyRead, this, &CupSingleHelper::readData);
udp_socket->writeDatagram(byte_data, QHostAddress::LocalHost, 8889);
}
void CupSingleHelper::readData()
{
while (udp_socket->hasPendingDatagrams())
{
QNetworkDatagram datagram = udp_socket->receiveDatagram();
QString receive_data = QString::fromUtf8(datagram.data());
QString temp = receive_data.section(":", 0, 0);
if (temp.toInt() == pid)
{
break;
}
temp = receive_data.section(":", 1, 1);
if (temp.compare("start") == 0)
{
emit onTop();
}
}
}
CupSingleHelper::~CupSingleHelper()
{
}
#include "cupsinglehelper.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
CupSingleHelper cup_single_helper;
qint64 app_pid = QCoreApplication::applicationPid();
cup_single_helper.setAppPid(app_pid); //set process id
cup_single_helper.initSocket();//init udpsocket to listen
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);
QQmlContext* rootContext = engine.rootContext();
rootContext->setContextProperty("classA",&cup_single_helper);
engine.load(url);
return app.exec();
}
import QtQuick
import QtQuick.Window
import QtQuick.Controls
Window {
visible: true
width: 500
height: 720
title: qsTr("CupTools")
property string strd: "22222"
Connections {
target: classA
function onOnTop(){
label12.text = strd
}
}
Text {
id: label12
anchors {
top: parent.top
horizontalCenter: parent.horizontalCenter
topMargin: 20
}
text:"11111"
}
Button {
id: mbutton
anchors.centerIn:parent
text: "Click"
onClicked: classA.printStr()
}
}
I need to present all the data in the QLinkedList container (this was given by the task). I created two classes, DataObject for my delegates in ListView, Glav for container with DataObject objects. I have a button on which I add data to the container (the addItem function in the Glav class). The data is added but not displayed in the ListView. How to display them? I tried it through signals, it didn't work.
Here is the complete code of the project.
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QStringListModel>
#include <QQmlContext>
#include <QLinkedList>
#include <QQuickView>
#include <container.h>
#include <dataobject.h>
#include "glav.h"
//#include <container.cpp>
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterUncreatableType<tile::Glav>( "Tile", 1, 0, "DataItem", "interface" );
qmlRegisterType<tile::Glav>( "Tile", 1, 0, "Glav");
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();
}
main.qml
import QtQuick 2.12
import QtQuick.Controls 2.5
//import App 1.0
import Tile 1.0
ApplicationWindow {
width: 640
height: 480
visible: true
title: qsTr("Scroll")
// Container {
// id: container
// }
Glav {
id: glav
}
Row {
id: buttons
spacing: 20
padding: 10
anchors.horizontalCenter: parent.horizontalCenter
RoundButton {
padding: 20
text: "add item"
onClicked: {
glav.addItem()
listView.currentIndex = -1
}
}
Connections{
target: myModelObjectWhichWasSetAsContextProperty
onRowsInserted: console.log("rows were inserted")
}
ScrollView {
anchors.fill: parent
anchors.topMargin: buttons.implicitHeight + 10
ListView {
id: listView
width: parent.width
model: glav.list
//required model
delegate: Text {
property var d
d: model.modelData.id
text: model.modelData.name
}
removeDisplaced: Transition {
NumberAnimation { properties: "x,y"; duration: 100; easing.type: Easing.InOutQuad }
}
}
}
}
dataobject.h
#ifndef DATAOBJECT_H
#define DATAOBJECT_H
#include <QObject>
namespace tile {
class DataObject : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY changed)
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY changed)
Q_PROPERTY(int id READ id WRITE setId NOTIFY changed)
public:
explicit DataObject(QString name = "Wana", QString color = "red", int id = 1, QObject *parent = nullptr);
QString name();
void setName(const QString &name);
QString color();
void setColor(const QString &color);
int id() const;
void setId(int id);
signals:
void changed();
private:
QString m_name;
QString m_color;
int m_id;
};
}
#endif // DATAOBJECT_H
dataobject.cpp
#include "dataobject.h"
#include <QDebug>
namespace tile {
DataObject::DataObject(QString name, QString color, int id, QObject* parent) :
QObject(parent)
, m_name (name)
, m_color (color)
, m_id (id)
{
//new DataObject();
qDebug() << m_name;
//emit changed();
}
QString DataObject::name()
{
return m_name;
}
void DataObject::setName(const QString &name)
{
m_name = name;
qDebug() << m_name;
emit changed();
}
QString DataObject::color()
{
return m_color;
}
void DataObject::setColor(const QString &color)
{
m_color = color;
emit changed();
}
int DataObject::id() const
{
return m_id;
}
void DataObject::setId(int id)
{
m_id = id;
emit changed();
}
}
glav.h
#ifndef GLAV_H
#define GLAV_H
#include <QObject>
#include <QLinkedList>
namespace tile {
class Glav : public QObject
{
Q_OBJECT
Q_PROPERTY( QLinkedList<QObject *> list READ list CONSTANT )
public:
explicit Glav(QObject *parent = nullptr);
//QLinkedList<QObject *> list();
//void setList(const QLinkedList<QObject *> &list);
Q_INVOKABLE QLinkedList<QObject *> pollist();
Q_INVOKABLE void addItem();
// Q_INVOKABLE QVariant pol();
signals:
void changed();
private:
QLinkedList<QObject *> m_list;
QLinkedList<QObject *> list();
};
}
#endif // GLAV_H
glav.cpp
#include "glav.h"
#include "dataobject.h"
#include <QDebug>
#include <QAbstractListModel>
namespace tile {
Glav::Glav(QObject *parent) : QObject(parent)
{
QLinkedList<QObject *> dataList = {
new DataObject("Item 1", "red"),
new DataObject("Item 2", "green"),
new DataObject("Item 3", "blue"),
new DataObject("Item 9", "yellow")
};
m_list << dataList;
QVariant::fromValue(m_list);
}
QLinkedList<QObject *> Glav::list()
{
return m_list;
}
//void Glav::setList(const QLinkedList<QObject *> &list)
//{
// m_list = list;
//}
//QVariant Glav::pol(){
// QVariant re = QVariant::fromValue(m_list);
// return re;
//}
QLinkedList<QObject *> Glav::pollist()
{
//qDebug() << QVariant::fromValue(m_list);
//QLinkedList<QObject *> f = m_list;
//QVariant::fromValue(m_list);
//qDebug() <<m_list;
return m_list;
}
void Glav::addItem()
{
qDebug() << m_list.count();
m_list << new DataObject();
qDebug() << m_list.count();
emit changed();
}
}
Your Glav class has a changed() signal but it doesn't do anything because your list property is constant (Q_PROPERTY( QLinkedList<QObject *> list READ list CONSTANT ))
You should change it to this:
Q_PROPERTY(QLinkedList<QObject*> list READ list NOTIFY changed)
That way you can let the ListView know that the list has just been changed by emitting the changed() signal.
Also, it's better to name the signal in accordance with the property it corresponds to: listChanged.
More info on how Q_PROPERTY works in the official documentation: https://doc.qt.io/qt-5/properties.html
According to HorizontalHeaderView's doc, If the model is a QAbstractTableModel, then the header will display the model's horizontal headerData(); otherwise, the model's data(). but It's not even calling it in my case. but QTableView from widgets module seems to work just fine.
here is my main.qml:-
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
id: root
HorizontalHeaderView {
id: header
syncView: tb
anchors.top: parent.top
// model: KoolModel // specify model explicitly also does not work
delegate: Button {
text: display
}
}
TableView {
id: tb
width: parent.width
height: parent.height - header.height
anchors.top: header.bottom
onWidthChanged: forceLayout()
model: KoolModel
columnWidthProvider: function (_) {
return root.width / 3
}
delegate: Frame {
Label {
text: display
anchors.centerIn: parent
}
}
}
}
here is my model:-
#include <QApplication>
#include <QGuiApplication>
#include <QTableView>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QAbstractTableModel>
#include <QString>
#include <map>
class Exams : public QAbstractTableModel {
private:
struct DS {
QString title;
unsigned int marks;
int state;
std::vector<int>* questions = nullptr;
};
//id and exams
std::map<int, DS> exams;
public:
Exams()
{
for (int i = 0; i < 10; i++) {
DS exam { "Exam" + QString::number(i), 0, (i * 3) / 2, nullptr }; // fill with garbage data for now
exams[i] = exam;
}
}
int rowCount(const QModelIndex& parent) const override
{
return exams.size();
}
int columnCount(const QModelIndex& parent) const override
{
return 3;
}
QVariant data(const QModelIndex& index, int role) const override
{
if (role == Qt::DisplayRole) {
if (index.column() == 0)
return exams.at(index.row()).title;
else if (index.column() == 1)
return exams.at(index.row()).marks;
else if (index.column() == 2)
return exams.at(index.row()).state;
}
qDebug() << "oops";
return QVariant();
}
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
qDebug() << "headerData is at least called";
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
switch (section) {
case 0:
return QString("Title");
case 1:
return QString("Marks");
case 2:
return QString("state");
}
}
return QVariant();
}
QHash<int, QByteArray> roleNames() const override
{
return { { Qt::DisplayRole, "display" } };
}
};
int main(int argc, char* argv[])
{
Exams exams;
// widgets seems to work
// QApplication app(argc, argv);
// QTableView widget;
// widget.setModel(&exams);
// widget.show();
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("KoolModel", &exams);
const QUrl url(QStringLiteral("qrc:/main.qml"));
engine.load(url);
return app.exec();
}
The problem is caused by a naming conflict between the Button's display property and the role. The solution is to access the role through the model explicitly:
// ...
delegate: Button {
text: model.display
}
// ...
I have been trying to use the SqlQueryModel generic model from the qt documentation but I just can't seem to get it working properly with qml.
very simular to another problem I saw on Stack OverFlow but they haven't got an answer yet either
My QSqlQueryModel doesn't show data in the listview
I can query the database and I get the right amount of results in my listview but if I try to access a property via the RoleName then it says the property is undefined
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "wordmodel.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
WordModel *wordListModel = new WordModel( qApp);
engine.rootContext()->setContextProperty("WLModel", wordListModel);
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
return app.exec();
}
SqlQueryModel.h
#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H
#include <QSqlQueryModel>
#include <QHash>
#include <QByteArray>
class SqlQueryModel : public QSqlQueryModel
{
Q_OBJECT
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole ) const;
inline RoleNameHash roleNames() const { return *roles; }
public:
explicit SqlQueryModel(QObject *parent = 0);
Q_INVOKABLE void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
Q_INVOKABLE void setQuery();
QHash<int, QByteArray> generateRoleNames();
//inline RoleNameHash roleNames() const { return *roles; }
QHash<int, QByteArray> roleNames() const{return roles;}
signals:
void countChanged();
public slots:
void connectToDb();
void closeDB();
private:
//QSqlQuery SQL_SELECT;
QHash<int, QByteArray> roles;
QSqlDatabase mydb;
};
#endif // SQLQUERYMODEL_H
SqlQueryModel.cpp
#include "sqlquerymodel.h"
#include <QSqlRecord>
#include <QSqlField>
#include <QDebug>
SqlQueryModel::SqlQueryModel(QObject *parent) :
QSqlQueryModel(parent)
{
mydb=QSqlDatabase::addDatabase("QSQLITE");
QString dbPath = "E://Qt//sqlite//dictionary.db";
mydb.setDatabaseName(dbPath);
mydb.setUserName("admin");
mydb.setPassword("admin");
}
void SqlQueryModel::connectToDb()
{
if(!mydb.open())
{
qDebug() << "Database didnt open";
}
else
{
qDebug() << "Your database is open";
}
}
void SqlQueryModel::closeDB()
{
mydb.close();
}
void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)
{
connectToDb();
QSqlQueryModel::setQuery("SELECT * FROM words_en WHERE word LIKE 'young%' LIMIT 10",mydb);
generateRoleNames();
closeDB();
}
void SqlQueryModel::setQuery()
{
connectToDb();
QSqlQueryModel::setQuery("SELECT * FROM words_en WHERE word LIKE 'young%' LIMIT 10");
generateRoleNames();
closeDB();
}
QHash<int, QByteArray> SqlQueryModel::generateRoleNames()
{
roles.clear();
for( int i = 0; i < record().count(); i++) {
roles[Qt::UserRole + i + 1] = record().fieldName(i).toLatin1();
qDebug() << roles[Qt::UserRole + i + 1];
}
return roles;
}
QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
{
qDebug() << "ALALLALALALALALA";
QVariant value = QSqlQueryModel::data(index, role);
qDebug() << 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.qml
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
visible: true
width: 800
height: 800
// here i tried putting the class into a Qml type with the qmlRegisterType()
// but also to no avail
// SqlQueryModel{
// id:something
// Component.onCompleted: {
// something.setQuery()
// }
// }
ListView{
width:parent.width
height:parent.height
model:wordListModel
delegate: Item{
width:parent.width
height: width/10
Text {
id: name
text: word
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
anchors.fill:parent
Component.onCompleted: {
console.log(id)
console.log(word)
}
}
}
}
}
Which version of Qt are you using. Method generateRoleNames() has been deprecated, reimplement roleNames() instead of generateRoleNames().
I have a project that I am working on, that will hopefully result in C++ code with a QML UI as a console, communicating with other consoles and equipment.
My test setup is based on a RaspBerry Pi running UDP comms (works fine into a normal Qt application).
I have tried to port to Qt Quick, and use a simple QML UI, but if I declare the subclass early in my "main.cpp" it doesn't connect properly.
If I declare it in the main.cpp "main" function, then I have scope issues with the subclass functions that I want to use to transfer data.
Open to suggestions / critique......and I am 100% sure that there is a "proper" way of doing this, so if anyone wants to point me at it......I will be enraptured!
Qt version is Qt5.3.
import QtQuick 2.2
import QtQuick.Controls 1.1
Rectangle {
width: 440; height: 150
color: "orange"
Column {
anchors.fill: parent; spacing: 20
Text {
text: rootItem.theChange
font.pointSize: 12; anchors.horizontalCenter: parent.horizontalCenter
}
}
}
// signalgrab.h
#ifndef SIGNALGRAB_H
#define SIGNALGRAB_H
#include <QObject>
#include <QUdpSocket>
#include <QString>
class SignalGrab : public QObject
{
Q_OBJECT
public:
explicit SignalGrab(QObject *parent = 0);
int SendValue();
void setupUDP();
signals:
public slots:
void readyRead();
private:
QUdpSocket *socket;
QHostAddress groupAddress;
};
#endif // SIGNALGRAB_H
// signalgrab.cpp
#include "signalgrab.h"
int i3 = 0;
SignalGrab::SignalGrab(QObject *parent) :
QObject(parent)
{
// create a QUDP socket
socket = new QUdpSocket(this);
socket->bind(QHostAddress("0.0.0.0"), 45454);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
qDebug() << "Socket connected ok!";
}
void SignalGrab::readyRead()
{
// when data comes in
QByteArray buffer;
buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(),
&sender, &senderPort);
std::string s1 (buffer);
std::string s2 = s1.substr(26,sizeof(s1));
i3 = atoi(s2.data());
SendValue();
}
int SignalGrab::SendValue()
{
qDebug() << "sending i3 as: "<< i3;
return i3;
}
// main.cpp
#include <QtGui>
#include <QApplication>
#include <QQmlContext>
#include <QQuickView>
#include <QString>
#include "signalgrab.h"
int y=13;
// ///////////////////////////////////
//SignalGrab nc;
// ///////////////////////////////////
class Object : public QObject
{
Q_OBJECT
Q_PROPERTY(QString theChange READ getTheChange NOTIFY changeOfStatus)
public:
Object()
{
changeMe = false;
myTimer = new QTimer(this);
myTimer->start(5000);
connect(myTimer, SIGNAL(timeout()), this, SLOT(testSlot()));
}
QString getTheChange()
{
if (theValue == 0)
{
return "The Acid QML Test";
}
if (theValue == 1)
{
y = (fetchValue());
qDebug() << "New y!" << y;
return QString("Returning %1").arg(y);
}
return "nothing has happened yet";
}
int fetchValue()
{
// return (nc::SendValue());
return y;
}
Q_INVOKABLE void someFunction(int i)
{
if ( i == 0) {
theValue = 0;
}
if (i == 1) {
theValue = 1;
}
emit changeOfStatus(i);
}
signals:
void changeOfStatus(int i) ;
public slots:
void testSlot()
{
if (changeMe)
{
someFunction(0);
} else {
someFunction(1);
}
changeMe = !changeMe;
}
private:
bool changeMe;
int theValue;
QTimer *myTimer;
};
#include "main.moc"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Object myObj;
// //////////////////////////////
SignalGrab nc; //added
// //////////////////////////////
nc.SendValue();
QQuickView view;
view.rootContext()->setContextProperty("rootItem", (QObject *)&myObj);
view.setSource(QUrl::fromLocalFile("main.qml"));
view.show();
return app.exec();
}