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. QModelIndex: how to get the Model
Forum Updated to NodeBB v4.3 + New Features

QModelIndex: how to get the Model

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.3k 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by
    #1

    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?

    sierdzioS 1 Reply Last reply
    0
    • I Infinity

      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?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      QSqlTableModel model = index.model();

      That method returns a pointer.

      QSqlTableModel *model = index.model();
      

      (Z(:^

      1 Reply Last reply
      3
      • I Offline
        I Offline
        Infinity
        wrote on last edited by Infinity
        #3

        If I do this

        const QSqlTableModel *model = index.model()
        

        I get the following compiler error:

        invalid conversion from ‘const QAbstractItemModel*’ to ‘const QSqlTableModel*’ [-fpermissive]
        
        JonBJ 1 Reply Last reply
        0
        • I Infinity

          If I do this

          const QSqlTableModel *model = index.model()
          

          I get the following compiler error:

          invalid conversion from ‘const QAbstractItemModel*’ to ‘const QSqlTableModel*’ [-fpermissive]
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @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.

          sierdzioS 1 Reply Last reply
          3
          • JonBJ JonB

            @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.

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            @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();
            

            (Z(:^

            1 Reply Last reply
            5
            • I Offline
              I Offline
              Infinity
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                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));

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                6

                • Login

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