Project

General

Profile

Implementing a Model based on Dbo QueryModel

Added by Uana Bipro over 3 years ago

Hello.

I'm trying to implement a class based on QAbstractTableModel (Qt) using the source code from QueryModel as guideline.

The problem I'm getting is that the data method from QueryModel uses fieldIdx_, a private member from QueryColumn and this class is marked as friend of QueryModel.

How can I solve this problem?

Thank you.


Replies (1)

RE: Implementing a Model based on Dbo QueryModel - Added by Uana Bipro over 3 years ago

I can extend from QueryModel, but that won't give me access to friends of the parent class.

What I have right now is this:

template <class Templ>
class GModel : public QAbstractTableModel
{
public:
    GModel();
public:
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const override;
    void setQuery(Wt::Dbo::Query<Templ> &query, bool keepColumns= false);
private:
    Wt::Dbo::QueryModel<Templ> mQueryModel;
};

template <class Templ>
int GModel<Templ>::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return mQueryModel.rowCount();
}
template <class Templ>
int GModel<Templ>::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    return mQueryModel.columnCount();
}
template <class Templ>
QVariant GModel<Templ>::data(const QModelIndex &item, int role) const
{
    if ((role >= 0 &&  role < 3) || role == Qt::ItemDataRole::UserRole)
    {
        Wt::cpp17::any vl = mQueryModel.data(item.row(), item.column(), role);
        WT_USTRING sc = Wt::asString(vl);

        return QVariant(QString::fromUtf8(sc.toUTF8().data()));
    }

    return QVariant();
}
template <class Templ>
void GModel<Templ>::setQuery(Wt::Dbo::Query<Templ> &query, bool keepColumns)
{
    Q_UNUSED(keepColumns)
    mQueryModel.setQuery(query);
}

This forces me to link against wt libraries, not ideal, but I guess there is no better way to do it without changing source code from Dbo.

    (1-1/1)