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.4k 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.
  • I Offline
    I Offline
    isan
    wrote on 18 Feb 2019, 06:56 last edited by isan
    #1

    I insert the data in the Sqlite table from my code, I see the table from SqliteBrowser on Windows, not in the code, I want to change the color of a row in the table? How can I do it?

    db = QSqlDatabase::addDatabase( "QSQLITE" );
    
        db.setDatabaseName( Dbname );
        if( !db.open() )
        {
            qDebug() << db.lastError();
            qFatal( "Failed to connect." );
        }
    else
        qDebug( "Connected to database!" );
    
        QSqlQuery  qry(db);
        qry.prepare( "SELECT * FROM "+mths+"");
        if( !qry.exec() )
            qDebug() << qry.lastError();
        else
        {
            if(!SearchFlag)
            {
                qry.prepare( "INSERT INTO "+mths+" (  WeekDay, Date,TimeIn, TimeOut ) VALUES (:wDay,:date,:timeIn,:timeOut)" );
                qry.bindValue(":wDay", wDay);
                qry.bindValue(":date", date);
                qry.bindValue(":timeIn", timeIn);
                rowcount++;
    
                if( !qry.exec() )
                    qDebug() << qry.lastError();
                else
                    qDebug( "Inserted!" );
    
            }
             db.close();
        }
    
     db.close();
    
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 18 Feb 2019, 07:25 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 18 Feb 2019, 09:22
      4
      • M mrjj
        18 Feb 2019, 07:25

        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 18 Feb 2019, 09:22 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

        M 1 Reply Last reply 18 Feb 2019, 11:39
        0
        • I isan
          18 Feb 2019, 09:22

          @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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 18 Feb 2019, 11:39 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

          V 1 Reply Last reply 18 Feb 2019, 14:19
          4
          • M mrjj
            18 Feb 2019, 11:39

            @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

            V Offline
            V Offline
            VRonin
            wrote on 18 Feb 2019, 14:19 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

            M 1 Reply Last reply 18 Feb 2019, 14:22
            5
            • V VRonin
              18 Feb 2019, 14:19

              @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"?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 18 Feb 2019, 14:22 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 25 Feb 2019, 10:31
              0
              • M mrjj
                18 Feb 2019, 14:22

                @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 25 Feb 2019, 10:31 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?

                M 1 Reply Last reply 25 Feb 2019, 11:42
                0
                • I isan
                  25 Feb 2019, 10:31

                  @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?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 25 Feb 2019, 11:42 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 26 Feb 2019, 08:36
                  1
                  • M mrjj
                    25 Feb 2019, 11:42

                    @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 26 Feb 2019, 08:36 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);
                    
                    V 1 Reply Last reply 26 Feb 2019, 09:40
                    0
                    • I isan
                      26 Feb 2019, 08:36

                      @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);
                      
                      V Offline
                      V Offline
                      VRonin
                      wrote on 26 Feb 2019, 09:40 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 26 Feb 2019, 09:53
                      0
                      • V VRonin
                        26 Feb 2019, 09:40

                        @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 26 Feb 2019, 09:53 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
                        • V Offline
                          V Offline
                          VRonin
                          wrote on 26 Feb 2019, 13:07 last edited by VRonin 4 Sept 2021, 07:37
                          #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 27 Feb 2019, 07:01 last edited by
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0

                            2/13

                            18 Feb 2019, 07:25

                            11 unread
                            • Login

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