Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: custom QAbstractTableModel class updating QTableView

  1. #21
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,141
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: custom QAbstractTableModel class updating QTableView

    But I reckon I'm missing the replacing part in my code!?
    Yes, I see you are calling begin / endResetModel(). You are missing the QFileSystemWatcher part to trigger the reload.

    Your code to parse the CSV file looks overly complex. Why don't you simply read the text stream line-by-line into a QString using QTextStream::readLine() (instead of character-by-character), and then use QString::split() with ';' as the split character, and behavior set to KeepEmptyParts? You'd then get a QStringList for every row in the table and could replace about 20 lines of parsing code with 4.

    Qt Code:
    1. while ( !in.atEnd() )
    2. {
    3. QString line = in.readLine();
    4. QStringList fields = line.split( ';', KeepEmptyParts );
    5.  
    6. // appendToCSVMatrix( fields );
    7. }
    To copy to clipboard, switch view to plain text mode 

    In fact, if all you are doing is reading a CSV file and displaying the fields in a table view, you can dispense with using a QAbstractTableModel entirely and simply use QTableWidget as is and insert the fields as QTableWidgetItem instances. Let your MainWindow handle watching the log file and reloading the table when needed.
    Last edited by d_stranz; 5th August 2016 at 19:10.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #22
    Join Date
    May 2016
    Posts
    13

    Default Re: custom QAbstractTableModel class updating QTableView

    Omg... I finally did it (with a lot of help)!
    Added a QFileSystemWatcher to my dialog and connected it to a slot which reloads the model.

    I'll give the QTableWidget a shot as well. Maybe that's even better. But I needed to figure this one out first because that bothered me for too long now.

    Thank you so much d_stranz and anda_skoa!!! Thumbs up for the both of you.

    The thread can be marked as solved!
    Cheers
    Last edited by MichaH; 5th August 2016 at 19:33.

  3. #23
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: custom QAbstractTableModel class updating QTableView

    Quote Originally Posted by MichaH View Post
    I'll give the QTableWidget a shot as well. Maybe that's even better.
    Since you already have a model I would stay with that approach.
    The work is already done and you could now easily combine it with a filter proxy model, etc.

    Cheers,
    _

  4. #24
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,141
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: custom QAbstractTableModel class updating QTableView

    Since you already have a model I would stay with that approach.
    I agree. @MichaH - it's been a good learning experience. Had you started with QTableWidget, wouldn't have learned anything about writing a custom model and interfacing it to external data.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QAbstractTableModel, QTableView and restoreState.
    By cydside in forum Qt Programming
    Replies: 0
    Last Post: 10th August 2014, 11:41
  2. Replies: 0
    Last Post: 5th April 2011, 16:51
  3. Custom role in custom QAbstractTableModel
    By hailflex in forum Newbie
    Replies: 3
    Last Post: 10th December 2009, 12:09
  4. Performance with QTableView and QAbstractTableModel
    By MBex in forum Qt Programming
    Replies: 3
    Last Post: 25th February 2009, 08:04
  5. Replies: 3
    Last Post: 29th January 2009, 08:38

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.