Quantcast
Channel: Installation and Deployment
Viewing all articles
Browse latest Browse all 22112

QSqlRelationalTableModel how it works ?

$
0
0
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(sql) == 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();            } //-------------------------------------------------------------------------------------------------     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();

Viewing all articles
Browse latest Browse all 22112

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>