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. QSqlQueryModel downcasting problem !
QtWS25 Last Chance

QSqlQueryModel downcasting problem !

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqlquerymodeldowncastingtableview
4 Posts 4 Posters 1.3k Views
  • 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.
  • _ Offline
    _ Offline
    _compiler
    wrote on last edited by _compiler
    #1
    class Model : public QSqlQueryModel {
        QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const;
    };
    
    QSqlQueryModel *model = new QSqlQueryModel();
    model->setQuery("select * from table");
    
    Model *derived = static_cast<QSqlQueryModel*>(model );  
    
    ui->tableView->setModel(derived);  //not working
    
    kshegunovK 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @_compiler said:
      Hi

      
       Model *derived = static_cast<QSqlQueryModel*>(model );  
      
      Should it not be 
       Model *derived = static_cast<Model *>(model );  
      

      ?

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        To add to @mrjj, it should be qobject_cast

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • _ _compiler
          class Model : public QSqlQueryModel {
              QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const;
          };
          
          QSqlQueryModel *model = new QSqlQueryModel();
          model->setQuery("select * from table");
          
          Model *derived = static_cast<QSqlQueryModel*>(model );  
          
          ui->tableView->setModel(derived);  //not working
          
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @_compiler

          Model *derived = static_cast<QSqlQueryModel*>(model);
          

          This isn't a valid cast because you can't implicitly cast to a derived type (see @mrjj's comment).

          Model *derived = static_cast<Model *>(model);  
          

          Is valid but unsafe, as @SGaist pointed out.

          Ultimately, what is not working?

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1

          • Login

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