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. Updating widget with QSqlTableModel
Qt 6.11 is out! See what's new in the release blog

Updating widget with QSqlTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 570 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.
  • H Offline
    H Offline
    Harsaith
    wrote on last edited by
    #1

    Hello,

    I have a database that look like this : db.png

    And my goal is to make a custom widget out of each row of the database
    draw.png

    For that I decided to use a QSqlTableModel and for each custom widget that I make, create a QWidgetMapper in whose purpose is to map a specific row to the QLabels of my custom widget like so

    
     m_dataMapper->addMapping(m_descriptionLabel, model->fieldIndex("description"), "text");
    m_dataMapper->addMapping(m_tagLabel, model->fieldIndex("tag"), "text");
    m_dataMapper->setCurrentIndex(row);
    

    In my application, I allow the user to modify those label using QDialog, but one thing I am not able to do is update the database using my model and force it to update automatically the QLabels of my custom widget. Should I use a delegate ? What am I doing wrong

    VRoninV 1 Reply Last reply
    0
    • H Harsaith

      Hello,

      I have a database that look like this : db.png

      And my goal is to make a custom widget out of each row of the database
      draw.png

      For that I decided to use a QSqlTableModel and for each custom widget that I make, create a QWidgetMapper in whose purpose is to map a specific row to the QLabels of my custom widget like so

      
       m_dataMapper->addMapping(m_descriptionLabel, model->fieldIndex("description"), "text");
      m_dataMapper->addMapping(m_tagLabel, model->fieldIndex("tag"), "text");
      m_dataMapper->setCurrentIndex(row);
      

      In my application, I allow the user to modify those label using QDialog, but one thing I am not able to do is update the database using my model and force it to update automatically the QLabels of my custom widget. Should I use a delegate ? What am I doing wrong

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @Harsaith said in Updating widget with QSqlTableModel:

      Should I use a delegate ?

      1000 times yes

      "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

      H 1 Reply Last reply
      3
      • VRoninV VRonin

        @Harsaith said in Updating widget with QSqlTableModel:

        Should I use a delegate ?

        1000 times yes

        H Offline
        H Offline
        Harsaith
        wrote on last edited by
        #3

        @VRonin
        But the delegate will now update the widgets unless and told force him to call setText() on the QLabels even though those qlabels are mapped to the model, am I correct ?

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

          A delegate is not a widget. It paints itself using a paint() method that gets data directly from the model

          "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
          5

          • Login

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