QStyledItemDelegate does not show desired Widget in the cells of QTableView - qt

I want to test how to put QSpinBox into cells of QTableView in Qt 6.2.2, but the delegate does not appear to work, which means it does not show QSpinBox in the cells, and I've tested the delegate member functions by qDebug which reveals that none of these functions work! could anyone help to fix the problem? Many thanks in advance!
Below is the code:
testtabledelegate.h
#ifndef TESTTABLEDELEGATE_H
#define TESTTABLEDELEGATE_H
#include <QStyledItemDelegate>
#include <QWidget>
class TestTableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
TestTableDelegate(QObject *parent = nullptr);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
};
#endif // TESTTABLEDELEGATE_H
testtabledelegate.cpp
#include "testtabledelegate.h"
#include <QSpinBox>
TestTableDelegate::TestTableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget *TestTableDelegate ::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug() << "Draw Control";
Q_UNUSED(option);
Q_UNUSED(index);
QSpinBox *editor = new QSpinBox(parent);
editor->setFrame(false);
editor->setMinimum(0);
editor->setMaximum(10000);
return editor;
}
void TestTableDelegate ::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::EditRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}
void TestTableDelegate ::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
}
void TestTableDelegate ::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
testtablemodel.h
#ifndef TESTTABLEMODEL_H
#define TESTTABLEMODEL_H
#include <QAbstractTableModel>
class TestTableModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit TestTableModel(QObject *parent = nullptr);
protected:
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QVariant data(const QModelIndex &index, int role) const;
private:
//QStringList *testList = QStringList() << "R1" << "R2" << "R3" << "R4" << "R5";
};
#endif // TESTTABLEMODEL_H
testtablemodel.cpp
#include "testtablemodel.h"
TestTableModel::TestTableModel(QObject *parent) : QAbstractTableModel(parent)
{
}
int TestTableModel::rowCount(const QModelIndex &parent) const
{
return 5;
}
int TestTableModel::columnCount(const QModelIndex &parent) const
{
return 5;
}
QVariant TestTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal)
return QStringLiteral("Field %1").arg(section);
else if (orientation == Qt::Vertical)
return section + 1;
else
return(QVariant());
}
else
return(QVariant());
}
QVariant TestTableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
return index.row();
}
else if (role == Qt::TextAlignmentRole)
{
return Qt::AlignCenter;
}
else
{
return QVariant();
}
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
//#include "testtableheader.h"
#include "testtablemodel.h"
#include "testtabledelegate.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
TestTableModel *testModel = new TestTableModel();
TestTableDelegate *testDelegate = new TestTableDelegate(this);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableView>
#include <QStyledItemDelegate>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableView->setAlternatingRowColors(true);
ui->tableView->setModel(testModel);
ui->tableView->setItemDelegate(testDelegate);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Related

Accessing item of QAbstractItemModel via Pointer gets NULL after several reads

