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. tableView get data from hidden cells
Forum Updated to NodeBB v4.3 + New Features

tableView get data from hidden cells

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.2k Views 2 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.
  • M Offline
    M Offline
    Mogli123
    wrote on last edited by Mogli123
    #1

    Hi,
    I'm new in Qt and c++ and have the following problem.
    I display som test data in a table view. This works so far.
    Now I had creat a column "Color".
    This column I hide becouse this Column is only for the information that I know how i had to color the row.
    My problem is that I can't read the data from the cells with the color information in it, becouse they are hidden.
    Is there any way to get the information from the cells?

    .cpp

    Tableanpassen::Tableanpassen(QObject *parent)
        : QSqlQueryModel(parent)
    {
    }
    
    
    QVariant Tableanpassen::data(const QModelIndex &index, int role) const
    {
        bool  static s_red = false;
        bool  static s_yellow = false;
        bool  static s_green = false;
        bool  static s_gray = false;
    
        //Zentriert den Text in den Zellen
        if(role == Qt::TextAlignmentRole)
        {
            return Qt::AlignCenter;
        }
    
        //setzt die gewünschte Hintergrundfarbe
        if(role == Qt::BackgroundRole )
        {
    
        // abfrage welche Hintergrundfarbe benötigt wird
        QString inhalt = index.data(Qt::DisplayRole).toString();
                if ( inhalt == "red" )
                {
                    s_red = true;
                    s_yellow = false;
                    s_green = false;
                    s_gray = false;
                }
                if (inhalt == "yellow")
                {
                    s_red = false;
                    s_yellow = true;
                    s_green = false;
                    s_gray = false;
                }
                if (inhalt == "green")
                {
                    s_red = false;
                    s_yellow = false;
                    s_green = true;
                    s_gray = false;
                }
                if (inhalt == "gray")
                {
                    s_red = false;
                    s_yellow = false;
                    s_green = false;
                    s_gray = true;
                }
    
    
                // Hintergrund wie gewünscht färben
                if (s_red == true)
                {
                    QBrush Background(Qt::red);
                    return Background;
                }
    
                if (s_yellow == true)
                {
                    QBrush Background(Qt::yellow);
                    return Background;
                }
    
                if (s_green == true)
                {
                    QBrush Background(Qt::green);
                    return Background;
                }
    
                if (s_gray == true)
                {
                    QBrush Background(Qt::gray);
                    return Background;
                }
        }
    
        //setzt die Daten
        return QSqlQueryModel::data(index, role);
    }
    
    #include "test_screen.h"
    #include "ui_test_screen.h"
    #include "tableanpassen.h"
    
    
    #include <QTableView>
    #include "QHeaderView"
    
    test_screen::test_screen(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::test_screen)
    {
        ui->setupUi(this);
    
    //***************************************************
    //  -- TableView Werkzeug Wechsel Warnung
    //***************************************************
    
        mytableanpassen = new Tableanpassen(this);
    
        mytableanpassen->setQuery("SELECT * FROM WekzeugWechselWarnung");
        mytableanpassen->setHeaderData(1, Qt::Horizontal, tr("Werkzeug Nr."));
        mytableanpassen->setHeaderData(2, Qt::Horizontal, tr("Zeit"));
    
        ui->table_werkzeug_wechsel_warnung->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->table_werkzeug_wechsel_warnung->setModel(mytableanpassen);
    
    
        // gewünschte Spalte wird nicht in der Tableview angezeigt
        // Befehl mus nach dem "setModel" Befehl stehen!!!
        ui->table_werkzeug_wechsel_warnung->setColumnHidden(0, true);
    
        ui->table_werkzeug_wechsel_warnung->setStyleSheet("QHeaderView::section { background-color:grey }");
    

    Thank you

    Regards

    raven-worxR 1 Reply Last reply
    0
    • M Mogli123

      Hi,
      I'm new in Qt and c++ and have the following problem.
      I display som test data in a table view. This works so far.
      Now I had creat a column "Color".
      This column I hide becouse this Column is only for the information that I know how i had to color the row.
      My problem is that I can't read the data from the cells with the color information in it, becouse they are hidden.
      Is there any way to get the information from the cells?

      .cpp

      Tableanpassen::Tableanpassen(QObject *parent)
          : QSqlQueryModel(parent)
      {
      }
      
      
      QVariant Tableanpassen::data(const QModelIndex &index, int role) const
      {
          bool  static s_red = false;
          bool  static s_yellow = false;
          bool  static s_green = false;
          bool  static s_gray = false;
      
          //Zentriert den Text in den Zellen
          if(role == Qt::TextAlignmentRole)
          {
              return Qt::AlignCenter;
          }
      
          //setzt die gewünschte Hintergrundfarbe
          if(role == Qt::BackgroundRole )
          {
      
          // abfrage welche Hintergrundfarbe benötigt wird
          QString inhalt = index.data(Qt::DisplayRole).toString();
                  if ( inhalt == "red" )
                  {
                      s_red = true;
                      s_yellow = false;
                      s_green = false;
                      s_gray = false;
                  }
                  if (inhalt == "yellow")
                  {
                      s_red = false;
                      s_yellow = true;
                      s_green = false;
                      s_gray = false;
                  }
                  if (inhalt == "green")
                  {
                      s_red = false;
                      s_yellow = false;
                      s_green = true;
                      s_gray = false;
                  }
                  if (inhalt == "gray")
                  {
                      s_red = false;
                      s_yellow = false;
                      s_green = false;
                      s_gray = true;
                  }
      
      
                  // Hintergrund wie gewünscht färben
                  if (s_red == true)
                  {
                      QBrush Background(Qt::red);
                      return Background;
                  }
      
                  if (s_yellow == true)
                  {
                      QBrush Background(Qt::yellow);
                      return Background;
                  }
      
                  if (s_green == true)
                  {
                      QBrush Background(Qt::green);
                      return Background;
                  }
      
                  if (s_gray == true)
                  {
                      QBrush Background(Qt::gray);
                      return Background;
                  }
          }
      
          //setzt die Daten
          return QSqlQueryModel::data(index, role);
      }
      
      #include "test_screen.h"
      #include "ui_test_screen.h"
      #include "tableanpassen.h"
      
      
      #include <QTableView>
      #include "QHeaderView"
      
      test_screen::test_screen(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::test_screen)
      {
          ui->setupUi(this);
      
      //***************************************************
      //  -- TableView Werkzeug Wechsel Warnung
      //***************************************************
      
          mytableanpassen = new Tableanpassen(this);
      
          mytableanpassen->setQuery("SELECT * FROM WekzeugWechselWarnung");
          mytableanpassen->setHeaderData(1, Qt::Horizontal, tr("Werkzeug Nr."));
          mytableanpassen->setHeaderData(2, Qt::Horizontal, tr("Zeit"));
      
          ui->table_werkzeug_wechsel_warnung->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
          ui->table_werkzeug_wechsel_warnung->setModel(mytableanpassen);
      
      
          // gewünschte Spalte wird nicht in der Tableview angezeigt
          // Befehl mus nach dem "setModel" Befehl stehen!!!
          ui->table_werkzeug_wechsel_warnung->setColumnHidden(0, true);
      
          ui->table_werkzeug_wechsel_warnung->setStyleSheet("QHeaderView::section { background-color:grey }");
      

      Thank you

      Regards

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Mogli123 said in tableView get data from hidden cells:

      My problem is that I can't read the data from the cells with the color information in it, becouse they are hidden.

      what?
      Hiding columns should only affect the view, not the model (which is actually holding the data).

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Mogli123
        wrote on last edited by
        #3

        If I remove the hide comment, to color the background works.
        I use the following test database.

        #ifndef DB_H
        #define DB_H
        
        #include <QMessageBox>
        #include <QSqlDatabase>
        #include <QSqlError>
        #include <QSqlQuery>
        
        // Datenbank für Tabellen
        
        
        static bool createConnection()
        {
            QSqlDatabase db_Werkzeug_Wech_Warn = QSqlDatabase::addDatabase("QSQLITE");
            db_Werkzeug_Wech_Warn.setDatabaseName(":memory:");
            if (!db_Werkzeug_Wech_Warn.open()) {
                QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
                    QObject::tr("Unable to establish a database connection.\n"
                                "This example needs SQLite support. Please read "
                                "the Qt SQL driver documentation for information how "
                                "to build it.\n\n"
                                "Click Cancel to exit."), QMessageBox::Cancel);
                return false;
            }
        
        // inhalte für tabellen + default überschrift (wird in "test_screen.cpp" überschrieben
        
        //********************************************
        //  -- Tabelle 1 für Werkzeug Wechsel Warnung --
        //********************************************
        
            QSqlQuery query;
            query.exec("Create Table WekzeugWechselWarnung (COLOR varchar(20), "
                       "NR int primary key,ZEIT varchar(20))");
            // bei NR int ist "primary key" gesetzt => jede Zahl kann nur einmal vorkommen
            // fals eine Zahl doppelt vorkommt wird die letzt Zeile (mit der gleichen Zahl) ingoriert
            query.exec("insert Into WekzeugWechselWarnung values('red', '3', '01:32')");
            query.exec("Insert Into WekzeugWechselWarnung values('yellow', '4', '00:25')");
            query.exec("Insert Into WekzeugWechselWarnung values('red', '2', '12:45')");
            query.exec("Insert Into WekzeugWechselWarnung values('green', '1', '04:36')");
            query.exec("Insert Into WekzeugWechselWarnung values('yellow', '6', '00:10')");
        
        

        I have no idea how I can solve the problem.
        I hope someone can halp me.

        Thank you

        raven-worxR 1 Reply Last reply
        0
        • M Mogli123

          If I remove the hide comment, to color the background works.
          I use the following test database.

          #ifndef DB_H
          #define DB_H
          
          #include <QMessageBox>
          #include <QSqlDatabase>
          #include <QSqlError>
          #include <QSqlQuery>
          
          // Datenbank für Tabellen
          
          
          static bool createConnection()
          {
              QSqlDatabase db_Werkzeug_Wech_Warn = QSqlDatabase::addDatabase("QSQLITE");
              db_Werkzeug_Wech_Warn.setDatabaseName(":memory:");
              if (!db_Werkzeug_Wech_Warn.open()) {
                  QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
                      QObject::tr("Unable to establish a database connection.\n"
                                  "This example needs SQLite support. Please read "
                                  "the Qt SQL driver documentation for information how "
                                  "to build it.\n\n"
                                  "Click Cancel to exit."), QMessageBox::Cancel);
                  return false;
              }
          
          // inhalte für tabellen + default überschrift (wird in "test_screen.cpp" überschrieben
          
          //********************************************
          //  -- Tabelle 1 für Werkzeug Wechsel Warnung --
          //********************************************
          
              QSqlQuery query;
              query.exec("Create Table WekzeugWechselWarnung (COLOR varchar(20), "
                         "NR int primary key,ZEIT varchar(20))");
              // bei NR int ist "primary key" gesetzt => jede Zahl kann nur einmal vorkommen
              // fals eine Zahl doppelt vorkommt wird die letzt Zeile (mit der gleichen Zahl) ingoriert
              query.exec("insert Into WekzeugWechselWarnung values('red', '3', '01:32')");
              query.exec("Insert Into WekzeugWechselWarnung values('yellow', '4', '00:25')");
              query.exec("Insert Into WekzeugWechselWarnung values('red', '2', '12:45')");
              query.exec("Insert Into WekzeugWechselWarnung values('green', '1', '04:36')");
              query.exec("Insert Into WekzeugWechselWarnung values('yellow', '6', '00:10')");
          
          

          I have no idea how I can solve the problem.
          I hope someone can halp me.

          Thank you

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Mogli123
          now i see. You background color logic is way beyond ugly.

          Rather do something like this (in your data() method):

          if(role == Qt::BackgroundRole )
          {
              // abfrage welche Hintergrundfarbe benötigt wird
              QString inhalt = index.sibling(index.row(), COLOR_COLUMNINDEX).data(Qt::DisplayRole).toString();
              if ( inhalt == "red" )   return QBrush(Qt::red);
              else if (inhalt == "yellow") return QBrush(Qt::yellow);
              ....
          }
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          3
          • M Offline
            M Offline
            Mogli123
            wrote on last edited by
            #5

            Thank you for your answer.

            Your example looks much better but how works it exactly?
            In which way I have to declare "COLOR_COLUMNINDEX"?
            Sorry I'm very new.
            I'm very grateful for each help and tip.

            Thank you

            raven-worxR 1 Reply Last reply
            0
            • M Mogli123

              Thank you for your answer.

              Your example looks much better but how works it exactly?
              In which way I have to declare "COLOR_COLUMNINDEX"?
              Sorry I'm very new.
              I'm very grateful for each help and tip.

              Thank you

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @Mogli123 said in tableView get data from hidden cells:

              Your example looks much better but how works it exactly?

              it exactly returns what was requested from the model.
              Your code expects a specific call order. You expect that the Display role is requested first before any other role of the specific row (since you are using static variables to determine the color).
              My code also checks the contents of your color column, but this way is independent from the call order.

              The color column index is the index number of the color column in your data. 0 for the first column, 1 for the second, and so on...
              Seems like it's 0 in your case?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2
              • M Offline
                M Offline
                Mogli123
                wrote on last edited by
                #7

                Thank you very much.
                It works. :)

                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