Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QStandardItemModel or subclass QAbtractTableModel



  • I think I'm beginning to "get it" but so far I've missed that "Aha!" moment.

    I have an existing class that encapsulates a data item that I want to display as a table row.

    class CListBitmap : public CFrameInfo
    {
    public :
    	bool					m_bRemoved;
    	DWORD					m_dwGroupID;
    	GUID					m_JobID;
    	bool					m_bUseAsStarting;
    	CString					m_strType;
    	CString					m_strPath;
    	CString					m_strFile;
    	bool					m_bRegistered;
    	bool					m_bChecked;
    	double					m_fOverallQuality;
    	double					m_fFWHM;
    	double					m_dX;
    	double					m_dY;
    	double					m_fAngle;
    	CSkyBackground			m_SkyBackground;
    	bool					m_bDeltaComputed;
    	CString					m_strCFA;
    	CString					m_strSizes;
    	CString					m_strDepth;
    	bool					m_bCompatible;
    	CBilinearParameters		m_Transformation;
    	VOTINGPAIRVECTOR		m_vVotedPairs;
    	LONG					m_lNrStars;
    	bool					m_bComet;
    

    I'm trying to get my head around how I would use this with QStandardItem/QStandardItemModel. Or is this a case where that isn't the right model?

    If that's so, I believe I need to sub-class QAbstractTableModel? If so what's the best approach to take? Should I store a vector<CListBitmap> inside the model? Does anyone have some code that does this sort of thing with an existing class they'd be prepared to share?

    Thanks
    David


  • Lifetime Qt Champion

    Hi
    Well, it depends.
    QStandardItemModel works pretty well load and displays the data but
    editing is a bit clunky as when data changes, you get an Item * and must take its text
    and stuff it back into the right m_xxxx variable.

    If you make your own model, you would directly return/set the data to your CListBitmap .

    look here.

    http://www.java2s.com/Code/Cpp/Qt/Asimpleexamplethatshowshowselectionscanbeuseddirectlyonamodel.htm

    they uses
    QList<QStringList> rowList;
    but you would just use
    vector<CListBitmap>

    and most of the code would be the same, except the data function should also
    return the right m_xxx memeber

    it does like this now
    return rowList[index.row()][index.column()];
    but you should map index.column() to a m_xxx variable
    A switch case could do it.

    A custom model is more code but also more flexible in the long run.

    It explains the functions here.
    https://doc.qt.io/qt-5/model-view-programming.html#an-editable-model


Log in to reply