I need to access items directly of an QAbstractListModel from QML.
I realized it by returning the address from a function from the model:
Function is
Q_INVOKABLE DataSourceObject *dataPointer(const QModelIndex &index);
#ifndef DATASOURCEMODEL_H
#define DATASOURCEMODEL_H
#include "datasourceobject.h"
#include <QAbstractListModel>
class DataSourceModel : public QAbstractListModel
{
Q_OBJECT
public:
enum datasourceRoles {
idRole = Qt::UserRole ,
nameRole,
unitRole,
valueRole
};
explicit DataSourceModel(QObject *parent = nullptr);
void addDataSourceObject(DataSourceObject *dataSourceObject);
Q_INVOKABLE QVariantMap get(int row) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Q_INVOKABLE DataSourceObject *dataPointer(const QModelIndex &index);
Qt::ItemFlags flags(const QModelIndex& index) const override;
QHash<int, QByteArray> roleNames() const override;
//bool checkIndex(const QModelIndex &index) const;
private:
QList<DataSourceObject*> m_DataSourceObjects;
};
#endif // DATASOURCEMODEL_H
#include "datasourcemodel.h"
#include <qdebug.h>
DataSourceModel::DataSourceModel(QObject *parent)
: QAbstractListModel(parent)
{
}
QVariantMap DataSourceModel::get(int row) const
{
return m_DataSourceObjects[row]->toMap();
}
void DataSourceModel::addDataSourceObject(DataSourceObject *dataSourceObject)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_DataSourceObjects << dataSourceObject;
endInsertRows();
}
int DataSourceModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_DataSourceObjects.count();
}
QVariant DataSourceModel::data(const QModelIndex &index, int role) const
{
if(index.row() < 0 || index.row() >= m_DataSourceObjects.count() || !index.isValid())
return QVariant();
DataSourceObject *dataSourceObject = m_DataSourceObjects[index.row()];
if (role == idRole)
return dataSourceObject->id();
else if (role == nameRole) {
return dataSourceObject->name();
}
else if (role == unitRole) {
return dataSourceObject->unit();
}
else if (role == valueRole)
return dataSourceObject->value();
return QVariant();
}
bool DataSourceModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
DataSourceObject *dataSourceObject = m_DataSourceObjects[index.row()];
if (data(index, role) != value) {
if(role == idRole)
dataSourceObject->setId(value.toInt());
else if(role == nameRole)
dataSourceObject->setName(value.toString());
else if(role == unitRole)
dataSourceObject->setUnit(value.toString());
else if(role == valueRole)
dataSourceObject->setValue(value.toDouble());
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
DataSourceObject *DataSourceModel::dataPointer(const QModelIndex &index)
{
qDebug() << m_DataSourceObjects[index.row()];
return m_DataSourceObjects[index.row()];
}
Qt::ItemFlags DataSourceModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable; // FIXME: Implement me!
}
QHash<int, QByteArray> DataSourceModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[idRole] = "id";
roles[nameRole] = "name";
roles[unitRole] = "unit";
roles[valueRole] = "value";
return roles;
}
I can access the item and it's propertys from QML and the GUI gets automatically updated with Q_PROPERTY on the gui.
text: dataSourceModel.dataPointer(dataSourceModel.index(88,0)).value
Object:
#ifndef DATASOURCEOBJECT_H
#define DATASOURCEOBJECT_H
#include <QString>
#include <QVariantMap>
class DataSourceObject : public QObject
{
Q_OBJECT
Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged)
public:
explicit DataSourceObject(QObject *parent = nullptr);
DataSourceObject(const QJsonObject &obj);
int id() const;
void setId(int id);
QString name() const;
void setName(const QString &name);
QString unit() const;
void setUnit(const QString &unit);
Q_INVOKABLE double value() const;
void setValue(double value);
QVariantMap toMap() const;
signals:
void valueChanged();
private:
int m_id;
QString m_name;
QString m_unit;
double m_value;
};
#endif // DATASOURCEOBJECT_H
The property "value" of the items get's changed during runtime in background.
After circa 80 changes of the propertys the pointer that points to the object returns NULL. So I assume the item in the model changed it's address.
How can I prevent the model to change the addresses of it's item.
But also when I call again Q_INVOKABLE DataSourceObject *dataPointer(const QModelIndex &index); to get the address again, the "old" address ist returned but a "QObject" instead of "DataSourceObject"

How to update QAbstractItemModel view when a Data is updated

