Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to connect QTableView to local file



  • Hi, all
    I created a new table model inherited from QAbstractTableModel, to connect QTableView to local file.
    I used QSettings INI format for the local file, and read/write/display correctly.
    But I don't know how to implement insertRows and removeRows, maybe the INI file format is not appropriate.
    I can't find similar examples, could you give me any suggestion please?
    This is my demo code: JCDemoSheet
    Best regards!



  • XML format?



  • @tovax The format is not really important, that's up to you to define which one is more appropriate for the type of your data. That being said, IMHO ini file are more appropriate for storing settings than data. So you can use CSV, XML, JSON, SQL or any of this kind of format, or even your custom format using plaintext. Note you can also use binary files alternatively.

    I saw in your code that you are opening the ini file each time data() is called ni your model. This is probably a bad idea because it will constantly accessing the disk. Instead you could read data from the file only once and store it's content in memory, like in a QList or QVector, a custom class or anything else. Then you can return data from there in model data() method

    Can you explain a little bit what you are trying to achieve ?



  • @Gojir4 Thank you very much for reply, I have achieved store contents in QList, but the data must be saved realtime, so I guess the data should be stored in file directly.



  • @Gojir4 INI format is very difficult to insert or remove some rows.



  • @Gojir4 The file is used for CNC(Computer numerical control). In order to prevent sudden power failure, the data needs to be saved to the hard disk in real time.



  • @Gojir4

    1. save data in real time;
    2. easy to insert or remove rows (column count is constant)
    3. easy to modify cell data


  • @tovax said in How to connect QTableView to local file:

    @Gojir4 The file is used for CNC(Computer numerical control). In order to prevent sudden power failure, the data needs to be saved to the hard disk in real time.

    I'm not sure that's correct.



  • @tovax OK, maybe a database would by a better solution in this case. But anyway you can save you content in file when you data have changed from the model, but you don't need to read them.

    The easiest way to insert content in a file is to replace completely with new content. but it depends also of the size of your file...



  • @Gojir4 Thanks, It's very helpful to me. I'll reconsider the implementation.



  • @tovax
    Just a couple of points, re-iterating the sort of thing @Gojir4 is saying.

    A .ini file is not an ideal candidate for treating as made of rows & columns, because it really isn't. You would, for example, have to implement your own "mapping" from row numbers to the keys (and vice versa), because they don't have any ordering. If you did implement that, insert/removeRows() would then be implementable. And it won't work at all if you want, say, the [Sections] which .ini file allows for.

    If ever you put a model on top of an external "text file", then unlike for an actual row-based database if you want to make any kind of change you must rewrite the whole of the file when saving, you cannot actually do any insert/update/deletes on actual lines in the file.

    If you wish to manage settings for your own app --- as opposed to edit existing, non-Qt .ini files --- you would be best doing that through QSettings, not dealing with the text file yourself.



  • @JonB Thank you very much for your reply, I got it. Based on your and @Gojir4 's suggestions, I have decided to use the database to solve this problem.
    I have done the mapping between QSqlTableModel and QTableView, but I have several databases. I don't know how to update tableview after changing the database. The simple code is as follows:

    	/* 1: Database file */
    		D:/xyz-1.db, D:/xyz-2.db, D:/xyz-3.db
    	
    	/* 2: Defines */
    		QSqlDatabase    database;
            QSqlTableModel  *tableModel = nullptr;
            QTableView      *tableView = nullptr;
    		
    	/* 3: Database and TableModel */
    		database = QSqlDatabase::addDatabase("QSQLITE", "connectionTemp");
    		database.setHostName("www.qt.io");
    		database.setDatabaseName("D:/xyz-1.db"); // xyz-1.db
    		database.setUserName("qter");
    		database.setPassword("123456");
    		if (database.open()) {
    			tableModel = new QSqlTableModel(this, database);
    			tableModel->setTable("MyTable");
    			tableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    			tableModel->select();
    		} else {
    			qDebug() << __FUNCTION__ << QString("Error!");
    			return;
    		}
    		
    	/* 4: TableView */
    		tableView = new QTableView(this);
    		tableView->setModel(tableModel);
    		
    	/* 5: Change database on pushbutton clicked */
    		void onPushbuttonClicked(void)
    		{
    			database.setDatabaseName("D:/xyz-2.db"); // xyz-2.db
    			// How to update TableModel and TableView please?
    		}
    


  • @tovax said in How to connect QTableView to local file:

    How to update TableModel and TableView:

    Step1: close database

    database.close();
    

    Step2: set database name

    database.setDatabaseName(dbName);
    if (database.open() == false) {
    
    }
    

    Step3: clear tableView's model

    tableView->setModel(nullptr);
    

    Step4: recreate tableModel

    tableModel = new QSqlTableModel(this, database);
    tableModel->setTable("MyTable");
    tableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
    tableModel->select();
    

    Step5: set tableView's model

    tableView->setModel(tableModel);
    


  • Thanks very much for the reply of @Gojir4 and @JonB . It's very useful to me. The above is my solution, which may not be the best way, but it can solve the problems I currently encounter. I hope it can help friends who encounter similar problems.
    Best regards!


Log in to reply