Help: New to model/view programming
-
In my app I want to have to list that each row will contain a mimeData. I want all the functionalities: insert row, remove row, move row, contextMenu etc etc
In a lazy day I would just use QTableWidget with only one column, hide the header and keep going as I got pretty comfortable using it. But I know that is not the ideal solution as I will very likely need to provide different views in the future.
I know that the way to go is to use model/view, but I just don't know how to start.
I read some tutorial but each one is different from the other so it got confused pretty fast .
I know that the first thing I need to write is a model, but I don't know where to start. What are the methods that I need to re-implement? How they work?I also know there is a QListWidget, but I want to use the QListView because in all the open-source Qt code I am going through model/view programming is being used everywhere and I am often find myself clueless about what is going on. So I need to learn it.
Thank you.
-
Hi,
No need to complicate things. You can start with a QStandardItemModel.
Did you already went through Qt's own Model View documentation ? And the corresponding tutorial ?
-
You only really need to write your model if you already have data in the memory and do not want to duplicate it. Otherwise you can re-use existing one. SGaist pointed where to look for information how to do it. it covers what do you need to do for editable/readonly models, etc.
-
@SGaist said in Help: New to model/view programming:
Did you already went through Qt's own Model View documentation ? And the corresponding tutorial ?
I did, I understood the examples given, more or less. When I read the code I can understand them but the problem is that I am not being able to create my own.
I find that I learn best by practice but I just can't seem to take what it's in the tutorial and in the doc and be able to apply to my current problem. I need some kind of kick-off that targets the very problem I have. Some very basic stuff that needs to go in my model to get me started. -
So by mimeData do you mean a list of QMimeData objects ?
If not, what are these mimeData objects ?Next question: what do you want to show on your QTableView ?
-
I recommend chapters 3 to 6 of Advanced Qt Programming: Creating Great Software with C++ and Qt by Mark Summerfield (you can easily find a pdf version online). They go through practical examples to explain the framwork
-
So by mimeData do you mean a list of QMimeData objects ?
If not, what are these mimeData objects ?Next question: what do you want to show on your QTableView ?
@SGaist said in Help: New to model/view programming:
If not, what are these mimeData objects ?
Actually I want to have QImage and text inserted into the row of the QListView. I had another idea about QMimeData but I went through its doc and realized I was wrong.
Next question: what do you want to show on your QTableView ?
Only text and Image for now.
-
@SGaist said in Help: New to model/view programming:
If not, what are these mimeData objects ?
Actually I want to have QImage and text inserted into the row of the QListView. I had another idea about QMimeData but I went through its doc and realized I was wrong.
Next question: what do you want to show on your QTableView ?
Only text and Image for now.
@hbatalha said in Help: New to model/view programming:
Only text and Image for now.
To get you going. Perhaps you want to use a
QStandardItemModelin aQListView? That would allow for an item which has text plus an icon to show against it, assuming icon is what you want for your image(?)auto model = new QStandardItemModel(this); ui->listView->setModel(model); model->appendRow(new QStandardItem(QIcon(":/icon.png"), "Test")); -
@hbatalha said in Help: New to model/view programming:
Only text and Image for now.
To get you going. Perhaps you want to use a
QStandardItemModelin aQListView? That would allow for an item which has text plus an icon to show against it, assuming icon is what you want for your image(?)auto model = new QStandardItemModel(this); ui->listView->setModel(model); model->appendRow(new QStandardItem(QIcon(":/icon.png"), "Test")); -
@JonB Sorry for the late reply
If I can just do that so why exactly do I need to create custom models like I am seeing so many times?
-
@hbatalha
Do you want to walk before you run? :)From what you describe it sounds like it would suffice. You want a custom model for efficiency and when your data deserves it.
-
You have an existing data structure that you want to make accessible to views. Using a custom model allows for example to not have several copies of your data, or add pre-processing to put in a pre-built model.