I use the Qt example for QAbstractItemModel and I try to update an Item to a given index.
I tried to use emit DataChangedbut it doesn't work, the view is not updated.
Here is an example:
What I want: When you click on the button, it will update Data at index 0, the type of the animal will be changed, it will become a Lion.
#include <QAbstractListModel>
#include <QStringList>
#include <qqmlcontext.h>
//![0]
class Animal
{
public:
Animal(const QString &type, const QString &size);
//![0]
QString type() const;
QString size() const;
void setType(QString q) {
m_type = q;
}
private:
QString m_type;
QString m_size;
//![1]
};
class AnimalModel : public QAbstractListModel
{
Q_OBJECT
public:
Q_INVOKABLE void test() ;
void setName(const QString &name);
enum AnimalRoles {
TypeRole = Qt::UserRole + 1,
SizeRole
};
AnimalModel(QObject *parent = 0);
//![1]
//!
//!
void setContext(QQmlContext *ctx) {
m_ctx = ctx;
}
void addAnimal(const Animal &animal);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
protected:
private:
QList<Animal> m_animals;
QQmlContext* m_ctx;
signals:
void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight);
//![2]
};
//![2]
model.h
#include "model.h"
#include "qDebug"
Animal::Animal(const QString &type, const QString &size)
: m_type(type), m_size(size)
{
}
QString Animal::type() const
{
return m_type;
}
QString Animal::size() const
{
return m_size;
}
AnimalModel::AnimalModel(QObject *parent)
: QAbstractListModel(parent)
{
}
void AnimalModel::addAnimal(const Animal &animal)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_animals << animal;
endInsertRows();
}
void AnimalModel::test() {
m_animals[0].setType("Lion");
emit dataChanged(QModelIndex(),QModelIndex());
//I also tried:
QModelIndex topLeft = createIndex(0,0);
emit dataChanged(topLeft, topLeft);
}
int AnimalModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return m_animals.count();
}
QVariant AnimalModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= m_animals.count())
return QVariant();
const Animal &animal = m_animals[index.row()];
if (role == TypeRole)
return animal.type();
else if (role == SizeRole)
return animal.size();
return QVariant();
}
//![0]
QHash<int, QByteArray> AnimalModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[TypeRole] = "type";
roles[SizeRole] = "size";
return roles;
}
//![0]
model.cpp
#include "model.h"
#include <QGuiApplication>
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
//![0]
int main(int argc, char ** argv)
{
QGuiApplication app(argc, argv);
AnimalModel model;
model.addAnimal(Animal("Wolf", "Medium"));
model.addAnimal(Animal("Polar bear", "Large"));
model.addAnimal(Animal("Quoll", "Small"));
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
QQmlContext *ctxt = view.rootContext();
ctxt->setContextProperty("myModel", &model);
//![0]
view.setSource(QUrl("qrc:view.qml"));
view.show();
return app.exec();
}
main.cpp
import QtQuick 2.0
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.2
import QtQml.Models 2.1
import QtQuick.Controls.Styles 1.2
//![0]
ListView {
width: 200; height: 250
model: myModel
delegate: Text { text: "Animal: " + type + ", " + size }
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: {
}
}
Button {
anchors.bottom: parent.bottom
width:50; height:50
text:"click"
onClicked: {
myModel.test()
}
}
}
//![0]
View.qml
Do you have any idea why it doesn't work ?
Thanks a lot !
Don't redefine signals in subclasses.
Remove the following lines (in model.h) and it should work as expected :
signals:
void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight);
Also this when calling dataChanged() you have to specify a valid QModelIndex. This is correct :
// I also tried:
QModelIndex topLeft = createIndex(0,0);
emit dataChanged(topLeft, topLeft);
You need create topLeft index and bottom right index and emit dataChanged. This update all model data in view.
QModelIndex topLeft = createIndex(0,0);
QModelIndex bottomRight = createIndex( rows count ,0);
emit dataChanged( topLeft, bottomRight );

how can i Polutate table Widget with combo Box and spinBox delegates

How can i have different delegates in different columns of a table. for instance, i want spinbox delegate in 1st column and combobox in 2nd column.
i took an example of spinbox delegate from examples directories and tried to do the changes:
main.cpp
#include "test1.h"
#include <QApplication>
#include <QHeaderView>
#include <QStandardItemModel>
#include <QTableWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStandardItemModel model(4, 2);
QTableView tableView;
tableView.setModel(&model);
SpinBoxDelegate sdelegate;
ComboBoxDelegate comDel;
tableView.setItemDelegate(&sdelegate);
tableView.horizontalHeader()->setStretchLastSection(true);
for (int row = 0; row < 4; ++row)
{
for (int column = 0; column < 1; ++column)
{
tableView.setItemDelegate(&comDel);
QModelIndex index = model.index(0, 0, QModelIndex());
}
for (int column = 1; column < 2; ++column)
{
tableView.setItemDelegate(&sdelegate);
QModelIndex index = model.index(1, 1, QModelIndex());
}
}
tableView.setWindowTitle(QObject::tr("Spin Box & combo Box Delegate"));
tableView.show();
return app.exec();
}
test1.h my header file
#ifndef LINEEDIT_H
#define LINEEDIT_H
#include <QStyledItemDelegate>
class SpinBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
SpinBoxDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
void setEditorData(QWidget *editor, const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
};
class ComboBoxDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ComboBoxDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
#endif // LINEEDIT_H
test1.cpp my cpp file
#include "test1.h"
#include <QSpinBox>
#include <QComboBox>
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */)/* const*/
{
QSpinBox *editor = new QSpinBox(parent);
editor->setFrame(false);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
}
void SpinBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) /*const*/
{
int value = index.model()->data(index, Qt::EditRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) /*const*/
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
}
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) /*const*/
{
editor->setGeometry(option.rect);
}
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
//QStringList list ;
//list << "srikanth" << "dilip";
//editor->addItems(list);
editor->installEventFilter(const_cast<ComboBoxDelegate*>(this));
return editor;
}
void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->addItem(value);
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();
model->setData(index, value);
}
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}

