Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change the color of the row in the Sqlite table?
Forum Updated to NodeBB v4.3 + New Features

How to change the color of the row in the Sqlite table?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    Hi
    The color you see in SqliteBrowser is not stored in
    the database and cannot be changed.
    i assume you mean the white / grey / white / grey way of showing the rows.

    Else please explain what color you mean as rows dont have colors as such.

    I 1 Reply Last reply
    4
    • mrjjM mrjj

      Hi
      The color you see in SqliteBrowser is not stored in
      the database and cannot be changed.
      i assume you mean the white / grey / white / grey way of showing the rows.

      Else please explain what color you mean as rows dont have colors as such.

      I Offline
      I Offline
      isan
      wrote on last edited by isan
      #3

      @mrjj Hi, I want to record absenteeism in the Sqlite database
      And I want to set red/black/gray/... (just for difference) rows in table for holiday

      mrjjM 1 Reply Last reply
      0
      • I isan

        @mrjj Hi, I want to record absenteeism in the Sqlite database
        And I want to set red/black/gray/... (just for difference) rows in table for holiday

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #4

        @isan
        Hi
        That is actually done by the view/widget where you show the data.
        You can do it like

        void setRowBackground(const QBrush& brush, QAbstractItemModel* model, int row, const QModelIndex& parent = QModelIndex() ) {
          if(!model || row < 0) {
            return;
          }
          if(row >= model->rowCount(parent)) {
            return;
          }
          if(parent.isValid()) {
            if(parent.model() != model) {
              return;
            }
          }
          for(int i = 0; i < model->columnCount(parent); ++i) {
            model->setData(model->index(row, i, parent), brush, Qt::BackgroundRole);
          }
        }
        
        

        credits to @VRonin from
        https://forum.qt.io/topic/75151/qtablewidget-set-specific-row-s-color/5

        VRoninV 1 Reply Last reply
        4
        • mrjjM mrjj

          @isan
          Hi
          That is actually done by the view/widget where you show the data.
          You can do it like

          void setRowBackground(const QBrush& brush, QAbstractItemModel* model, int row, const QModelIndex& parent = QModelIndex() ) {
            if(!model || row < 0) {
              return;
            }
            if(row >= model->rowCount(parent)) {
              return;
            }
            if(parent.isValid()) {
              if(parent.model() != model) {
                return;
              }
            }
            for(int i = 0; i < model->columnCount(parent); ++i) {
              model->setData(model->index(row, i, parent), brush, Qt::BackgroundRole);
            }
          }
          
          

          credits to @VRonin from
          https://forum.qt.io/topic/75151/qtablewidget-set-specific-row-s-color/5

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #5

          @mrjj That method only works if the model supports multiple roles (i.e. QSql*Models are a no go). I still didn't understand where OP wants the color to appear. Is it in a custom Qt application or in a completely unrelated "SqliteBrowser on Windows"?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          mrjjM 1 Reply Last reply
          5
          • VRoninV VRonin

            @mrjj That method only works if the model supports multiple roles (i.e. QSql*Models are a no go). I still didn't understand where OP wants the color to appear. Is it in a custom Qt application or in a completely unrelated "SqliteBrowser on Windows"?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @VRonin
            Hi.
            thank you for clarification.
            Also unclear to me, however since he shows
            code with QSqlQuery , i assume he will have a view/widget to show the data in.

            I 1 Reply Last reply
            0
            • mrjjM mrjj

              @VRonin
              Hi.
              thank you for clarification.
              Also unclear to me, however since he shows
              code with QSqlQuery , i assume he will have a view/widget to show the data in.

              I Offline
              I Offline
              isan
              wrote on last edited by isan
              #7

              @mrjj yes I use tableView to show QSqlite Table
              after this

              ui->tableView->setModel(model);
              

              I should use setRowBackground function?
              and what are the inputs of function?

              mrjjM 1 Reply Last reply
              0
              • I isan

                @mrjj yes I use tableView to show QSqlite Table
                after this

                ui->tableView->setModel(model);
                

                I should use setRowBackground function?
                and what are the inputs of function?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #8

                @isan
                hi
                its actually the Qt::BackgroundRole
                that is key.
                the setRowBackground is just a helper function VRonin wrote.

                • and what are the input function?
                  Im not sure what you mean by this ?
                  However, if you mean save back to the database, that's not really possible.
                  Unless you change the table definition to include a QColor or something like that.
                I 1 Reply Last reply
                1
                • mrjjM mrjj

                  @isan
                  hi
                  its actually the Qt::BackgroundRole
                  that is key.
                  the setRowBackground is just a helper function VRonin wrote.

                  • and what are the input function?
                    Im not sure what you mean by this ?
                    However, if you mean save back to the database, that's not really possible.
                    Unless you change the table definition to include a QColor or something like that.
                  I Offline
                  I Offline
                  isan
                  wrote on last edited by
                  #9

                  @mrjj my mean : what are QAbstractItemModel *model and const QModelIndex &parent
                  I do this and row color doesn't make change

                  QSqlQueryModel *model =new QSqlQueryModel();
                          db1.setDatabaseName( DBn );
                          if( !db1.open() )
                          {
                              qDebug() << db1.lastError();
                              qDebug( "Failed to connect." );
                          }
                          else
                              qDebug( "Connected to database!" );
                  
                          QSqlQuery  qry(db1);
                          qry.prepare( "SELECT * FROM _"+Ms+"");
                          qry.exec();
                          model->setQuery(qry);
                          ui->tableView->setModel(model);
                          setRowBackground(QColor(250,0,0),ui->tableView->model(),1);
                  
                  VRoninV 1 Reply Last reply
                  0
                  • I isan

                    @mrjj my mean : what are QAbstractItemModel *model and const QModelIndex &parent
                    I do this and row color doesn't make change

                    QSqlQueryModel *model =new QSqlQueryModel();
                            db1.setDatabaseName( DBn );
                            if( !db1.open() )
                            {
                                qDebug() << db1.lastError();
                                qDebug( "Failed to connect." );
                            }
                            else
                                qDebug( "Connected to database!" );
                    
                            QSqlQuery  qry(db1);
                            qry.prepare( "SELECT * FROM _"+Ms+"");
                            qry.exec();
                            model->setQuery(qry);
                            ui->tableView->setModel(model);
                            setRowBackground(QColor(250,0,0),ui->tableView->model(),1);
                    
                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #10

                    @isan said in How to change the color of the row in the Sqlite table?:

                    setRowBackground(QColor(250,0,0),ui->tableView->model(),1);

                    @VRonin said in How to change the color of the row in the Sqlite table?:

                    That method only works if the model supports multiple roles (i.e. QSql*Models are a no go).

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    I 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @isan said in How to change the color of the row in the Sqlite table?:

                      setRowBackground(QColor(250,0,0),ui->tableView->model(),1);

                      @VRonin said in How to change the color of the row in the Sqlite table?:

                      That method only works if the model supports multiple roles (i.e. QSql*Models are a no go).

                      I Offline
                      I Offline
                      isan
                      wrote on last edited by isan
                      #11

                      @VRonin said in How to change the color of the row in the Sqlite table?:

                      That method only works if the model supports multiple roles (i.e. QSql*Models are a no go).

                      my model something like :

                      0_1551174579117_model.jpg

                      That method not work for this?
                      For example I want to set color for rows that have Thursday and Friday

                      1 Reply Last reply
                      0
                      • VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by VRonin
                        #12

                        Check out this library: https://github.com/VSRonin/QtModelUtilities
                        In particular this example: https://github.com/VSRonin/QtModelUtilities/blob/1.0.0/examples/exam_RoleMaskProxyModel/exam_rolemaskhighlight.cpp

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply
                        2
                        • I Offline
                          I Offline
                          isan
                          wrote on last edited by
                          #13
                          This post is deleted!
                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved