diff --git a/pyside6/qsqltablemodel.md b/pyside6/qsqltablemodel.md new file mode 100644 index 00000000..42e19449 --- /dev/null +++ b/pyside6/qsqltablemodel.md @@ -0,0 +1,78 @@ +The `QSqlTableModel` class in PySide6 (the Python bindings for the Qt framework) provides an editable data model for a single database table. It allows you to manipulate and display data from a database table in a table view. Here's a detailed overview of `QSqlTableModel` and its parameters, settings, and attributes, along with examples on how to use it in your Python program. + +### Overview of QSqlTableModel + +- **Inheritance**: `QSqlTableModel` inherits from `QAbstractTableModel`. +- **Purpose**: Provides an editable data model for a single database table. +- **Features**: Supports adding, deleting, and modifying records. + +### Key Methods and Attributes + +- **Methods**: + - `setTable(table_name)`: Sets the model to use the specified table. + - `setEditStrategy(strategy)`: Sets the edit strategy for the model. + - `setRelation(column, relation)`: Sets a relation for a specific column. + - `relationModel()`: Returns the model used for a specific relation. + - `setRelation(column, relation)`: Sets a relation for a specific column. + - `setJoinMode(mode)`: Sets the join mode for the model. + +- **Attributes**: + - `relation(column)`: Returns the relation for a specific column. + +### Example Usage + +Below is an example demonstrating how to use `QSqlTableModel` in a Python program. This example assumes you have a SQLite database with a table named `employees`. + +```python +import sys +from PySide6.QtWidgets import QApplication, QMainWindow, QTableView +from PySide6.QtSql import QSqlTableModel, QSqlDatabase + +class MainWindow(QMainWindow): + def __init__(self): + super().__init__() + + # Create a database connection + db = QSqlDatabase.addDatabase("QSQLITE") + db.setDatabaseName("employees.db") + + if not db.open(): + print("Database Error: ", db.lastError().text()) + return + + # Create a model and set the table + model = QSqlTableModel() + model.setTable("employees") + model.select() + + # Create a view and set the model + view = QTableView() + view.setModel(model) + + self.setCentralWidget(view) + self.setWindowTitle("QSqlTableModel Example") + self.resize(600, 400) + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = MainWindow() + window.show() + sys.exit(app.exec()) +``` + +### Explanation + +1. **Database Connection**: The example connects to an SQLite database named `employees.db`. +2. **Model Creation**: A `QSqlTableModel` instance is created and set to use the `employees` table. +3. **Model Selection**: The `select()` method is called to populate the model with data from the table. +4. **View Setup**: A `QTableView` is created and set to display the data from the model. +5. **Main Window**: The `MainWindow` class is instantiated and shown. + +### Additional Notes + +- **Edit Strategy**: You can set the edit strategy using `setEditStrategy()`. For example, `model.setEditStrategy(QSqlTableModel.OnFieldChange)` will allow editing directly on the field change. +- **Relations**: You can set relations using `setRelation()`, which is useful for displaying foreign key relationships in a user interface. + +### Conclusion + +`QSqlTableModel` is a powerful class for managing and displaying data from a single database table in a PySide6 application. By understanding its methods and attributes, you can create versatile and interactive user interfaces for your database-driven applications.