Implementing your own tree-like QT representation. The model inheritance as it should be from the QABstractItemModel class. I put 4 columns in the model, overrides the QABSTRACTIMMODEL class method
int musicmodel :: columncount (const qmodelindex & amp; parent) const {
Return 4;
}
DATA method redefined as follows:
qvariant MusicModel :: Data (Const Qmodelindex & amp; index, int role) const {
// if (Role == Qt :: DisplayRole) {
if (index.isvalid ()) {
int column = index.column ();
Item * Item = Static_Cast & LT; Item * & GT; (index.internalPointer ());
Switch (column) {
Case 0:
Return Item- & GT; Name ();
Case 1:
if (Item- & gt; Tosong ()) Return Item- & gt; Tosong () - & gt; time ();
Break;
Case 2:
if (Item- & gt; Tosong ()) Return Item- & gt; Tosong () - & gt; Rating ();
Break;
Case 3:
qdebug () & lt; & lt; Qtime :: CurrateTime (). Tostring ("HH: MM: SS") & lt; & lt; Item- & gt; Name () & lt; & lt; column;
if (Item- & gt; tosong ()) Return Item- & gt; tosong () - & gt; name ();
if (Item- & gt; toartist ()) Return Item- & gt; name ();
Break;
}
}
Return qvariant ();
//}
}
So far I do not focus on the roles, displaying for all roles equally.
Hierarchy looks like this:
0 level M_ROOT
1 level artist
2 levels album
3 level Song
Faced a problem when I try to display 3 levels. He simply does not appear if I am trying to interact with the 4th column in the DATA method. In other words, if you remove the line if (Item- & gt; Tosong ()) Return Item- & gt; Tosong () - & gt; name ();
or add 5th column (columncount will return 5). Only then the 3rd nesting level will be able to open in the presentation.
Now the situation looks like this:
As you can notice, there are 2 artists: “Name” and “Second_A”. There are also 4 albums at the first artist (“M2”, “M3”, “M4”, “4th”). But I can’t reveal the album “M2” to see the nested songs. Thus, it is not possible to edit the last column of the model …
WINDING QUESTION: Why at the last level of nesting I can’t edit the last element?
Adjust the project code below. Thanks in advance for the help!
musicmodel.h:
# ifndef musicmodel_h
#Define MusicModel_h.
#Include & lt; QabstractItemModel & gt;
#Include & lt; item.h & gt;
Class MusicModel: Public QabstractItemModel
{
Item * M_ROot;
Public:
MusicModel ();
MusicModel (Item * _M_ROOT) {M_ROOT = _M_ROOT;};
Qmodelindex index (int row, int column, const qmodelindex & amp; parent = qmodelindex ()) const;
QModelindex Parent (Const Qmodelindex & Amp; index) Const;
INT ROWCOUNT (const qmodelindex & amp; parent = qmodelindex ()) const;
INT COLUMNCOUNT (const Qmodelindex & amp; Parent) Const;
QVariant Data (Const Qmodelindex & Amp; Index, Int Role = Qt :: DisplayRole) Const;
Qt :: Itemflags Flags (Const Qmodelindex & Amp; index) Const;
BOOL SETDATA (Const Qmodelindex & Amp; Index, Const Qvariant & Amp; Value, Int Role = Qt :: EDITROLE);
BOOL INSERTROWS (INT ROW, INT COUNT, CONST QMODELINDEX & AMP; PARENT = QModelindex ());
BOOL REMOVEROWS (INT ROW, INT COUNT, CONST QMODELINDEX & AMP; PARENT = QModelindex ());
};
#endif // MusicModel_h.
musicmodel.cpp:
# include "musicmodel.h"
#Include & lt; Qdebug & gt;
MusicModel :: Musicmodel ()
{
M_ROOT = NEW ITEM;
}
Qmodelindex MusicModel :: Index (Int Row, Int Column, Const Qmodelindex & Amp; Parent) Const
{
Item * ParentItem = M_ROot;
if (parent.isvalid ())
parentitem = static_cast & lt; item * & gt; (parent.internalpointer ());
If (Parentitem- & GT; Childat (ROW))
RETURN CREATEINDEX (ROW, COLUMN, PARENTITEM- & GT; CHILDAT (ROW));
RETURN QMODELINDEX ();
}
Qmodelindex MusicModel :: Parent (Const Qmodelindex & Amp; index) const {
if (! index.isvalid ()) RETURN QMODELINDEX ();
Item * Childem = Static_Cast & LT; Item * & GT; (index.internalPointer ());
Item * Parent = ChildItem- & gt; parent ();
if ((Parent == M_ROOT) ||! (Parent)) Return qmodelindex ();
Item * GrandParent = Parent- & GT; PARENT ();
RETURN CREATEINDEX (Grandparent- & GT; Indexof (Parent), 0, Parent);
}
Int MusicModel :: Rowcount (Const Qmodelindex & Amp; Parent) Const {
Item * ParentItem = M_ROot;
if (parent.isvalid ())
parentitem = static_cast & lt; item * & gt; (parent.internalpointer ());
RETURN PARENTITEM- & GT; childcount ();
}
Int MusicModel :: ColumnCount (Const Qmodelindex & Amp; Parent) Const {
Return 4;
}
Qvariant MusicModel :: Data (Const Qmodelindex & Amp; Index, Int Role) Const {
// if (Role == Qt :: DisplayRole) {
if (index.isvalid ()) {
int column = index.column ();
Item * Item = Static_Cast & LT; Item * & GT; (index.internalPointer ());
Switch (column) {
Case 0:
Return Item- & GT; Name ();
Case 1:
if (Item- & gt; Tosong ()) Return Item- & gt; Tosong () - & gt; time ();
Break;
Case 2:
if (Item- & gt; Tosong ()) Return Item- & gt; Tosong () - & gt; Rating ();
Break;
Case 3:
qdebug () & lt; & lt; Qtime :: CurrateTime (). Tostring ("HH: MM: SS") & lt; & lt; Item- & gt; Name () & lt; & lt; column;
if (Item- & gt; tosong ()) Return Item- & gt; tosong () - & gt; name ();
if (Item- & gt; toartist ()) Return Item- & gt; name ();
Break;
}
}
Return qvariant ();
//}
// ELSE If (Role == Qt :: Editrole) {
//}
}
Qt :: Itemflags MusicModel :: Flags (Const Qmodelindex & Amp; index) const {
RETURN QABSTRACTIMMODEL :: Flags (index) | Qt :: Itemiseditable;
}
BOOL MUSICMODEL :: SETDATA (Const Qmodelindex & Amp; Index, Const Qvariant & Amp; Value, Int Role) {
}
BOOL MUSICMODEL :: INSERTROWS (INT ROW, INT COUNT, CONST QMODELINDEX & AMP; PARENT) {
}
BOOL MUSICMODEL :: REMOVEROWS (INT ROW, INT COUNT, CONST QMODELINDEX & AMP; PARENT) {
}
item.h:
# ifndef item_h
#Define Item_h.
#include & lt; QLIST & GT;
#Include & lt; Qpixmap & gt;
#Include & lt; Qtime & gt;
#Include & LT; QDataStream & GT;
class artist;
class album;
Class Song;
Class Item {
Public:
Item ();
Item (Const Qstring & Amp;);
~ Item ();
Item * Parent () Const;
Void SetParent (Item *);
Void Insertchild (Item *, int position = -1);
Item * Takechild (int);
Item * Childat (int) const;
INDEXOF (Item *) Const;
int childcount () const;
QString Name () Const {Return M_NAME; }
Void SetName (Const Qstring & amp; _Name) {M_NAME = _NAME; }
Virtual Artist * toartist () {Return 0; }
Virtual album * toalbum () {Return 0; }
Virtual Song * Tosong () {Return 0; }
Private:
Item * M_Parent;
QLIST & LT; Item * & GT; M_CHILDREN;
Qstring M_NAME;
Qstring M_COMMENT;
};
Class ARTIST: Public Item {
QPIXMAP _Picture;
QSTRING _COUNTRY;
Qstring _comment;
Public:
ARTIST (): Item () {}
Artist (Const Qstring & amp; n): Item (N) {}
Const Qpixmap Picture () Const {Return _PICTURE; }
Void setPicture (Const Qpixmap & amp; _p) {_picture = _p; }
Const Qstring Country () Const {Return _Country; }
Void SetCountry (Const Qstring & amp; _c) {_country = _c; }
const qstring comment () const {return _comment; }
Void SetComment (Const Qstring & amp; _C) {_comment = _c; }
Artist * toartist () {Return this; }
Album * toalbum () {Return 0; }
SONG * TOSONG () {Return 0; }
};
Class Album: Public Item {
int _year;
QPIXMAP _Picture;
Qstring _Genre;
Qstring _comment;
Public:
Album (): item () {}
Album (Const Qstring & amp; n): Item (N) {}
int year () const {return _year; }
void setyear (int _y) {_year = _y; }
Const Qpixmap Picture () Const {Return _PICTURE; }
void setPicture (const QPixmap & amp; _p) {_picture = _p; }
const QString genre () const {return _genre; }
void setGenre (const QString & amp; _g) {_genre = _g; }
const QString comment () const {return _comment; }
void setComment (const QString & amp; _c) {_comment = _c; }
Artist * toArtist () {return 0; }
Album * toAlbum () {return this; }
Song * toSong () {return 0; }
};
class Song: public Item {
QTime _time;
int _rating;
QString _comment;
public:
Song (): Item () {}
Song (const QString & amp; n): Item (n) {}
const QTime time () const {return _time; }
void setTime (const QTime & amp; _t) {_time = _t; }
int rating () const {return _rating; }
void setRating (int _r) {_rating = _r; }
QString comment () const {return _comment; }
void setComment (const QString & amp; _c) {_comment = _c; }
Artist * toArtist () {return 0; }
Album * toAlbum () {return 0; }
Song * toSong () {return this; }
};
QDataStream & amp; operator & lt; & lt; (QDataStream & amp;, const Artist & amp;);
QDataStream & amp; operator & lt; & lt; (QDataStream & amp ;, const Album & amp;);
QDataStream & amp; operator & lt; & lt; (QDataStream & amp ;, const Song & amp;);
QDataStream & amp; operator & gt; & gt; (QDataStream & amp ;, Artist & amp;);
QDataStream & amp; operator & gt; & gt; (QDataStream & amp ;, Album & amp;);
QDataStream & amp; operator & gt; & gt; (QDataStream & amp ;, Song & amp;);
#endif // ITEM_H
Item.cpp:
# include "item.h"
#include & lt; QDebug & gt;
Item :: Item () {
m_parent = 0;
m_name = "Nameless";
}
Item :: Item (const QString & amp; name) {
m_parent = 0;
m_name = name;
}
Item :: ~ Item () {
qDebug () & lt; & lt; "delete" & lt; & lt; name ();
int count = childCount ();
if (m_parent) m_parent- & gt; m_children.takeAt (m_parent- & gt; indexOf (this));
for (int i = 0; i & lt; count; i ++) delete childAt (0);
}
void Item :: insertChild (Item * child, int position) {
if (position & lt; 0) position = childCount ();
if (! m_children.contains (child)) m_children.insert (position, child);
if (child- & gt; parent ()! = this) child- & gt; setParent (this);
}
Item * Item :: takeChild (int position) {
Item * child = m_children.takeAt (position);
child- & gt; setParent (0);
return child;
}
void Item :: setParent (Item * _parent) {
if (_parent) {
if (m_parent) {
m_parent- & gt; takeChild (m_parent- & gt; indexOf (this));
}
m_parent = _parent;
m_parent- & gt; insertChild (this);
}
m_parent = _parent;
}
Item * Item :: parent () const {
return m_parent;
}
Item * Item :: childAt (int position) const {
if (position & lt; 0 || position & gt; = childCount ()) return nullptr;
return m_children.at (position);
}
int Item :: indexOf (Item * el) const {
return m_children.indexOf (el);
}
int Item :: childCount () const {
return m_children.size ();
}
QDataStream & amp; operator & lt; & lt; (QDataStream & amp; stream, const Artist & amp; artist) {
stream & lt; & lt; artist.name () & lt; & lt; artist.picture () & lt; & lt; artist.country ()
& lt; & lt; artist.comment ();
// serialize children
int cnt = artist.childCount ();
stream & lt; & lt; cnt;
for (int i = 0; i & lt; cnt; ++ i) {
Album * album = artist.childAt (i) - & gt; toAlbum ();
if (album) stream & lt; & lt; * album;
}
return stream;
}
QDataStream & amp; operator & lt; & lt; (QDataStream & amp; stream, const Album & amp; album) {
stream & lt; & lt; album.name () & lt; & lt; album.year () & lt; & lt; album.picture ()
& lt; & lt; album.genre () & lt; & lt; album.comment ();
int cnt = album.childCount ();
stream & lt; & lt; cnt;
for (int i = 0; i & lt; cnt; i ++) {
Song * song = album.childAt (i) - & gt; toSong ();
if (song) stream & lt; & lt; * song;
}
return stream;
}
QDataStream & amp; operator & lt; & lt; (QDataStream & amp; stream, const Song & amp; song) {
stream & lt; & lt; song.name () & lt; & lt; song.time () & lt; & lt; song.rating ()
& lt; & lt; song.comment ();
return stream;
}
QDataStream & amp; operator & gt; & gt; (QDataStream & amp; stream, Artist & amp; artist) {
QString name;
QPixmap picture;
QString country;
QString comment;
Stream & gt; & gt; Name & gt; & gt; Picture & gt; & gt; Country & gt; & gt; Comment;
Artist.SetName (Name);
Artist.SetPicture (Picture);
Artist.Setcountry (Country);
Artist.SetComment (Comment);
INT CNT;
Stream & gt; & gt; CNT;
Album * album;
For (int i = 0; i & lt; CNT; I ++) {
album = new album;
Stream & gt; & gt; * album;
artist.insertchild (ALBUM);
}
Return Stream;
}
QDataStream & amp; Operator & gt; & gt; (QDataStream & amp; Stream, Album & amp; album) {
QString Name;
INT YEAR;
QPIXMAP PICTURE;
QString Genre;
QString Comment;
Stream & gt; & gt; Name & gt; & gt; Year & gt; & gt; Picture & gt; & gt; Genre & gt; & gt; Comment;
album.setname (name);
album.setyear (year);
Album.SetPicture (Picture);
Album.Setgenre (Genre);
album.setcomment (comment);
INT CNT;
Stream & gt; & gt; CNT;
SONG * SONG;
For (int i = 0; i & lt; CNT; I ++) {
Song = New Song;
Stream & gt; & gt; * Song;
album.insertchild (SONG);
}
Return Stream;
}
QDataStream & amp; Operator & GT; & gt; (QDataStream & amp; Stream, Song & Amp; Song) {
QString Name;
Qtime Time;
int rating;
QString Comment;
Stream & gt; & gt; Name & gt; & gt; Time & gt; & gt; Rating & gt; & gt; Comment;
Song.SetName (Name);
Song.Settime (Time);
Song.Setrating (Rating);
Song.SetComment (Comment);
Return Stream;
}
main.cpp:
# include "mainwindow.h"
#include & lt; QLIST & GT;
#Include & lt; Qapplication & gt;
#Include & lt; Qdebug & gt;
#Include & lt; item.h & gt;
#Include & LT; QDataStream & GT;
#include & lt; qfiledialog & gt;
#Include & lt; QtreeView & GT;
#Include & lt; musicmodel.h & gt;
Int Main (int argc, char * argv [])
{
Qapplication A (ARGC, ARGV);
// Mainwindow W;
// w.show ();
// QLIST & LT; int & gt; L;
// L.APPEND (1);
// L.APPEND (2);
// L.Insert (-10, 0);
// qdebug () & lt; & lt; L.Takeat (0);
// qdebug () & lt; & lt; L.Takeat (0);
// qdebug () & lt; & lt; L.INDEXOF (1);
Item * _m1 = new artist;
ARTIST * M1 = _M1- & gt; toartist ();
M1- & GT; SetComment ("Comment0");
M1- & GT; SetCountry ("Russia");
M1- & gt; setPicture (QPIXMAP ());
M1- & gt; SetName ("Name");
Item * _m2 = new album ("m2");
Album * m2 = _m2- & gt; toalbum ();
M2- & GT; SetComment ("Comment1");
M2- & GT; SETYEAR (1993);
M2- & GT; SETGENRE ("ROCK");
// QString Name = QFileDialog :: GetOpenFileName (0, "Open", QDIR :: CURRENTPATH ());
// qdebug () & lt; & lt; Name;
// M2- & GT; SetPicture (QPIXMAP (Name));
M2- & GT; SETPICTURE (QPIXMAP ("C: / Users / Public / Pictures / Sample Pictures / Chrysanthemum.jpg"));
Album * m3 = new album ("m3");
Album * m4 = new album ("m4");
Item * _m5 = New Song ("M5");
SONG * M5 = _M5- & GT; TOSONG ();
M5- & gt; settime (qtime ());
M5- & gt; Setrating (4);
M5- & GT; SetComment ("Comment2");
SONG * M6 = NEW SONG ("M6");
M1- & GT; INSERTCHILD (M2);
// M1- & GT; INSERTCHILD (M4);
M4- & GT; SetParent (M1);
M2- & GT; INSERTCHILD (M5);
M4- & GT; INSERTCHILD (M6);
M1- & GT; INSERTCHILD (M3, 1);
// M6- & GT; SetParent (M3);
qdebug () & lt; & lt; M1- & gt; childcount ();
// qdebug () & lt; & lt; M1- & gt; Childat (1) - & gt; name ();
// Delete M4;
// qdebug () & lt; & lt; M3- & GT; PARENT () & lt; & lt; M3 & LT; & LT; M3- & GT; ChildCount () & lt; & lt; M3- & GT; NAME ();
// Item * Cur;
// qdebug () & lt; & lt; M1- & gt; Childat (0) - & gt; name ();
// CUR = M1- & GT; TAKECHILD (0);
// qdebug () & lt; & lt; CUR- & GT; NAME () & LT; & LT; CUR- & GT; PARENT () & lt; & lt; "CUR";
// qdebug () & lt; & lt; M1- & gt; indexof (M4) & lt; & lt; "index";
qdebug () & lt; & lt; M1- & GT; Childat (0) - & gt; name () & lt; & lt; M1- & GT; Childat (0) - & gt; toalbum () & lt; & lt; M1- & gt; comment () & lt; & lt; M1- & GT; Country () & lt; & lt; M1- & GT; Picture ();
qdebug () & lt; & lt; M2- & gt; year () & lt; & lt; M2- & gt; comment () & lt; & lt; M2- & gt; Picture () & lt; & lt; M2- & gt; genre ();
qdebug () & lt; & lt; M1- & GT; PARENT () & lt; & lt; M1 & LT; & LT; M1- & GT; ChildCount () & lt; & lt; M1- & GT; NAME ();
qdebug () & lt; & lt; M2- & GT; PARENT () - & GT; NAME () & LT; & LT; M2 & LT; & LT; M2- & gt; childcount () & lt; & lt; M2- & GT; NAME ();
qdebug () & lt; & lt; M3- & GT; PARENT () - & gt; name () & lt; & lt; M3 & LT; & LT; M3- & GT; ChildCount () & lt; & lt; M3- & GT; NAME ();
qdebug () & lt; & lt; M4- & GT; PARENT () - & gt; name () & lt; & lt; M4 & LT; & LT; M4- & gt; childcount () & lt; & lt; M4- & GT; Name ();
qdebug () & lt; & lt; M5- & GT; PARENT () - & gt; name () & lt; & lt; M5 & LT; & LT; M5- & gt; childcount () & lt; & lt; M5- & GT; NAME ()
& lt; & lt; M5- & GT; TIME () & LT; & LT; M5- & gt; Rating () & lt; & lt; M5- & gt; comment ();
qdebug () & lt; & lt; (M5 == nullPTR);
qdebug () & lt; & lt; M6- & GT; PARENT () - & gt; name () & lt; & lt; M6 & LT; & LT; M6- & GT; Childcount () & lt; & lt; M6- & GT; NAME ();
qdebug () & lt; & lt; M1- & gt; Childat (1) - & gt; name ();
Qfile file ("d: /qt/code/music/new.txt");
QDataStream Stream (& amp; File);
If (File.open (Qiodevice :: WriteOnly)) {
Stream & lt; & lt; * M1;
}
file.close ();
Artist * artist = new artist;
if (File.open (Qiodevice :: Readonly)) {
Stream & gt; & gt; * artist;
}
qdebug () & lt; & lt; "Deserialization";
qdebug () & lt; & lt; artist- & gt; childcount ();
qdebug () & lt; & lt; Artist- & GT; Childat (0) - & gt; name () & lt; & lt; Artist- & gt; Childat (0) - & gt; toalbum ()
& lt; & lt; Artist- & gt; comment () & lt; & lt; Artist- & GT; Country () & lt; & lt; Artist- & gt; Picture ();
Album * album_1;
album_1 = artist- & gt; Childat (0) - & gt; toalbum ();
qdebug () & lt; & lt; album_1- & gt; yar () & lt; & lt; album_1- & gt; comment ()
& lt; & lt; Album_1- & gt; Picture () & lt; & lt; album_1- & gt; genre ();
qdebug () & lt; & lt; Artist- & gt; parent () & lt; & lt; Artist & lt; & lt; Artist- & gt; childcount () & lt; & lt; Artist- & gt; name ();
qdebug () & lt; & lt; album_1- & gt; parent () - & gt; name () & lt; & lt; album_1
& lt; & lt; album_1- & gt; childcount () & lt; & lt; album_1- & gt; name ();
Album * album_2;
album_2 = artist- & gt; childat (1) - & gt; toalbum ();
qdebug () & lt; & lt; album_2- & gt; parent () - & gt; name () & lt; & lt; album_2.
& lt; & lt; album_2- & gt; childcount () & lt; & lt; album_2- & gt; name ();
Album * album_3;
album_3 = artist- & gt; Childat (2) - & gt; toalbum ();
qdebug () & lt; & lt; album_3- & gt; parent () - & gt; name () & lt; & lt; album_3.
& lt; & lt; Album_3- & gt; childcount () & lt; & lt; album_3- & gt; name ();
SONG * SONG_1, * SONG_2;
Song_1 = Album_1- & gt; Childat (0) - & gt; tosong ();
qdebug () & lt; & lt; Song_1- & GT; Parent () - & gt; name () & lt; & lt; Song_1 & LT; & LT; Song_1- & GT; ChildCount ()
& lt; & lt; SONG_1- & GT; NAME () & lt; & lt; Song_1- & GT; Time () & lt; & lt; SONG_1- & GT; Rating ()
& lt; & lt; Song_1- & gt; comment ();
qdebug () & lt; & lt; (SONG_1 == NULLPTR);
Song_2 = Album_3- & gt; Childat (0) - & gt; tosong ();
qdebug () & lt; & lt; Song_2- & gt; parent () - & gt; name () & lt; & lt; Song_2.
& lt; & lt; Song_2- & gt; Childcount () & lt; & lt; Song_2- & GT; Name ();
qdebug () & lt; & lt; Artist- & gt; Childat (1) - & gt; name ();
Item * root = new item;
root- & gt; insertchild (artist);
ARTIST * artist2 = new artist;
artist2- & gt; setname ("second_a");
Artist2- & GT; SetParent (Root);
MusicModel * Model = New MusicModel (root);
Qmodelindex index_a1 = model- & gt; index (0,0, qmodelindex ());
Qmodelindex index_a2 = model- & gt; index (1.0, qmodelindex ());
Album * album4 = new album ("4th");
Artist- & GT; InsertChild (Album4);
qdebug () & lt; & lt; "Artist 1" & lt; & lt; Static_Cast & LT; Item * & gt; ((index_a1) .internalPointer ()) - & gt; name ()
& lt; & lt; "Parent" & lt; & lt; Static_Cast & Lt; Item * & GT; ((Model & GT; Parent (index_a1)). INTERNALPOINTER ())
& lt; & lt; "Rows" & lt; & lt; Model- & gt; rowcount (index_a1) & lt; & lt; "Columns" & lt; & lt; Model- & gt; columncount (index_a1);
qdebug () & lt; & lt; "Artist 2" & lt; & lt; Static_Cast & LT; Item * & GT; ((index_a2) .internalPointer ()) - & gt; name ()
& lt; & lt; "Rows" & lt; & lt; Model- & gt; rowcount (index_a2) & lt; & lt; "Columns" & lt; & lt; Model- & gt; columncount (index_a2);
Qmodelindex index_a_1_1 = model- & gt; index (0, 0, index_a1);
qdebug () & lt; & lt; "Album 1" & lt; & lt; static_cast & lt; item * & gt; ((index_a_1_1) .internalPointer ()) - & gt; name ()
& lt; & lt; "Parent" & lt; & lt; static_cast & lt; item * & gt; ((Model- & gt; parent (index_a_1_1)). INTERNALPOINTER ()) - & GT; NAME ()
& lt; & lt; "Rows" & lt; & lt; Model- & gt; rowcount (index_a_1_1) & lt; & lt; "Columns" & lt; & lt; Model- & gt; columncount (index_a_1_1);
Qmodelindex index_a_1_2 = model- & gt; index (1, 0, index_a1);
qdebug () & lt; & lt; "Album 2" & lt; & lt; Static_Cast & LT; Item * & GT; ((index_a_1_2) .internalPointer ()) - & gt; name ()
& lt; & lt; "Rows" & lt; & lt; Model- & gt; rowcount (index_a_1_2) & lt; & lt; "Columns" & lt; & lt; Model- & gt; columncount (index_a_1_2);
Qmodelindex index_a_1_3 = model- & gt; index (2, 0, index_a1);
qdebug () & lt; & lt; "Album 3" & lt; & lt; static_cast & lt; item * & gt; ((index_a_1_3) .internalPointer ()) - & gt; name ()
& lt; & lt; "Rows" & lt; & lt; Model- & gt; rowcount (index_a_1_3) & lt; & lt; "Columns" & lt; & lt; Model- & gt; columncount (index_a_1_3);
Qmodelindex index_a_2_1 = model- & gt; index (0, 0, index_a_1_1);
qdebug () & lt; & lt; "Song 1" & lt; & lt; Static_Cast & LT; Item * & gt; ((index_a_2_1) .internalPointer ()) - & gt; name ()
& lt; & lt; "Parent" & lt; & lt; Static_Cast & Lt; Item * & gt; ((Model- & gt; parent (index_a_2_1)). INTERNALPOINTER ()) - & GT; NAME ()
& lt; & lt; "Rows" & lt; & lt; Model- & gt; rowcount (index_a_2_1) & lt; & lt; "Columns" & lt; & lt; Model- & gt; columncount (index_a_2_1);
QtreeView * Tree = New QtreeView;
Tree- & gt; SetModel (Model);
Tree- & gt; show ();
/ *
Qfile file ("d: /qt/code/music/new.txt");
QDataStream Stream (& amp; File);
if (File.Open (Qiodevice :: WriteOnly | Qiodevice :: Append)) {
Stream & lt; & lt; Qstring ("ads") & lt; & lt; 123 & lt; & lt; QString ("Last");
if (stream.status ()! = QDataStream :: OK) {
qdebug () & lt; & lt; "Error recording.";
}
}
file.close ();
QString First, Last, Another;
INT MIDDLE;
if (File.open (Qiodevice :: Readonly)) {
Stream & gt; & gt; FIRST & GT; & GT; Middle & gt; & gt; Last & gt; & gt; Another;
// Stream & GT; & GT; Middle;
// Stream & GT; & GT; Last;
// Stream & GT; & GT; Another;
}
qdebug () & lt; & lt; FIRST & LT; & LT; Middle & lt; & lt; Last & lt; & lt; Another;
file.close ();
* /
Return a.exec ();
}
Answer 1
everything turned out to be quite simple. It was necessary to leave only the role of Qt :: DisplayRole
and it is possible QT :: EDITROLE
in the DATA function. This solves my problem with the inability to deploy a tree at all levels.