Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QModelIndex: how to get the Model



  • I have a QTableView of a QSqlTableModel. When I change the selection in QTableView I get the QModelIndex from QItemSelectionModel::selectionChanged. If I qDebug the QModelIndex I get following output:

    QModelIndex(2,0,0x0,QSqlTableModel(0x1359740))
    

    I pass the QModelIndex as a parameter to a QDialog like this:

    QModelIndex index =  QModelIndex();
    DialogProfileEdit *dialogProfileEdit = new DialogProfileEdit(index, this);
    

    In the QDialog I try to get the QSqlTableModel with

    QSqlTableModel model = index.model();
    

    With this i get the following compiler error:

    no match for ‘operator=’ (operand types are ‘const QSqlTableModel’ and ‘const QAbstractItemModel*’)
    

    How can I get the QSqlTableModel from index?


  • Moderators

    @Infinity said in QModelIndex: how to get the Model:

    QSqlTableModel model = index.model();

    That method returns a pointer.

    QSqlTableModel *model = index.model();
    


  • If I do this

    const QSqlTableModel *model = index.model()
    

    I get the following compiler error:

    invalid conversion from ‘const QAbstractItemModel*’ to ‘const QSqlTableModel*’ [-fpermissive]
    


  • @Infinity
    Yes, so use one of the C++ ..._cast<> expressions to specify explicit conversion type to keep the compiler happy.... E.g. static_cast<QSqlTableModel *> if you feel confident, or dynamic_cast<QSqlTableModel *> and check for nullptr if you don't.


  • Moderators

    @JonB said in QModelIndex: how to get the Model:

    @Infinity
    Yes, so use one of the C++ ..._cast<> expressions to specify explicit conversion type to keep the compiler happy.... E.g. static_cast<QSqlTableModel *> if you feel confident, or dynamic_cast<QSqlTableModel *> and check for nullptr if you don't.

    I recommend qobject_cast<>(). Or, if you don't need the extra functionality, just do:

    QAbstractItemModel *model = index.model();
    


  • I used

    qobject_cast<const QSqlTableModel *>(index.model());
    

    but index.model() returns a const QAbstractItemModel *.

    Is there a way to make the QSqlTableModel to a non const pointer, because I would like to use setRecord()? Or is there another way that I can use setRecord() on this model?



  • If you need to do non-const operations you need to pass the model pointer downstream.
    You currently have something like a slot with a signature void onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected); and a connect that looks something like connect(view->selectionModel(),&QItemSelectionModel::selectionChanged,this,&MyClass::onSelectionChanged);

    all you have to do is chane the signature to void onSelectionChanged(QSqlTableModel * model,const QItemSelection &selected, const QItemSelection &deselected); and the connect to connect(view->selectionModel(),&QItemSelectionModel::selectionChanged,this,std::bind(&MyClass::onSelectionChanged,this,model,std::placeholders::_1,std::placeholders::_2));


Log in to reply