Important: Please read the Qt Code of Conduct -

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

  • 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)
    -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?

  • Lifetime Qt Champion


    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

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

    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?

  • Lifetime Qt Champion

    What exact error are you getting ?

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

  • Lifetime Qt Champion

    Sounds like you need to check your pointers handling

  • @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! :)

  • Lifetime Qt Champion

    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 :)

Log in to reply