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. QTableview editing problem using proxy model
Forum Updated to NodeBB v4.3 + New Features

QTableview editing problem using proxy model

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.8k 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.
  • C Offline
    C Offline
    cdontheqt
    wrote on last edited by
    #1

    Sorry, I am new to Qt and trying to implement a Qtableview/Database program. I used this code for proxy model but at the end it says that the QComboboxes are replaced by TextEdits when editing. There is a link to a fix but it is broken. Anyone know a fix for this? or know how to fix it by any chance??
    http://wiki.qt.io/QSortFilterProxyModel_subclass_for_text_alignment_-and_readonly_columns
    Thanks in advance.

    raven-worxR 1 Reply Last reply
    0
    • C cdontheqt

      Sorry, I am new to Qt and trying to implement a Qtableview/Database program. I used this code for proxy model but at the end it says that the QComboboxes are replaced by TextEdits when editing. There is a link to a fix but it is broken. Anyone know a fix for this? or know how to fix it by any chance??
      http://wiki.qt.io/QSortFilterProxyModel_subclass_for_text_alignment_-and_readonly_columns
      Thanks in advance.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @cdontheqt
      for what types in the DB are now text-edit widgets displayed instead of combo-boxes? Boolean values?

      But nevertheless. A model shouldn't be responsible for the display of the data. The only thing what can influence this is, by returning a different QVariant type in the model's data() method.

      Also what type of item delegate are you using in your table view?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • C Offline
        C Offline
        cdontheqt
        wrote on last edited by
        #3

        Apologies, I am a noob!
        All my types are QStrings.
        I'm using ..tableView->setItemDelegate(new QSqlRelationalDelegate(this));
        This works perfectly and adds combobox which automatically fills the values. When I use the proxy model the comboboxes are replaced with lineEdits and anything can be typed in which I don't want.
        I think I will try to use a custom delegate but the problem I see with that is that I would have to implement the values in the combobox manually, and I know that the standard delegate already does this. Does that make sense or am I not understanding something basic? Thanks

        raven-worxR 1 Reply Last reply
        0
        • C cdontheqt

          Apologies, I am a noob!
          All my types are QStrings.
          I'm using ..tableView->setItemDelegate(new QSqlRelationalDelegate(this));
          This works perfectly and adds combobox which automatically fills the values. When I use the proxy model the comboboxes are replaced with lineEdits and anything can be typed in which I don't want.
          I think I will try to use a custom delegate but the problem I see with that is that I would have to implement the values in the combobox manually, and I know that the standard delegate already does this. Does that make sense or am I not understanding something basic? Thanks

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @cdontheqt
          as i said, you actually just need to make sure that the model's data() method returns the correct QVariant types for the columns.
          Since you are using a proxy model (which normally just forwards the calls mapped to the source model) and a QSqlQueryModel(??) this should already work. I would expect that QSqlQueryModel returns the QVariant types corresponding to the database type (from the used sql plugin). But to be honest i am not sure right now.

          Alternatively you can reimplement a custom delegate or try (in your proxymodel) to intercept the QVariant returned and convert it to a bool variant, or whatever.

          Creating a combo-box for booleans works with the default delegate out of the box. If you want a combo-box for a string list with predefined strings you need to create a custom delegate subclass.
          For example this might be necessary to show a combobox for columns containing foreign keys. Then you need to create a combobox in your custom delegate's createEditor() method for the specific column. This combobox then has it's own sql model only fetching the ids from the table the foreign key refers to.

          Hope this clears things up a bit?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • C Offline
            C Offline
            cdontheqt
            wrote on last edited by
            #5

            Thanks for replies. I tried a custom delegate and this seems to work. I haven't done extensive testing yet. I just reimplemented the creatEditor method as below.

            QWidget *CustomDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const
            {
            if ((index.column() != ID_INDEX) && (index.column() != DATE_ADDED_INDEX))
            {
            QSqlRelationalDelegate::createEditor(parent,option,index);
            }
            }

            Thanks again for help.

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

              Hi and welcome to devnet,

              Aren't missing a return statement in your createEditor implementation in the case the column is ID_INDEX or DATE_ADDED_INDEX ?

              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
              0

              • Login

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