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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:04:42+00:00 2026-06-05T19:04:42+00:00

I am trying to get to grips with how you display data from a

  • 0

I am trying to get to grips with how you display data from a database into a Qt table. What I want to do is change the displayed value on a boolean to read “PASS” or “FAIL” and then change the row colour depending on this.

I have created an QSqlQueryModel derived class for the model and overrode the function:

QVariant TestResultsViewModel::data(const QModelIndex &index, int role) const
{
    QVariant value = QSqlQueryModel::data(index, role);

    if (value.isValid() && role == Qt::DisplayRole){
        switch(index.column()){
        case 0:
            return value.toBool() ? "PASS" : "FAIL";
        }
    }

    if (role == Qt::TextColorRole){
        // Get column 0
        QVariant pass = index.sibling(index.row(), 0).data();
        if (pass.isValid()){
            if (pass.toBool()){
                return QVariant::fromValue(QColor(Qt::blue));
            }
            else{
                return QVariant::fromValue(QColor(Qt::red));
            }
        }
    }

    return value;
}

But what seems to happen is that the first part is done first and thereafter the values of the column are “PASS” or “FAIL” and not 0, 1 so the colours aren’t changing.

So how should I really be doing this?

  • 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-05T19:04:43+00:00Added an answer on June 5, 2026 at 7:04 pm

    The implementation of QModelIndex::data() explains this behaviour:

    inline QVariant QModelIndex::data(int arole) const
    { return m ? m->data(*this, arole) : QVariant(); }
    

    where m is the model of the model index.

    This means your call of

    index.sibling(index.row(), 0).data(); 
    

    ends up calling TestResultsViewModel::data() again, meaning you indeed get “PASS” or “FAIL” as a result.

    In order to avoid this, you can do the following:

    QModelIndex firstColumnIndex = index.sibling( index.row(), 0 );
    QVariant pass = QSqlQueryModel::data( firstColumnIndex );
    

    This will explicitly call QSqlQueryModel::data() instead of TestResultsViewModel::data() and skip your code

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

Sidebar

Related Questions

I'm trying to get to grips with web2py/python. I want to get the user
I'm trying to get to grips with regular expressions: I have a database connection
I've been trying to get to grips with Core Data and I'm having some
I am trying to get to grips with OOP, and just want a little
I'm trying to get to grips with indexes. Given the table: Books ------ ID
I'm trying to get to grips with the different ways of styling/binding data in
I'm trying to get to grips with Qt's signal and slots mechanism. I have
I'm trying to get to grips with NHibernate, Fluent NHibernate and Spring. Following domain-driven
I am trying to get to grips with programmatically configuring views. I have a
Hi i am trying to get to grips with Dapper. My situation is 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.