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. SelectedIndexes() generates an error!
Forum Updated to NodeBB v4.3 + New Features

SelectedIndexes() generates an error!

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 8.0k Views 1 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.
  • F Offline
    F Offline
    freecamellia
    wrote on last edited by
    #1

    When I try to use the selectedIndexes() method of the QListview to retrieve the QModelIndexList of selected items, this error is generated: 'virtual QModelIndexList QListView::selectedIndexes() const' is protected

    I addition, I want retrieve the one only selected item index.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Use this:

      @
      QModelIndexList selected = listView->selectionModel()->selectedIndexes();
      @

      protected methods are for internal use only.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      1
      • F Offline
        F Offline
        freecamellia
        wrote on last edited by
        #3

        To edit a selected record in a tableModel, why this code don't work!

        @QModelIndexList i = ui->listView->selectionModel()->selectedIndexes(); // retrieve selected item indexes
        QModelIndex ii = i.at(0); // I need the one only selected item index
        QModelIndex iii = proxyModel->mapToSource(ii); // corresponding index in the tableModel
        QSqlRecord r = tableModel->record(iii.row()); // return the needed record to edit
        r.setValue(1 ,textToPutInTheField1); // edit the record@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          First of all, try to use more meaningful variable names. It is a bad idea to use i, ii, iii. It really isn't that much work to type a meaningful name, and it will make understanding your code by others and by yourself later on much easier.

          Then, the question is, what does not work exactly? Do you get an error? What was the result you expected, and what happened instead?
          It seems to me that you are trying to modify a value. This is not the way to do it. You get a QSqlRecord, but that record is just a value object. If you change values in it, these are not magically written to the underlying database, nor even to the QSqlTableModel itself. If you want to change the data in a QSqlTableModel, I suggest you use the setData() method. That would also get rid of much of the rest of your code, as chances are, your translation back to the source model index are not needed.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            Can you define "doesn't work"?

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • F Offline
              F Offline
              freecamellia
              wrote on last edited by
              #6

              but to edit a record in the tableModel, the concerning record index should be retrieved first using his coresponding proxyModel index. Without the mapToSource() method how can I do this.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                freecamellia
                wrote on last edited by
                #7

                [quote author="Volker" date="1323779496"]Can you define "doesn't work"?[/quote]

                the concerning record is not edited in the database nor in the tableModel.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  [quote author="freecamellia" date="1323780138"]but to edit a record in the tableModel, the concerning record index should be retrieved first using his coresponding proxyModel index. Without the mapToSource() method how can I do this.[/quote]
                  You simply do the edit directly on the proxyModel. The proxyModel's implementation will then do the edit on the corresponding item in the sourceModel (which may also be a proxy model itself, etc.).

                  [quote author="freecamellia" date="1323780316"]
                  [quote author="Volker" date="1323779496"]Can you define "doesn't work"?[/quote]

                  the concerning record is not edited in the database nor in the tableModel.[/quote]
                  I already explained the reasons why that happens in my posting above.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    freecamellia
                    wrote on last edited by
                    #9

                    I don't know who to use setData() in this case, so I conserve the mapToSource() because I find working with records in the tableModel is more intuitive than indexes in the proxyModel.

                    With this code the record is not edited, but a new record is created with the same information except the field 1 = "anything"

                    @QModelIndexList indexList = ui->termListView->selectionModel()->selectedIndexes();
                    QModelIndex proxyIndex = indexList.at(0);
                    QModelIndex tableIndex = proxyModel->mapToSource(proxyIndex);
                    QSqlRecord r = tableModel->record(tableIndex.row());
                    r.setValue(1 ,"anything");
                    tableModel->insertRecord(tableIndex.row(), r);@

                    what I do now?

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      giesbert
                      wrote on last edited by
                      #10

                      insertRecord does what it's name says, it inserts a record, not edits one.

                      "from the docs:":http://doc.qt.nokia.com/4.7/qsqltablemodel.html#insertRecord
                      [quote]
                      Inserts the record after row. If row is negative, the record will be appended to the end. Calls insertRows() and setRecord() internally.
                      Returns true if the row could be inserted, otherwise false.
                      [/quote]

                      Perhaps you were looking for: "setRecord":http://doc.qt.nokia.com/4.7/qsqltablemodel.html#setRecord ?

                      Nokia Certified Qt Specialist.
                      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                      1 Reply Last reply
                      0

                      • Login

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