Results 1 to 6 of 6

Thread: Detailed info in QTreeView Model

  1. #1
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    10

    Default 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

    Qt Code:
    1. #ifndef EGLOWNY_H
    2. #define EGLOWNY_H
    3. #include <QMainWindow>
    4. #include <QAbstractItemModel>
    5. #include <QTreeView>
    6.  
    7.  
    8. extern QAbstractItemModel *asd(QWidget *parent, QString file);
    9.  
    10. class Eglowny : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit Eglowny(QWidget *parent = 0);
    16.  
    17. private:
    18. void wnetrzeokna();
    19. QGridLayout *uklad;
    20. QWidget *wyg;
    21. QTreeView *lista;
    22. SortFilterProxyModel* filtr;
    23.  
    24. private slots:
    25. void zrodlo(QAbstractItemModel *model);
    26. };
    27. #endif // EGLOWNY_H
    To copy to clipboard, switch view to plain text mode 

    eglowny.cpp

    Qt Code:
    1. #include "eglowny.h"
    2. Eglowny::Eglowny(QWidget *parent) :
    3. QMainWindow(parent)
    4. {
    5. wnetrzeokna();
    6. setWindowTitle("Kolekcjoner");
    7. resize(800, 600);
    8. }
    9. void Eglowny::wnetrzeokna()
    10. {
    11. filtr = new QSortFilterProxyModel;
    12. filtr->setDynamicSortFilter(true);
    13. lista = new QTreeView;
    14. lista->setRootIsDecorated(false);
    15. lista->setAlternatingRowColors(true);
    16. lista->setModel(filtr);
    17. lista->setSortingEnabled(true);
    18.  
    19. uklad = new QGridLayout;
    20. uklad->addWidget(lista, 0, 0, 1, 3);
    21.  
    22. wyg = new QWidget;
    23. wyg->setLayout(uklad);
    24. lista->sortByColumn(1,Qt::AscendingOrder);
    25.  
    26. setCentralWidget(wyg);
    27. }
    28.  
    29. void Eglowny::save ()
    30. {
    31. QString file = QFileDialog::getSaveFileName(this, tr("Save"));
    32. if (file.isEmpty())
    33. return;
    34. saveb (file);
    35. }
    36.  
    37. void Eglowny::upload()
    38. {
    39. QString file
    40. = QFileDialog::getOpenFileName(this, tr("Upload "));
    41. if (file.isEmpty())
    42. return;
    43. zrodlo(asd(wyg, file));
    44. }
    45.  
    46. void Eglowny::zrodlo(QAbstractItemModel *model)
    47. {
    48. filtr->setSourceModel(model);
    49. }
    To copy to clipboard, switch view to plain text mode 
    baza.h
    Qt Code:
    1. #ifndef BAZA_H
    2. #define BAZA_H
    3. #include <QApplication>
    4. #include <QList>
    5. #include <QFile>
    6. #include <QTextStream>
    7. #include <QStringList>
    8. #include <QMessageBox>
    9. #include <QSet>
    10. #include <QAbstractItemModel>
    11. #include <QStandardItemModel>
    12.  
    13. QList<QString> ListaPodstawowychAtrybutow;
    14. QAbstractItemModel *asd(QWidget *parent, QString file);
    15. void saveb(QString &file);
    16. void wypiszpodstawoweatrybuty();
    17. void dodajwpis(QAbstractItemModel *model, QString wpist, QString wpisn, QString wpisz, QString wpisk);
    18. #endif // BAZA_H
    To copy to clipboard, switch view to plain text mode 
    baza.cpp

    Qt Code:
    1. #include "baza.h"
    2. void saveb(QString &file)
    3. {
    4. QFile plikzdanymi(file);
    5. if (!plikzdanymi.open(QFile::WriteOnly| QFile::Text))
    6. {
    7. ////
    8. return;
    9. };
    10. QTextStream plikdocelowydane (&plikzdanymi);
    11. QString wpislinia;
    12. while (model->rowCount()>0)
    13. {
    14. QList<QStandardItem*>asd= model->takeRow(0);
    15. foreach(QStandardItem* lista,asd)
    16. {
    17. wpislinia.append("_"+lista->text());
    18. }
    19. plikdocelowydane<<wpislinia;
    20. endl(plikdocelowydane);
    21. asd.clear();
    22. wpislinia.clear();
    23. }
    24. plikzdanymi.close();
    25. }
    26.  
    27. QAbstractItemModel *asd(QWidget *parent, QString file)
    28. {
    29. QFile plik(file);
    30. if (!plik.open(QFile::ReadOnly| QFile::Text))
    31. {
    32. /////
    33. return NULL;
    34. };
    35. typ.clear();
    36. atrybut.clear();
    37. model = new QStandardItemModel(0, 4, parent);
    38. model->setHeaderData(0, Qt::Horizontal, "0");
    39. wypiszpodstawoweatrybuty();
    40. int i=1;
    41. foreach(QString podstawowyatrybut, ListaPodstawowychAtrybutow)
    42. {
    43. model->setHeaderData(i, Qt::Horizontal, podstawowyatrybut);
    44. i++;
    45. };
    46. QFile plikzdanymi(file);
    47. if (!plikzdanymi.open(QFile::ReadOnly| QFile::Text))
    48. {
    49. ////////////
    50.  
    51. return;
    52. };
    53. QTextStream plikdane (&plikzdanymi);
    54. QString wpislinia= plikdane.readLine();
    55. while (!wpislinia.isNull())
    56. {
    57. QStringList wpislista = wpislinia.split("_");
    58. dodajwpis(model,wpislista[0],wpislista[1],wpislista[2],wpislista[3]);
    59. wpislinia = plikdane.readLine();
    60. }
    61. ;
    62. plikzdanymi.close();
    63. return model;
    64. }
    65. void dodajwpis(QAbstractItemModel *model, QString wpist, QString wpisn, QString wpisz, QString wpisk)
    66. {
    67. model->insertRow(0);
    68. model->setData(model->index(0,0),wpist);
    69. model->setData(model->index(0,1),wpisn);
    70. model->setData(model->index(0,2),wpisz);
    71. model->setData(model->index(0,3),wpisk);
    72. }
    73.  
    74. void wypiszpodstawoweatrybuty()
    75. {
    76. ListaPodstawowychAtrybutow.clear();
    77. ListaPodstawowychAtrybutow <<"1"<<"2"<<"3";
    78. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. itm->setData("Test1",Qt::DisplayRole);
    2. itm->setData("Test2",Qt::EditRole);
    3. itm->setData("Test3",Qt::UserRole);
    4.  
    5. qDebug() << "display " << itm->data(Qt::DisplayRole); //debug result is: display QVariant(QString, "Test1")
    6. qDebug() << "edit " << itm->data(Qt::EditRole); //debug result is: edit QVariant(QString, "Test2")
    7. qDebug() << "user " << itm->data(Qt::UserRole); //debug result is: user QVariant(QString, "Test3")
    To copy to clipboard, switch view to plain text mode 

    you can create an object and put it into the variant
    Last edited by StrikeByte; 20th August 2012 at 12:57.

  3. The following user says thank you to StrikeByte for this useful post:

    Pawello (23rd August 2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    10

    Default 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?

  5. #4
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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?
    Last edited by StrikeByte; 20th August 2012 at 14:06.

  6. #5
    Join Date
    Jul 2012
    Posts
    25
    Thanks
    10

    Default 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)

    sample.jpg
    Last edited by Pawello; 20th August 2012 at 16:16.

  7. #6
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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
    List.zip

  8. The following user says thank you to StrikeByte for this useful post:

    Pawello (23rd August 2012)

Similar Threads

  1. Replies: 0
    Last Post: 27th June 2012, 05:46
  2. QTreeView model
    By ComaWhite in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2012, 22:56
  3. detailed explaining qwt example
    By AD in forum Qwt
    Replies: 6
    Last Post: 9th July 2008, 21:13
  4. Replies: 1
    Last Post: 4th July 2008, 17:17
  5. again Open Cascade + Qt 4.2.2 but detailed...
    By Shuchi Agrawal in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 06:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.