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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:48:26+00:00 2026-06-02T13:48:26+00:00

void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){ model = new QSqlQueryModel(this); model->setQuery(sql); } With this method

  • 0
void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){
        model = new QSqlQueryModel(this);
        model->setQuery(sql);
}

With this method i can set a QSQlQueryModels to my QTableviews.

But How i can set color to a row based on a cell value?

  • 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-02T13:48:27+00:00Added an answer on June 2, 2026 at 1:48 pm

    The view draws the background based on the Qt::BackgroundRole role of the cell which is the QBrush value returned by QAbstractItemModel::data(index, role) for that role.

    You can subclass the QSqlQueryModel to redefine data() to return your calculated color, or if you have Qt > 4.8, you can use a QIdentityProxyModel:

    class MyModel : public QIdentityProxyModel
    {
        QColor calculateColorForRow(int row) const {
            ...
        }
    
        QVariant data(const QModelIndex &index, int role)
        {
            if (role == Qt::BackgroundRole) {
               int row = index.row();
               QColor color = calculateColorForRow(row);           
               return QBrush(color);
            }
            return QIdentityProxyModel::data(index, role);
        }
    };
    

    And use that model in the view, with the sql model set as source with QIdentityProxyModel::setSourceModel.

    OR

    You can keep the model unchanged and modify the background with a delegate set on the view with QAbstractItemView::setItemDelegate:

    class BackgroundColorDelegate : public QStyledItemDelegate {
    
    public:
        BackgroundColorDelegate(QObject *parent = 0)
            : QStyledItemDelegate(parent)
        {
        }
        QColor calculateColorForRow(int row) const;
    
        void initStyleOption(QStyleOptionViewItem *option,
                             const QModelIndex &index) const
        {
            QStyledItemDelegate::initStyleOption(option, index);
    
            QStyleOptionViewItemV4 *optionV4 =
                    qstyleoption_cast<QStyleOptionViewItemV4*>(option);
    
            optionV4->backgroundBrush = QBrush(calculateColorForRow(index.row()));
        }
    };
    

    As the last method is not always obvious to translate from C++ code, here is the equivalent in python:

    def initStyleOption(self, option, index):
        super(BackgroundColorDelegate,self).initStyleOption(option, index)
        option.backgroundBrush = calculateColorForRow(index.row())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code: [DllImport(user32.dll)] static extern void mouse_event(uint dwFlags, uint dx, uint dy,
In this code I'm creating a new window when I click the button. In
public class MyWindow extends JFrame { JButton botonCreate = new JButton(Open son windows); public
void FileManager::CloseFile(File * const file) { for (int i = 0; i < MAX_OPEN_FILES;
- (void)viewDidUnload { self.GPSArray = nil; self.accelerometerArray = nil; self.headingArray = nil; self.managedObjectContext =
- (void)applicationDidFinishLaunching:(UIApplication *)application { // Create the navigation and view controllers RootViewController *rootViewController =
-(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *theXML = [[NSString alloc] initWithBytes: [myWebData mutableBytes] length:[myWebData length] encoding:NSUTF8StringEncoding];
void BinaryTree::InitializeFromFile(string Filename){ ifstream inFile; treenode* Freq[256]; inFile.open(Filename.c_str(), fstream::binary); if(inFile.fail()){ cout<<Error in opening file
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { touchStartTime = [event timestamp]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
void main(void) { vec4 clipCoord = glModelViewProjectionmatrix * gl_Vertex; gl_Position = clipCoord; gl_FrontColor =

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.