Detailed info in QTreeView Model
Hi, I have a problem with creating a program based on
Basic Sort/Filter Model Example , but including also detailed view of each record like Master Detail Example. I can make normal Model from first example (I read data from txt file as QStringList separated by "_"), but I don't know how to include to each line in table additional info (e.g. in a form of QStringList) which will appear after activating record.
Here you have my code. I shortened it so there could be some logic gaps but I think this would be enough to show general idea of my program
eglowny.h
Code:
#ifndef EGLOWNY_H
#define EGLOWNY_H
#include <QMainWindow>
#include <QAbstractItemModel>
#include <QTreeView>
{
Q_OBJECT
public:
explicit Eglowny
(QWidget *parent
= 0);
private:
void wnetrzeokna();
SortFilterProxyModel* filtr;
private slots:
};
#endif // EGLOWNY_H
eglowny.cpp
Code:
#include "eglowny.h"
Eglowny
::Eglowny(QWidget *parent
) :{
wnetrzeokna();
setWindowTitle("Kolekcjoner");
resize(800, 600);
}
void Eglowny::wnetrzeokna()
{
filtr->setDynamicSortFilter(true);
lista->setRootIsDecorated(false);
lista->setAlternatingRowColors(true);
lista->setModel(filtr);
lista->setSortingEnabled(true);
uklad->addWidget(lista, 0, 0, 1, 3);
wyg->setLayout(uklad);
lista->sortByColumn(1,Qt::AscendingOrder);
setCentralWidget(wyg);
}
void Eglowny::save ()
{
if (file.isEmpty())
return;
saveb (file);
}
void Eglowny::upload()
{
if (file.isEmpty())
return;
zrodlo(asd(wyg, file));
}
{
filtr->setSourceModel(model);
}
baza.h
Code:
#ifndef BAZA_H
#define BAZA_H
#include <QApplication>
#include <QList>
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QMessageBox>
#include <QSet>
#include <QAbstractItemModel>
#include <QStandardItemModel>
QList<QString> ListaPodstawowychAtrybutow;
void wypiszpodstawoweatrybuty();
#endif // BAZA_H
baza.cpp
Code:
#include "baza.h"
{
if (!plikzdanymi.
open(QFile::WriteOnly|
QFile::Text)) {
////
return;
};
while (model->rowCount()>0)
{
QList<QStandardItem*>asd= model->takeRow(0);
{
wpislinia.append("_"+lista->text());
}
plikdocelowydane<<wpislinia;
endl(plikdocelowydane);
asd.clear();
wpislinia.clear();
}
plikzdanymi.close();
}
{
{
/////
return NULL;
};
typ.clear();
atrybut.clear();
model->setHeaderData(0, Qt::Horizontal, "0");
wypiszpodstawoweatrybuty();
int i=1;
foreach
(QString podstawowyatrybut, ListaPodstawowychAtrybutow
) {
model->setHeaderData(i, Qt::Horizontal, podstawowyatrybut);
i++;
};
if (!plikzdanymi.
open(QFile::ReadOnly|
QFile::Text)) {
////////////
return;
};
QString wpislinia
= plikdane.
readLine();
while (!wpislinia.isNull())
{
dodajwpis(model,wpislista[0],wpislista[1],wpislista[2],wpislista[3]);
wpislinia = plikdane.readLine();
}
;
plikzdanymi.close();
return model;
}
{
model->insertRow(0);
model->setData(model->index(0,0),wpist);
model->setData(model->index(0,1),wpisn);
model->setData(model->index(0,2),wpisz);
model->setData(model->index(0,3),wpisk);
}
void wypiszpodstawoweatrybuty()
{
ListaPodstawowychAtrybutow.clear();
ListaPodstawowychAtrybutow <<"1"<<"2"<<"3";
}
Re: Detailed info in QTreeView Model
If i can remember correct you can use setData on the QStandardItem using the Qt::UserRole, using this role you can add you're own type of data (an object for example)
example:
Code:
itm->setData("Test1",Qt::DisplayRole);
itm->setData("Test2",Qt::EditRole);
itm->setData("Test3",Qt::UserRole);
qDebug() << "display " << itm->data(Qt::DisplayRole); //debug result is: display QVariant(QString, "Test1")
qDebug() << "edit " << itm->data(Qt::EditRole); //debug result is: edit QVariant(QString, "Test2")
qDebug() << "user " << itm->data(Qt::UserRole); //debug result is: user QVariant(QString, "Test3")
you can create an object and put it into the variant
Re: Detailed info in QTreeView Model
It sounds good, but unfortunatelly I haven't done anything like this before. Could you tell me step by step what should I do?
Re: Detailed info in QTreeView Model
Then i will need some aditional information
is the detailed info also stored in the file?
the most important question is what you exatly want to create (can you make a sketch of how it should look?)
do you need a tree or a list view?
1 Attachment(s)
Re: Detailed info in QTreeView Model
My problem rather doesn't need use tree model (but datailed info could be also treated as child) so I think list could be enough. I want to create list of items with few standard informations showed in table (header 1, 2 and 3- all QString), and few additional details showed while item is chose (QStrings and e.g Picture- items could have different number of additional informations it depends on their type represented by header1). List must be sortable by column and must be filtered. I think that's all. At this moment using TreeView I get almost all facilities except includind detail informations.
I store items informations in txt files in form:
item1header1_item1header2_item1header3_item1info1_ item1info2_item1info3
item2header1_item2header2_item2header3_item2info1_ item1info5
item3header1_item3header2_item3header3_item3info1_ item3info2_item3info3
and general info about types:
header1a_header2_header3_info1_info2_info3
header1b_header2_header3_info1_info5
I firstly upload second file with headers for every type( this fragment of code isn't included in code which I posted) and in next step file with informations for every item, and depending on header1 I know which attributes should have chosen item. eg when header1a= item1header1 I know that this item should 6 informations about it (header1,header2,header3,info1,info2,info3)
Attachment 8137
1 Attachment(s)
Re: Detailed info in QTreeView Model
I've made a small example, i did not include the sorting and filtering for that you can take a look at the zipcode1 example from http://www.qtrac.eu/aqpbook.html (aqpbook.zip)
(the code is not that lean and clean but you will get the idea)
see the attachment
Attachment 8138