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. Help: New to model/view programming

Help: New to model/view programming

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 1.0k Views 2 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • A Offline
        A Offline
        AlexMaly
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        2
        • H Offline
          H Offline
          hbatalha
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            H 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              3
              • SGaistS SGaist

                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 ?

                H Offline
                H Offline
                hbatalha
                wrote on last edited by
                #7

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

                JonBJ 1 Reply Last reply
                0
                • H hbatalha

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

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

                  @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 QStandardItemModel in a QListView? 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"));
                  
                  H 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @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 QStandardItemModel in a QListView? 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"));
                    
                    H Offline
                    H Offline
                    hbatalha
                    wrote on last edited by
                    #9

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

                    JonBJ 1 Reply Last reply
                    0
                    • H hbatalha

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

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

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

                      H 1 Reply Last reply
                      1
                      • JonBJ JonB

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

                        H Offline
                        H Offline
                        hbatalha
                        wrote on last edited by
                        #11

                        @JonB

                        Do you want to walk before you run?

                        No, hahahaha

                        It was a genuine question. I am trying to understand the need for writing custom models.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          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.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0

                          • Login

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