Re: TreeModel



Can't you use plain old loops? Something like this:

class ModelColumns : public Gtk::TreeModel::ColumnRecord
        {
                public:

                ModelColumns()
                {
                    for (int col = 0; col < 6; ++col)
                      add(m_cols[col]);
                }
                Gtk::TreeModelColumn<Glib::ustring> m_cols[6];
        }
......
              m_refTreeModel =  Gtk::ListStore::create(m_Columns);
              m_TreeView->set_model(m_refTreeModel);
              for (int col = 0; col < 6; ++col)
                m_TreeView->append_column(title[col], m_Columns.m_cols[col]);

             Gtk::TreeModel::Row rows[35];
             for (int row = 0; row < 35; ++row)
             {
               rows[row] = *(m_refTreeModel->append());

               for (int col = 0; col < 6; ++col)
                  rows[row][m_Columns.m_cols[col]] = array[row][col];
           }


Den 2017-09-19 kl. 14:35, skrev Deepak Chiradoni:
Hi,

I am currently adding data coming from CSV(Comma Separated Values) file.All the data from CSV is string only. As I have 35 rows and 6 Columns of data in string.

class ModelColumns : public Gtk::TreeModel::ColumnRecord
        {
                public:

                ModelColumns()
                {
                              add(m_colA);
                              add(m_colB);
                              add(m_colC);
                              add(m_colD);
                              add(m_colE);
                              add(m_colF);
                }
Gtk::TreeModelColumn<Glib::ustring>      m_colA;
Gtk::TreeModelColumn<Glib::ustring>      m_colB;
Gtk::TreeModelColumn<Glib::ustring>      m_colC;
Gtk::TreeModelColumn<Glib::ustring>      m_colD;
Gtk::TreeModelColumn<Glib::ustring>      m_colE;
Gtk::TreeModelColumn<Glib::ustring>      m_colF;
        };

void LoadBtn()
{
       Gtk::FileChooserDialog dialog("Please select a file to open",
       Gtk::FILE_CHOOSER_ACTION_OPEN);
       dialog.set_transient_for(*this);

        //Add response buttons the the dialog:
        dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL);
        dialog.add_button("_Open", Gtk::RESPONSE_OK);

        //Add filters, so that only certain file types can be selected:

        auto filter_text = Gtk::FileFilter::create();
        filter_text->set_name("csv files (*.csv)|*.csv");
        filter_text->add_pattern("*");
        dialog.add_filter(filter_text);

        //Show the dialog and wait for a user response:
        int result = dialog.run();

          //Handle the response:
          switch(result)
          {
            case(Gtk::RESPONSE_OK):
            {
                    std::string filename = dialog.get_filename();

                    ifstream in(filename);

                        string line, field;

                        vector< vector<Glib::ustring> > array;  // the 2D array                         vector<Glib::ustring> v;              // array of values for one line only

                        while ( getline(in,line) )    // get next line in file
                        {
                            v.clear();
                            stringstream ss(line);


                            while (getline(ss,field,','))  // break line into comma delimitted fields
                            {
                                v.push_back(field);  // add each field to the 1D array
                            }

                            array.push_back(v);  // add the 1D array to the 2D array
                        }

              m_refTreeModel =  Gtk::ListStore::create(m_Columns);
              m_TreeView->set_model(m_refTreeModel);
              m_TreeView->append_column("Data1", m_Columns.m_colA);
              m_TreeView->append_column("Data2", m_Columns.m_colB);
              m_TreeView->append_column("Data3", m_Columns.m_colC);
              m_TreeView->append_column("Data4", m_Columns.m_colD);
              m_TreeView->append_column("Data5", m_Columns.m_colE);
              m_TreeView->append_column("Data6", m_Columns.m_colF);

              Gtk::TreeModel::Row row = *(m_refTreeModel->append());
              row[m_Columns.m_colA] = array[0][0]; row[m_Columns.m_colB] = array[0][1]; row[m_Columns.m_colC] = array[0][2];               row[m_Columns.m_colD] = array[0][3]; row[m_Columns.m_colE] = array[0][4]; row[m_Columns.m_colF] = array[0][5];

              Gtk::TreeModel::Row row1 = *(m_refTreeModel->append());
              row1[m_Columns.m_colA] = array[1][0]; row1[m_Columns.m_colB] = array[1][1]; row1[m_Columns.m_colC] = array[1][2];                       row1[m_Columns.m_colD] = array[1][3]; row1[m_Columns.m_colE] = array[1][4]; row1[m_Columns.m_colF] = array[1][5];

              //35 Rows Like This

              break;
            }
            default:
            {
              break;
            }
        }

}

Like this I am adding 35 rows. Its very Lengthy. I am getting data from string vector as I have added the code for FileChoser Function. I am defining a class for TreeModel as I added.

Regrads
Deepak, India.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]