Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: How to populate a QTableView using data in a .txt file

  1. #1
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default How to populate a QTableView using data in a .txt file

    I am new to Qt, I am trying to populate a QTableView using data entered into a .txt file. It would be great if I can see an example of the same.
    The viewer should basically be able to see all the data in the file in columns and rows in the table accordingly.
    Ex- Col1, Col2 with 5 rows of data corresponding to each of the columns.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to populate a QTableView using data in a .txt file

    Read the data from your file line by line, parse into the individual columns, then you will use that data to populate your model. You can use QStandardItemModel or create your own subclass of QAbstractTableModel.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Thanks, but I am having trouble understanding how to use QstandardItem model, because I am parsing the lines in the file, but unable to decide how best to use QStandardItem model. Forgive me if it is basic, but I am new to Qt
    while(!file.atEnd()){
    line=file.readLine();
    line_data=line.split(";", QString::SkipEmptyParts);
    QStandardItem item;
    }

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to populate a QTableView using data in a .txt file

    For each item in line_data you create a QStandardItem on the heap (using "new") and put it into a QList<QStandardItem*>.
    Then you append this list to the QStandardItemModel using appendRow().

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    newtoQ_s (22nd September 2015)

  6. #5
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Thanks,
    But while trying to add the QstandardItemModel to the QTreeWidget
    I am getting an error, because the setModel() function is private.
    QStandardItemModel model(10,3);
    while(!file.atEnd()){
    line=file.readLine();
    line_data=line.split(";", QString::SkipEmptyParts);
    for(int j=0;j<line_data.size();j++){
    QString m_prop=line_data.at(j);
    QStandardItem *item=new QStandardItem(m_prop);
    model.setItem(line_count,j,item);
    }
    line_count++;
    }
    }
    ui->tableWidget->setModel(model);

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to populate a QTableView using data in a .txt file

    Quote Originally Posted by newtoQ_s View Post
    Thanks,
    But while trying to add the QstandardItemModel to the QTreeWidget
    I am getting an error, because the setModel() function is private.
    No, it is not.

    Quote Originally Posted by newtoQ_s View Post
    Qt Code:
    1. QStandardItemModel model(10,3);
    To copy to clipboard, switch view to plain text mode 
    That stack allocated instance will go out of scope as soon as the current method ends.

    Quote Originally Posted by newtoQ_s View Post
    Qt Code:
    1. ui->tableWidget->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    setModel() takes a pointer to a model, not a copy of a model.

    Cheers,
    _

  8. #7
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Thanks, but I am unable to understand as to how I can correct this error.

    QStandardItemModel model(10,3);

    int line_count=0;
    if(file.open(QIODevice::ReadOnly)){
    QString line;
    QStringList line_data;

    while(!file.atEnd()){
    line=file.readLine();
    line_data=line.split(";", QString::SkipEmptyParts);
    for(int j=0;j<line_data.size();j++){
    QString m_prop=line_data.at(j);
    QStandardItem *item=new QStandardItem(m_prop);
    model.setItem(line_count,j,item);
    }
    line_count++;
    }
    }
    ui->tableWidget->setModel(&model);

    I am sure it must be something I am overlooking. Its not in the main() method, I am using a separate method.

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to populate a QTableView using data in a .txt file

    Quote Originally Posted by newtoQ_s View Post
    Thanks, but I am unable to understand as to how I can correct this error.
    You allocate the model on the heap instead.

    A stack allocated object's live time ends at the end of the block scope it was created in.

    Cheers,
    _

  10. #9
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Hi
    That's true, but when I tried doing that I am getting an error.

    Cannot convert from QStandardItemModel ** to QAbstractItemModel*.
    Change:
    QStandardItemModel model=new QStandardItemModel(10,3, this);

    Thanks


    Added after 4 minutes:


    Correction
    QStandardItemModel *model=new QStandardItemModel(10,3,this);

    If I do the following; The exe stops working giving an error

    if(file.open(QIODevice::ReadOnly)){
    QString line;
    QStringList line_data;

    while(!file.atEnd()){
    line=file.readLine();
    line_data=line.split(";", QString::SkipEmptyParts);
    for(int j=0;j<line_data.size();j++){
    QString m_prop=line_data.at(j);
    QTableWidgetItem *item=new QTableWidgetItem(m_prop);
    ui->tableWidget->setItem(line_count,j,item);
    }
    line_count++;

    }
    }


    Added after 1 14 minutes:


    why do I need to create 3 different data structures just to simply acces strings from a file, parse them and display them onto a Table?

    I am unfortunately still unable to find a solution to this.
    Last edited by newtoQ_s; 22nd September 2015 at 12:29.

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to populate a QTableView using data in a .txt file

    Quote Originally Posted by newtoQ_s View Post
    If I do the following; The exe stops working giving an error
    Qt Code:
    1. if(file.open(QIODevice::ReadOnly)){
    2. QString line;
    3. QStringList line_data;
    4.  
    5. while(!file.atEnd()){
    6. line=file.readLine();
    7. line_data=line.split(";", QString::SkipEmptyParts);
    8. for(int j=0;j<line_data.size();j++){
    9. QString m_prop=line_data.at(j);
    10. ui->tableWidget->setItem(line_count,j,item);
    11. }
    12. line_count++;
    13.  
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    What kind of error?
    Does it crash? When yes, where?
    Is line_count initialized properly?

    Otherwise this looks quite ok.

    Cheers,
    _

  12. #11
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    The following is the code that I have used in the function, I have also seen the Qt documentation and numerous examples and have tried them, but none to avail. I am in a bit of a fix because of this.

    void MainWindow::init()
    {

    QFile file("address1.txt");
    if(!file.exists()){
    return;
    }
    int line_count=0;
    QString line;
    QStringList line_data;

    if(file.open(QIODevice::ReadOnly)){
    while(!file.atEnd()){
    line=file.readLine();
    line_data=line.split(";", QString::SkipEmptyParts);
    for(int j=0;j<line_data.size();j++){
    QString m_prop=line_data.at(j);
    QTableWidgetItem *item=new QTableWidgetItem(m_prop);
    ui->tableWidget->setItem(line_count,j,item);
    }
    line_count++;
    }
    }
    qDebug()<<line_count;
    }

    The Message I get is Applicationname.exe has stopped working, would you like to debug, restart or quit.

  13. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to populate a QTableView using data in a .txt file

    Quote Originally Posted by newtoQ_s View Post
    The Message I get is Applicationname.exe has stopped working, would you like to debug, restart or quit.
    So you have debugged it and found the line where it crashes?

    Cheers,
    _

  14. #13
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Unfortunately the debug doesn't work in the Qt creator and even if it did, I am yet to learn how to do it.
    But is the code fine?

  15. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to populate a QTableView using data in a .txt file

    Yes, looks ok.
    You have verified that the "ui" pointer has been initialized properly?

    Cheers,
    _

  16. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to populate a QTableView using data in a .txt file

    ui->tableWidget->setItem(line_count,j,item);
    If you have not initialized the table widget to the total number of rows and columns before you start adding items to it, it will blow up. Because your code mixes up reading the file with adding to the table, you have no way to determine in advance what the total row count will be.

    If you had followed the advice given earlier and created a QStandardItemModel instance on the heap (using "new"), then you could read each row, create a QList< QStandardItem * > for the columns in that row (as also advised), and call QStandardItemModel::appendRow() to dynamically build your model as you read the file.

    After you have read the file, you can then use a QTableView (not Widget) and call its setModel() method to tell it to use your model as its data source.

    If you want to do it the way you are trying to, you will have to read the file once to count the number of rows and columns, set the table widget dimensions, then rewind and read the file again, this time creating the QTableWidgetItem instances to fill the cells.

  17. #16
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Hi,

    I have set the number of rows and columns for the tableWidget in the Qt designer itself. I am able to only see empty rows and columns with the column titles and row numbers without the data that I actually would like to see in the table.
    Could I use table view to set the model to Table Widget?
    If I morph the table widget to TableView, the designer doesn't have the option of defining the number of rows and columns, how do I do that? In the program?

    In the method that I am using, I have read the file only once, I am simultaneously trying to populate the table with each line that is read by parsing the values read to each of the columns. The file already consists of the data and doesn't exceed the table widget's capacity.

    Hi,

    I have not done anything to the ui pointer because I believe that the Qt creator takes care of the initialization part. I simply use the pointer to access different elements on the screen.

  18. #17
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to populate a QTableView using data in a .txt file

    If I morph the table widget to TableView, the designer doesn't have the option of defining the number of rows and columns, how do I do that? In the program?
    The table view uses the model to determine how many rows and columns there are. There are no setRowCount() or setColumnCount() methods for QTableView for that reason. When the model tells the table view it needs to update (because the model has changed), the table view asks the model for the number of rows and columns and uses that to configure itself.

    You want to do something like this:
    Qt Code:
    1. // Not real C++ code...
    2. void MainWindow::init()
    3. {
    4. // open the file, etc.
    5.  
    6. // create the model
    7. QStandardItemModel * pModel = new QStandardItemModel( this );
    8.  
    9. for each line in the file
    10. {
    11. QList< QStandardItem * > items;
    12. parse the line into fields
    13. for each field
    14. {
    15. QStandardItem * pItem = new QStandardItem( field );
    16. items.push_back( pItem );
    17. }
    18. pModel->appendRow( items );
    19. }
    20.  
    21. // close the file, etc.
    22.  
    23. // set the model on the table VIEW
    24. ui->tableView->setMode( pModel );
    25. }
    To copy to clipboard, switch view to plain text mode 

  19. The following user says thank you to d_stranz for this useful post:

    newtoQ_s (23rd September 2015)

  20. #18
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Thanks,
    I shall try the above, as I am new, I am not sure about converting the TableWidget to TableView.


    Added after 1 12 minutes:


    Thanks a lot,
    Your suggestion has helped me finally see the output on the table, the application not running was because I was calling it before the ui could be setup.
    Just wondering as to why one needs to create 2 data structures i..e QList and QStandardItemModel in addition to the QStandardItem in order to populate the screen with String values?
    Last edited by newtoQ_s; 23rd September 2015 at 06:34.

  21. #19
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to populate a QTableView using data in a .txt file

    Quote Originally Posted by newtoQ_s View Post
    Your suggestion has helped me finally see the output on the table, the application not running was because I was calling it before the ui could be setup.


    Quote Originally Posted by newtoQ_s View Post
    Just wondering as to why one needs to create 2 data structures i..e QList and QStandardItemModel in addition to the QStandardItem in order to populate the screen with String values?
    You are effectively using one data structure, the QStandardItemModel.
    It somehow needs to be told what data is should provide to the view, so you need API to put data into the model.

    Since a node can hold various values (text, font, color, etc), each node is represented by an object, in this case an object of type QStandardItemModel.
    So this is not something extra, it is part of the QStandardItemModel API, just making it more convenient to use than a bunch of setters and getters that always need to location of the cell as well as the data.

    For simple views this is easier with the item widgets, say QTableWidget, since their cell value objects to not need to cover the whole range of options (e.g. QTableWidget only deals with tables, QStandardItemModel needs to be capable for holding tree data).

    Ultimately writing your own model is often the least complex solution when dealing with lists or tables (deriving from QAbstractListModel or QAbstractTableModel respectively).

    Cheers,
    _

  22. #20
    Join Date
    Sep 2015
    Posts
    25
    Thanks
    5

    Default Re: How to populate a QTableView using data in a .txt file

    Hi,

    Thanks for the explanation. I am not sure I understood the entire reasoning. Another question that came to mind was:-
    How do I understand which of the variables n the stack I need to explicitly delete using delete and which of them I need not delete.
    In C++ the general rule to to make sure that a variable allocated in the heap is deleted and set to Null to avoid dangling pointers and memory loss.
    What is the general thumb rule in Qt? Because I am not able to get my head around it, and its imperative that I understand it.

Similar Threads

  1. Replies: 0
    Last Post: 28th January 2013, 09:14
  2. how to populate data in QTableWidget
    By gauravg in forum Qt-based Software
    Replies: 1
    Last Post: 25th March 2011, 12:50
  3. Replies: 3
    Last Post: 1st February 2011, 11:57
  4. How to populate Combobox in QTableView
    By ronak.vashi in forum Newbie
    Replies: 8
    Last Post: 4th December 2010, 17:09
  5. How to populate delegate with model data.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 09:31

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.