Hello , thanks for the breaking up the steps
Below is what i have tried
model.h
#include <QAbstractListModel>
#include <QStringList>
//![0]
class list
{
public:
//![0]
//type title size id animal list
private:
//![1]
};
{
Q_OBJECT
public:
enum listroles {
titlerole = Qt::UserRole + 1,
idrole
};
void addpages(const list &list);
protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<list> m_list;
};
#include <QAbstractListModel>
#include <QStringList>
//![0]
class list
{
public:
list(const QString &title, const QString &id);
//![0]
//type title size id animal list
QString title() const;
QString id() const;
private:
QString m_title;
QString m_id;
//![1]
};
class listmodel : public QAbstractListModel
{
Q_OBJECT
public:
enum listroles {
titlerole = Qt::UserRole + 1,
idrole
};
listmodel(QObject *parent = 0);
void addpages(const list &list);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<list> m_list;
};
To copy to clipboard, switch view to plain text mode
model.cpp
#include "model.h"
: m_title(title), m_id(id)
{
}
{
return m_title;
}
{
return m_id;
}
listmodel
::listmodel(QObject *parent
){
}
void listmodel::addpages(const list &list)
{
beginInsertRows
(QModelIndex(), rowCount
(), rowCount
());
m_list << list;
endInsertRows();
}
int listmodel
::rowCount(const QModelIndex & parent
) const { Q_UNUSED(parent);
return m_list.count();
}
if (index.row() < 0 || index.row() >= m_list.count())
const list &list = m_list[index.row()];
if (role == titlerole)
return list.title();
else if (role == idrole)
return list.id();
}
//![0]
QHash<int, QByteArray> listmodel::roleNames() const {
QHash<int, QByteArray> roles;
roles[titlerole] = "title";
roles[idrole] = "id";
return roles;
}
//![0]
#include "model.h"
list::list(const QString &title, const QString &id)
: m_title(title), m_id(id)
{
}
QString list::title() const
{
return m_title;
}
QString list::id() const
{
return m_id;
}
listmodel::listmodel(QObject *parent)
: QAbstractListModel(parent)
{
}
void listmodel::addpages(const list &list)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_list << list;
endInsertRows();
}
int listmodel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return m_list.count();
}
QVariant listmodel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= m_list.count())
return QVariant();
const list &list = m_list[index.row()];
if (role == titlerole)
return list.title();
else if (role == idrole)
return list.id();
return QVariant();
}
//![0]
QHash<int, QByteArray> listmodel::roleNames() const {
QHash<int, QByteArray> roles;
roles[titlerole] = "title";
roles[idrole] = "id";
return roles;
}
//![0]
To copy to clipboard, switch view to plain text mode
main.cpp snippet
listmodel mod;
if(!db.open())
{
qDebug() <<"error in opening DB";
}
else
{
qDebug() <<"connected to DB" ;
}
QVector<QString> page_id;
QVector<QString> rev_id;
QSqlQuery quer
("SELECT page_ID , page_revision FROM Pages");
while (quer.next()) {
QString i
= quer.
value(0).
toString();
page_id.push_back(i);
QString r
= quer.
value(1).
toString();
rev_id.push_back(r);
}
for (int i = 0; i < page_id.size(); ++i)
{
qDebug() << page_id.at(i).toLocal8Bit().constData() << endl;
qDebug() << rev_id.at(i).toLocal8Bit().constData() << endl;
mod.addpages(list(page_id.at(i) , rev_id.at(i)));
}
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", &mod);
listmodel mod;
if(!db.open())
{
qDebug() <<"error in opening DB";
}
else
{
qDebug() <<"connected to DB" ;
}
QVector<QString> page_id;
QVector<QString> rev_id;
QSqlQuery quer("SELECT page_ID , page_revision FROM Pages");
while (quer.next()) {
QString i = quer.value(0).toString();
page_id.push_back(i);
QString r = quer.value(1).toString();
rev_id.push_back(r);
}
for (int i = 0; i < page_id.size(); ++i)
{
qDebug() << page_id.at(i).toLocal8Bit().constData() << endl;
qDebug() << rev_id.at(i).toLocal8Bit().constData() << endl;
mod.addpages(list(page_id.at(i) , rev_id.at(i)));
}
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", &mod);
To copy to clipboard, switch view to plain text mode
qml
import QtQuick 2.6
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.0
import Qt.labs.folderlistmodel 2.1
Pane{
ListView {
width: 200; height: 250
model: myModel
delegate: Text { text: "pages: " + title + ", " + id }
}
}
import QtQuick 2.6
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.0
import Qt.labs.folderlistmodel 2.1
Pane{
ListView {
width: 200; height: 250
model: myModel
delegate: Text { text: "pages: " + title + ", " + id }
}
}
To copy to clipboard, switch view to plain text mode
OUTPUT :
1.png
As you can see it's not even close to my original view : https://s32.postimg.org/vrsdrl2p1/man.png
now i need to add buttons to it but how ?
Bookmarks