Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to connect QTableView to local file
Qt 6.11 is out! See what's new in the release blog

How to connect QTableView to local file

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Gojir4G Gojir4

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

    tovaxT Offline
    tovaxT Offline
    tovax
    wrote on last edited by
    #5

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

    1 Reply Last reply
    0
    • Gojir4G Gojir4

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

      tovaxT Offline
      tovaxT Offline
      tovax
      wrote on last edited by
      #6

      @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.

      tovaxT Gojir4G 2 Replies Last reply
      0
      • Gojir4G Gojir4

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

        tovaxT Offline
        tovaxT Offline
        tovax
        wrote on last edited by
        #7

        @Gojir4

        1. save data in real time;
        2. easy to insert or remove rows (column count is constant)
        3. easy to modify cell data
        1 Reply Last reply
        0
        • tovaxT tovax

          @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.

          tovaxT Offline
          tovaxT Offline
          tovax
          wrote on last edited by
          #8

          @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.

          1 Reply Last reply
          0
          • tovaxT tovax

            @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.

            Gojir4G Offline
            Gojir4G Offline
            Gojir4
            wrote on last edited by
            #9

            @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...

            tovaxT 1 Reply Last reply
            1
            • Gojir4G Gojir4

              @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...

              tovaxT Offline
              tovaxT Offline
              tovax
              wrote on last edited by
              #10

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

              JonBJ 1 Reply Last reply
              0
              • tovaxT tovax

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #11

                @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.

                tovaxT 1 Reply Last reply
                3
                • JonBJ JonB

                  @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.

                  tovaxT Offline
                  tovaxT Offline
                  tovax
                  wrote on last edited by
                  #12

                  @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?
                  		}
                  
                  tovaxT 1 Reply Last reply
                  0
                  • tovaxT tovax

                    @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?
                    		}
                    
                    tovaxT Offline
                    tovaxT Offline
                    tovax
                    wrote on last edited by tovax
                    #13

                    @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);
                    
                    1 Reply Last reply
                    0
                    • tovaxT tovax

                      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!

                      tovaxT Offline
                      tovaxT Offline
                      tovax
                      wrote on last edited by
                      #14

                      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!

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved