Qt (QAbstractItemModel usage for the QTableView) / QTableView da QAbstractItemModeldan qo'llash (1)

Hello everyone Today I am going to explain about Qt Model By making a small project we will see how it works. Let’s start First Create a GUI application press the create button and press the next button Now I am going to add QTableView item to the QMainWindow Then show some information on the table As I mentioned previous lesson In order to use the model There are Item Views (Based-Model) It means that these items work with model Now I am going to show you how to display information on the QTableView If you press the “ok” button the tableView should display some information Clean and Build the project Do not pay attention this error as I explained already about this in my previous lesson If you restart the MVS then everything will be okay Now I am going to create a model name the model here press the “Add” button Let’s give the QAbstractItemModel to Base Class Now we have created our abstract model This abstract model includes 5 different methods and we must use all of them As you know abstract class, at least one virtual function must be equal to zero In C# case the abstract class is created with abstract keyword No we have to add those 5 methods to our class As you can see here I am adding abstract classes’ methods Let’s make a default constructor

and create constructor with column count Now I am going to implement the source file Let’s create our model If you do not use abstract class method then you can not create your model The MVS 2019 notes this error but in MVS 2010 version you cannot see this error while you are creating your class The error may occur here After opening commit that error has disappeared Let’s implement the source file The purpose of QModelIndex is to give you the view of row and column

As you know model saves data In order to save data in model we need a QVector Here row represent QVector and column represent QList One column may include multiple rows We can use here Q_UNUSED in order to avoid any error of compiler You can use QAssistant if you want to know about the class or something else The Purpose of index method is that Qt creates index of model based on this class

We have to check our row and column whether valid or not Here “data” method rule is to display our information on the view First of all we check index whether is valid or not There are several roles to customize our model For instance “DisplayRole”, “DecorationRole” and so on If role is “DisplayRole” then data displays on view and If role is “BackgroundRole” then it paints your view Let’s get reference because we have to carry about the memory As you know C++ has deep and shallow copy Now we are making shallow copy We are reading the data which related to column

Now our “DisplayRole” is ready It reads a list which matches each row then displays for each column Now we have to set our model to the tableView Now we can use this model anywhere instead of using tableview Now we need a slot for the button Now we need to add some data to our model In order to add data to model we need an “append” method In order to display data we need a “reset” method

As you see here our data did not display because we did not call reset function of our model When we add data to the model then we need to reset it It gave an error because in our model we forgot to return “data” method Now our data has displayed In this view column and row headers named automatically but we can modify them if we want Next lesson I will explain about this Thank you I hope you liked this video