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. [solved] What model/view architecture should I use in my case?
Forum Updated to NodeBB v4.3 + New Features

[solved] What model/view architecture should I use in my case?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 1.9k 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.
  • HarbH Offline
    HarbH Offline
    Harb
    wrote on last edited by Harb
    #1

    Hi guys!

    Now, I'am a little bit confused with that model/view architecture. Seems quite sophisticated for me, and too many options to choose. So, maybe more experienced developers could give me an advice, at least what subclasses should I implement in my particular case.

    I want to create a subwindow in my mdi project, which will work as a plot manager. I have a set of y=f(x) data. User can add y=f(x) data to plot manager to visualize it. Plot manager contains two widgets: PlotWidget(for plotting y=f(x) graphs) and QTreeView(or maybe QTableView).

    In QTreeView(QTableView) is going to be a table, with columns like:
    -name (name of y=f(x) data)
    -color
    -line type
    -line width
    -scatter type

    And of course I want to synchronize this table with Plot, so changing of color, line width, etc... would change appearance of the related graph, and visa versa selecting graph in the plot would select row in the table. So what classes of QT model/view architecture should I use? Now I am thinking about keeping all graphs data in a QList<GraphData>, where GraphData is a strucutre, which holds color, line type etc... and a pointer to a data. Then I will have to implement QAbstractModel as a wrapper to my QList, use standard View class, QTableView for example, and also implement custom Delegate. It seems like a lot of tedious work for me right now.

    And one more important question return not Qt integral data type in my model. Because QAbstractItemModel::data() return QVariant, does it mean that I will have to do type conversion from my Type to QT integral type?

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

      Hi,

      Looks like a QAbstractTableModel to handle your list of GraphData where each column corresponds to one property of GraphData. The custom delegate is only needed if you have special types that you want to use or you want a different representation of your data.

      It might look a bit daunting at first be it's really worth it in the long run.

      You will have to do the conversion when you store the data back in your GraphData struct but its really not complicated i.e. int myValue = variant.toInt();

      Hope it helps

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

      HarbH 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Looks like a QAbstractTableModel to handle your list of GraphData where each column corresponds to one property of GraphData. The custom delegate is only needed if you have special types that you want to use or you want a different representation of your data.

        It might look a bit daunting at first be it's really worth it in the long run.

        You will have to do the conversion when you store the data back in your GraphData struct but its really not complicated i.e. int myValue = variant.toInt();

        Hope it helps

        HarbH Offline
        HarbH Offline
        Harb
        wrote on last edited by
        #3

        @SGaist

        Well, finally I decided to take QAbstractTableModel, as you mentioned in your post. Seems like most suitable option.

        I have tried to implement QAbstractTableModel subclassing, but it seems that I do something wrong. Because I get runtime error right now, when my program create my Plot Manager SubWindow. Debugging showed that error occurs on line
        ui->tableView->setModel(pGraphDataModel);

        pGraphDataModel is a pointer to GraphDataModel, my subclass of QAbstractTableModel.
        pGraphDataModel is a property of SubWindow. While construction I create GraphDataModel object in heap and give returning pointer to pGraphDataModel.

        In GraphDataModel object I have property pGraphList, which apparently is a pointer to my QList<GraphData>. I give GraphDataModel this pointer in constructor. I also have implemented following virtual functions:
        QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
        QVariant headerData(int section, Qt::Orientation orientation,
        int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
        int rowCount(const QModelIndex &index = QModelIndex()) const Q_DECL_OVERRIDE;
        Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;

        I don't want to implement other functions yet, course now I just want to display headers in my QTableView. So, where can be a problem? Maybe I need to implement some other functions?

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

          What exact error are you getting ?

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

          HarbH 1 Reply Last reply
          0
          • SGaistS SGaist

            What exact error are you getting ?

            HarbH Offline
            HarbH Offline
            Harb
            wrote on last edited by
            #5

            @SGaist
            "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support for more information"

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

              Sounds like you need to check your pointers handling

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

              HarbH 1 Reply Last reply
              1
              • SGaistS SGaist

                Sounds like you need to check your pointers handling

                HarbH Offline
                HarbH Offline
                Harb
                wrote on last edited by
                #7

                @SGaist
                Oh! just several second ago I have found a problem! I didn't implement columnCount virtual funtion! Now everything work properly. Anyway tnx for answer! :)

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

                  Good !

                  Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                  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