qt5.3 tableview minimum size not work

i want to create calendar widget use qtableview. i test successful on Qt5.4, but on Qt5.3, the qtableview minimum size not work.
i wanto to set fixed size 300 * 300
Qt5.4 is perfect,but in Qt5.3 tableview cant show completely: http://i.stack.imgur.com/VzSjR.png
same code, same fixed size (300 * 300), but Qt5.3 cant display completely.
you can download my Demo code at: here
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MyModel *model = new MyModel;
MyDelegate *delegate = new MyDelegate;
MyTableView *view = new MyTableView(this);
view->setModel(model);
view->setItemDelegate(delegate);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(view);
ui->widget->setLayout(layout);
ui->widget->setStyleSheet("background-color:red;");
ui->widget->setFixedSize(300, 300);
}
MainWindow::~MainWindow()
{
delete ui;
}
mydelegate.cpp:
MyDelegate::MyDelegate(QWidget *parent)
: QItemDelegate(parent)
{
}
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
painter->fillRect(option.rect, Qt::gray);
painter->setPen(Qt::black);
painter->drawText(option.rect, Qt::AlignCenter, index.data().toString());
painter->restore();
}
mymodel.cpp:
MyModel::MyModel()
{
}
int MyModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 6;
}
int MyModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 7;
}
QVariant MyModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::TextAlignmentRole)
return Qt::AlignCenter;
if (role == Qt::DisplayRole)
return QString::number(index.row()) + " - " + QString::number(index.column());
return QVariant();
}
mytableview.cpp:
MyTableView::MyTableView(QWidget *parent)
: QTableView(parent)
{
setTabKeyNavigation(false);
setShowGrid(false);
verticalHeader()->setVisible(false);
horizontalHeader()->setVisible(true);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setSelectionBehavior(QAbstractItemView::SelectItems);
setSelectionMode(SingleSelection);
setFrameStyle(QFrame::NoFrame);
horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
horizontalHeader()->setSectionsClickable(false);
verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
verticalHeader()->setSectionsClickable(false);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
thanks a lot !

base class 'QAbstractListModel' has private copy constructor

I have a QT QML project. (still very small)
I started by binding a listview on my UScenario model, by subclassing QAbstractListModel and it worked fined.
Now, each UScenario has a list of UTask, which also have a list of UCondition (so, Utask also subclasses QAbstractListModel). But then, QT Creator gives me an error:
Core/Tasks/utask.h:6: erreur : base class 'QAbstractListModel' has private copy constructor
class UTask: public QAbstractListModel
^
So I'm not sure where is my problem. I tried reading the doc about QAbstractListModel vs QAbstractItemModel, but I have no clue.
I also tried to see if I ever constructed a UTask in a wrong way; I think not.
// USCENARIO.h
#ifndef USCENARIO_H
#define USCENARIO_H
#include <QAbstractListModel>
#include "../Tasks/utask.h"
class UScenario : public QAbstractListModel
{
Q_OBJECT
public slots:
void cppSlot() { // Used to test the insertion from UI
this->addTask(UTask());
}
public:
enum TaskRoles {
IdRole = Qt::UserRole + 1
};
UScenario(QObject *parent = 0);
private:
QList<UTask> m_tasks;
public:
void addTask(const UTask &task);
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role) const;
virtual QHash<int, QByteArray> roleNames() const;
};
#endif // USCENARIO_H
// USCENARIO.CPP
#include "uscenario.h"
UScenario::UScenario(QObject *parent)
: QAbstractListModel(parent)
{
}
void UScenario::addTask(const UTask &task)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_tasks.append(task);
endInsertRows();
}
int UScenario::rowCount(const QModelIndex & parent) const {
return m_tasks.count();
}
QVariant UScenario::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= m_tasks.count())
return QVariant();
const UTask &task = m_tasks[index.row()];
if (role == IdRole)
return task.id();
return QVariant();
}
QHash<int, QByteArray> UScenario::roleNames() const {
QHash<int, QByteArray> roles;
roles[IdRole] = "id";
return roles;
}
// UTASK.H
#ifndef UTASK_H
#define UTASK_H
#include <QAbstractListModel>
#include "../Conditions/ucondition.h"
class UTask: public QAbstractListModel
{
Q_OBJECT
public:
enum TaskRoles {
typeRole = Qt::UserRole + 1
};
UTask(QObject *parent = 0);//:m_id(0){}
int id() const{return m_id;}
private:
int m_id;
QList<UCondition> m_conditions;
// QAbstractItemModel interface
public:
void addCondition(const UCondition &cond);
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role) const;
virtual QHash<int, QByteArray> roleNames() const;
};
#endif // UTASK_H
// UTASK.cpp
#include "utask.h"
UTask::UTask(QObject *parent):
QAbstractListModel(parent), m_id(0)
{
}
void UTask::addCondition(const UCondition &cond)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_conditions.append(cond);
endInsertRows();
}
int UTask::rowCount(const QModelIndex &parent) const
{
return m_conditions.count();
}
QVariant UTask::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_conditions.count())
return QVariant();
const UCondition &cond = m_conditions[index.row()];
if (role == typeRole)
return cond.type();
return QVariant();
}
QHash<int, QByteArray> UTask::roleNames() const
{
QHash<int, QByteArray> roles;
roles[typeRole] = "type";
return roles;
}
// MAIN
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include "../uCtrlCore/Scenario/uscenario.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
UScenario scenarioModel;
scenarioModel.addTask(UTask());
scenarioModel.addTask(UTask());
scenarioModel.addTask(UTask());
QtQuick2ApplicationViewer viewer;
QQmlContext *ctxt = viewer.rootContext();
ctxt->setContextProperty("myScenarioModel", &scenarioModel);
viewer.setMainQmlFile(QStringLiteral("qml/uCtrlDesktopQml/main.qml"));
QObject *item = viewer.rootObject()->findChild<QObject*>("btn");
QObject::connect(item, SIGNAL(qmlSignal()), &scenarioModel, SLOT(cppSlot()));
viewer.showExpanded();
return app.exec();
}
There problem is with how you're storing the UTask objects in your UScenario class
QList<UTask> m_tasks
In simple terms when you call m_tasks.append it is attempting to allocate a new UTask object in the QList by copying the source UTask object via the default copy constructor. In the case of QAbstractListModel it is private. This is why you're at getting the error.
A simply solution is to change the storage type to a list of UTask pointers, QList< UTask* > along with the supporting code to properly release the memory when your UScenario object is destroyed.
For example here are some but not all of the changes but should point you in the right direction. Just make sure to change m_tasks to QList< UTask* > first:
int main(int argc, char *argv[])
{
...
UScenario scenarioModel;
scenarioModel.addTask( new UTask() );
scenarioModel.addTask( new UTask() );
scenarioModel.addTask( new UTask() );
...
return app.exec();
}
void UScenario::cppSlot()
{
// Used to test the insertion from UI
this->addTask( new UTask() );
}
// Change the signature to take a pointer
void UScenario::addTask( UTask* task )
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_tasks.append(task);
endInsertRows();
}
// Make sure you define a destructor for UScenario
UScenario::~UScenario()
{
QList< UTask* >::iterator task = m_tasks.begin();
while( m_tasks.end() != task )
{
// Release the memory associated with the task.
delete (*task);
++task;
}
m_tasks.clear();
}

Resources