Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:31:49+00:00 2026-06-18T06:31:49+00:00

I wrote QSQLTableModel inheritor for working with qml and it’s work well. I need

  • 0

I wrote QSQLTableModel inheritor for working with qml and it’s work well. I need use it with QTableView too, data shows, but I cannot modify it – when I edit everything is ok, but all changes drop when I get out from field (I know about editStrategy, but the problem occurs earlier). I suppose that something wrong with virtual function, but I cant undestant what. If i create QSqlTableModel with the same parameters, everything is ok. Somebody have any idea how can i fix this? My code:

h:

class ListModel : public QSqlTableModel
{

    Q_OBJECT
    Q_PROPERTY( int count READ rowCount() NOTIFY countChanged())

signals:
    void countChanged();

public:
    Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const;
    ListModel(QObject *parent, QSqlDatabase _db):QSqlTableModel(parent,_db){this->setEditStrategy(QSqlTableModel::OnManualSubmit);}
    void applyRoles();
#ifdef HAVE_QT5

virtual QHash<int, QByteArray> roleNames() const{return roles;}

#endif

private:
    int count;
    QHash<int, QByteArray> roles;
};

cpp:

//based on http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML

void ListModel::applyRoles()
{
    roles.clear();
    qDebug()<<"\n"<<this->tableName();
    for (int i = 0; i < this->columnCount(); i++) {
            QString role=this->headerData(i, Qt::Horizontal).toString();
            roles[Qt::UserRole + i + 1] = QVariant(role).toByteArray();
            qDebug()<<this->headerData(i, Qt::Horizontal);
    }
#ifndef HAVE_QT5
    setRoleNames(roles);
#endif
}

QVariant ListModel::data(const QModelIndex &index, int role) const{

    QVariant value;
    if(role < Qt::UserRole)
    {
        value = QSqlQueryModel::data(index, role);
    }
    else {
        int columnIdx = role - Qt::UserRole - 1;
        QModelIndex modelIndex = this->index(index.row(), columnIdx);
        value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
    }
    return value;
}

UPD
I understood that the problem is in data method’s quantifier const, if I remove it everything is ok with QTableView, but I cant get data from model with gml’s listviews. I see only one solution – replace interition from QSqlTableModel with aggregation it, but maybe someone knows better solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:31:50+00:00Added an answer on June 18, 2026 at 6:31 am

    Summary: Solved with strange hack – inherited from QSqlRelationalTableModel instead QSqlTableModel, I think the reason is that QSqlRelationalTableModel has rewritten non virtual method data

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote an application in c# that can read srd files well, but now
I wrote a facebook application but authentication is not working properly. Despite of googling
I wrote a symfony task to fill a database of sample data. Here's a
I wrote a simple XML file and a DTD file including an entity, but
I wrote a tweak for cydia , but it does not seem to be
I wrote the sentence as follows: allLinkValues = ie.getLinksValue('class') but the return values are
I wrote a Java program to download a HTML page. But CPU usage is
Wrote a custom backend component, been working fine. A subset of users were only
I wrote some code to generate a boost::mpl::vector to use as a lookup table
I wrote a stored procedure to return the record but as soon as I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.