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 5987939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:53:14+00:00 2026-05-22T22:53:14+00:00

I am trying to replace true and false values with checkboxes in QSqlTableModel for

  • 0

I am trying to replace true and false values with checkboxes in QSqlTableModel for a database column with bool type.

The following code appears to be work.
QSqlTableModel::setData() does post data to back end and returns true for columns other than the bool column.

The problem I encounter is that QSqlTableModel::setData() always returns false for checkbox field.

After checking the checkbox, database log does not show the SQL updating the bool column. This, if I am right, indicates that the problem is in my Qt code, not in table, column, or data type in database.

Helps will be much appreciated.

//————-

class CheckboxTableModel : public QSqlTableModel
{
public:
    CheckboxTableModel(QObject * parent=0,QSqlDatabase db=QSqlDatabase());
    QVariant data( const QModelIndex& idx, int role) const;
    Qt::ItemFlags flags(const QModelIndex&) const;
    bool setData( const QModelIndex& idx, const QVariant&, int);
};
//-------------
CheckboxTableModel::CheckboxTableModel(QObject *parent,QSqlDatabase db)
    : QSqlTableModel(parent,db)
{
}
//-------------
#define BOOLEAN_COLUMN_INDEX 7 //index of the boolean field

QVariant CheckboxTableModel::data(const QModelIndex& idx,int role) const
{
    QVariant v=QSqlTableModel::data(idx,role);
    if(idx.column() == BOOLEAN_COLUMN_INDEX && (role == Qt::CheckStateRole || role == Qt::EditRole))
        return v.toBool() ? Qt::Checked : Qt::Unchecked;
    else
        return v;
}

Qt::ItemFlags CheckboxTableModel::flags(const QModelIndex& idx) const
{
    if(idx.row() < 0 || idx.row() >= rowCount() || idx.column() < 0 || idx.column() >= columnCount())
        return 0;
    if(idx.column() == BOOLEAN_COLUMN_INDEX)
        return Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

bool CheckboxTableModel::setData(const QModelIndex& idx,const QVariant& value,int role)
{
    if(idx.row() < 0 || idx.row() >= rowCount() || idx.column() < 0 || idx.column() >= columnCount())
        return false;
    QVariant v;
    if(role == Qt::CheckStateRole && idx.column() == BOOLEAN_COLUMN_INDEX)
        v = value.toInt() == Qt::Checked ? true : false;
    else
        v=value;
    bool r= QSqlTableModel::setData(idx,v,role);
qDebug() << (r ? "Y":"N"); //Always returns "false" for checkbox column. Why? How do I fix this?
    return r;
}

edit strategy:

CheckboxTableModel *model=new CheckboxTableModel(this);
model->setEditStrategy(QSqlTableModel::OnFieldChange);
  • 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-05-22T22:53:15+00:00Added an answer on May 22, 2026 at 10:53 pm

    When calling bool r= QSqlTableModel::setData(idx,v,role); instead of passing the role try calling with Qt::EditRole as below.

    bool r= QSqlTableModel::setData(idx,v,Qt::EditRole);
    

    I haven’t tried it but i guess that this should make it work.

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

Sidebar

Related Questions

I am trying to replace multiple rows in an Access database to follow a
I've been trying to replace to following string using the following... var r =
When trying to replace the content of an XML file in C#.NET with a
I'm trying to replace each , in the current file by a new line:
I'm trying to replace the programs that run from my startup directory with a
I'm trying to replace every multiline import inside a Python source file.. So, the
I am trying to replace a two letter state abbreviation with text then the
I'm trying to replace a call to ::CoCreateGUID so I can generate GUIDs in
I am trying to replace all £ symbols in a HTML file with &pound;
I am trying to replace the backslash (escape) character in a Javascript string literal.

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.