QSqlRelationalTableModel how it works ?



  • I made my class based on QSqlRelationalTableModel: @

    #include <QSqlRelationalTableModel>

    class AReportsModel : public QSqlRelationalTableModel {
    //-------------------------------------------------------------------------------------------------
    private: sxProperties m_filterProps;
    //-------------------------------------------------------------------------------------------------
    public: AReportsModel() : QSqlRelationalTableModel() {

           }
    

    //-------------------------------------------------------------------------------------------------
    public: ~AReportsModel() {

               destroyTempTable(tableName());
           }
    

    //-------------------------------------------------------------------------------------------------
    public: sxProperties getFilterProperties () {

                   return m_filterProps;
               };
    

    //-------------------------------------------------------------------------------------------------
    public : void setFilterProperties (sxProperties props) {

               m_filterProps = props;
               QString filter;
               sxPropertiesIterator iter(props);
    
               while(iter.hasNext()) {
    
                   iter.next();
    
                   QString arg    = iter.key();
                   QVariant value = iter.value();
    
                   if (value.isNull())
                       continue;
    
                   if (value.toInt() < 0)
                       continue;
    
                   filter += QString("%1 = %2").arg(arg).arg(value.toString());
    
                   if (iter.hasNext())
                       filter += ", ";
                   else
                       filter += ";";
               }
    
               setFilter(filter);
               select();
           };
    

    //-------------------------------------------------------------------------------------------------
    public: bool createTempTable(const QString & sTableName) {

               if (tableName() == sTableName)
                   return true;
    
               destroyTempTable(sTableName);
    
               QSqlQuery query(GET_DATABASE());
    
               QString sql = QString("CREATE TEMPORARY TABLE %1 (%2 integer, %3 text, %4 text, %5 timestamp without time zone, %6 text, %7 integer, %8 text, %9 text , text,  integer, PRIMARY KEY(%2));")
                   .arg(sTableName)
                   .arg(sxDB::cmd_list::CMD_ID)
                   .arg(PropName::session)
                   .arg(PropName::source)
                   .arg(sxDB::cmd_list::CMD_TIME/*PropName::start_date*/)//.arg(PropName::start_time)
                   .arg(PropName::duration)
                   .arg(sxPropName::priority)                       
                   .arg(PropName::owner)
                   .arg(sxPropName::action)
                   .arg(sxPropName::note)
                   .arg(PropName::zoneID);              
               
               if (query.exec&#40;sql&#41; == false)
                   return false;             
                                            
               setTable(sTableName);               
    
               return true;
           }
    

    //-------------------------------------------------------------------------------------------------
    private: bool destroyTempTable(const QString & tableName) {

               if (tableName.isEmpty())
                   return false;
    
               QSqlQuery query(QString("DROP TABLE %1 ").arg(tableName), GET_DATABASE());
               return query.exec&#40;&#41;;
           }
    

    //-------------------------------------------------------------------------------------------------
    public: void run() {

           }
    

    //-------------------------------------------------------------------------------------------------
    private: void initHeader() {

           }
    

    //-------------------------------------------------------------------------------------------------
    protected: virtual bool prepareRecord(const sxProperties & props) = 0;
    //-------------------------------------------------------------------------------------------------
    };

    Then set this model for view:
    @AReportsModel * model = new ZoneReportsModel();

        model->run();
    
        ReportsView * view = new ReportsView(this/*, model*/);      
        view->setModel(model);
        view->hideColumn(0);@
    

    But my view shows data only when i call @query.exec(QString("SELECT * FROM %1").arg(tableName()));
    setQuery(query);@ in model before setting model to this view but shows nothing if I replace this by @setTable("table_name");
    select();@



  • Method run looks like this: @

               createTempTable("temp_table");
    
               // Big Select to get all interesting records
               QString sql = QString("SELECT %1, %2, %3, %4 FROM public.%5, public.%6, public.%7 WHERE %4 = %8 AND %9 =  ORDER BY %4")
                   .arg(QString("%1.%2").arg(sxDB::table::cmd_desc).arg(sxDB::cmd_desc::PROP_NAME))
                   .arg(QString("%1.%2").arg(sxDB::table::cmd_args).arg(sxDB::cmd_args::PROP_VALUE))
                   .arg(QString("%1.%2").arg(sxDB::table::cmd_list).arg(sxDB::cmd_list::CMD_TIME))
                   .arg(QString("%1.%2").arg(sxDB::table::cmd_list).arg(sxDB::cmd_list::CMD_ID))
                   .arg(sxDB::table::cmd_list)
                   .arg(sxDB::table::cmd_desc)
                   .arg(sxDB::table::cmd_args)
                   .arg(QString("%1.%2").arg(sxDB::table::cmd_args).arg(sxDB::cmd_args::CMD_ID))
                   .arg(QString("%1.%2").arg(sxDB::table::cmd_desc).arg(sxDB::cmd_desc::PROP_ID))
                   .arg(QString("%1.%2").arg(sxDB::table::cmd_args).arg(sxDB::cmd_args::PROP_ID));
               
               QSqlQuery query(GET_DATABASE());              
    
               if (query.exec&#40;sql&#41; == false)
                   return;
    
               sxProperties props;
               int id = -1;
    
               while (query.next()) {
    
                   QSqlRecord record = query.record();
    
                   QSqlField fieldName  = record.field(sxDB::cmd_desc::PROP_NAME);
                   QSqlField fieldValue = record.field(sxDB::cmd_args::PROP_VALUE);
                   QSqlField fieldTime  = record.field(sxDB::cmd_list::CMD_TIME);
                   QSqlField fieldId    = record.field(sxDB::cmd_list::CMD_ID);
    
                   if (id < 0 || id == fieldId.value().toInt()) {
    
                       if (id < 0)
                           id = fieldId.value().toInt();
    
                       QString  arg   = fieldName.value().toString();
                       QVariant value = fieldValue.value();
    
                       if (arg == PropName::zoneID || arg == PropName::session ||
                           arg == sxDB::cmd_list::CMD_TIME/*PropName::start_date*/ || arg == PropName::duration ||
                           arg == sxPropName::priority || arg == PropName::owner ||
                           arg == sxPropName::action || arg == sxPropName::note) {
                               props.insert(arg, value);
                       }  
    
                       props.insert(sxDB::cmd_list::CMD_ID, fieldId.value());
                       props.insert(sxDB::cmd_list::CMD_TIME, fieldTime.value());
    
                   } else {
    
                       // check for filter
                       if (prepareRecord(props))
                            GET_DATABASE().insertRecord(tableName(), props);
    
                       props.clear();
                       id = fieldId.value().toInt();
                   }                                      
               }  
    
               //select      
               query.clear();
               query.exec&#40;QString("SELECT * FROM %1"&#41;.arg(tableName()));
               setQuery(query);
    
               initHeader();@

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.