Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. best practices: I'm doing it wrong
Forum Updated to NodeBB v4.3 + New Features

best practices: I'm doing it wrong

Scheduled Pinned Locked Moved Solved Brainstorm
40 Posts 7 Posters 11.6k Views 4 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #29

    Based on input from a few people here, I've decided to leave the details in the main display disabled. The user will press an "edit" button which will create a QDialog with the details enabled for editing.

    It's not a huge deal, but I will be performing redundant work if I just re-create the display elements for the details in the QDialog. Does it make sense to create an aggregate display element with all the details, and use this element in both the main and edit windows? Can .ui files be nested?

    1 Reply Last reply
    0
    • mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #30

      I've created an edit dialog, but I'm not able to map the individual elements to the model as I did in the main widget. I create the edit dialog like this:

      DeviceModel *m_d;
      ...
      void Widget::on_pushButtonEdit_clicked()
      {
          EditDialog editDialog(m_d);
          QObject::connect(&editDialog, &EditDialog::editCommitPressed, this, &Widget::sendCommit);
          editDialog.exec();
      }
      

      And I try to map to the model like this (seemingly identical to how I do it in the main widget):

      EditDialog::EditDialog(DeviceModel *d, QWidget *parent) :
          QDialog(parent),
          ui(new Ui::EditDialog)
      {
          ui->setupUi(this);
          m_mapper = new QDataWidgetMapper(this);
          m_mapper->setModel(d->getModel());
      
          m_mapper->addMapping(ui->deviceName, TAG_DEVICENAME);
      

      The mapping works in the main widget, but not for my edit dialog. Any ideas what I'm doing wrong? Thanks.

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

        Did you forget to connect to setCurrentModelIndex?

        "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
        1
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #32

          Well...maybe.

          In my main widget, I have this line:

              QObject::connect(ui->tableView->selectionModel(), &QItemSelectionModel::currentRowChanged,
                               m_mapper, &QDataWidgetMapper::setCurrentModelIndex);
          

          Wouldn't that be sufficient for anything using the same model?

          VRoninV 1 Reply Last reply
          0
          • mzimmersM mzimmers

            Well...maybe.

            In my main widget, I have this line:

                QObject::connect(ui->tableView->selectionModel(), &QItemSelectionModel::currentRowChanged,
                                 m_mapper, &QDataWidgetMapper::setCurrentModelIndex);
            

            Wouldn't that be sufficient for anything using the same model?

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

            @mzimmers said in best practices: I'm doing it wrong:

            for anything using the same model

            The connect uses the selection of the view and the mapper so has nothing to do with the model.
            If either the view or the m_mapper are different objects from those in the main window then you have to redo the connection

            "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
            0
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #34

              Oh, OK, I think I see the problem: since my edit dialog doesn't have a QTableView, there's no way to select a row (no table --> no rows). I guess I can't just have the details mapped without some table to give it context, right?

              So, do I need an invisible table in my edit dialog, or is there some more clever way around this?

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

                No, you can also just straight call m_mapper->setCurrentModelIndex(index); where index is the index of the model you want to load without doing any connection

                "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
                0
                • mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #36

                  I'm getting a bad index back. Here's my c'tor:

                  EditDialog::EditDialog(DeviceModel *d, QWidget *parent) :
                      QDialog(parent),
                      ui(new Ui::EditDialog)
                  {
                      ui->setupUi(this);
                      m_mapper = new QDataWidgetMapper(this);
                      m_mapper->setModel(d->getModel());
                  
                      int i = m_mapper->currentIndex();
                      QModelIndex qmi = d->getModel()->index(i, 0);
                      m_mapper->setCurrentModelIndex(qmi);
                  

                  The variable i is set to (-1). What might I be doing wrong?

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

                    create a public method in the dialog:
                    void EditDialog::loadIndex(const QModelIndex& idx){m_mapper->setCurrentModelIndex(idx);}

                    After EditDialog editDialog(m_d);, add
                    editDialog.loadIndex(ui->tableView->selectionModel()->currentIndex());

                    "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

                    mzimmersM 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      create a public method in the dialog:
                      void EditDialog::loadIndex(const QModelIndex& idx){m_mapper->setCurrentModelIndex(idx);}

                      After EditDialog editDialog(m_d);, add
                      editDialog.loadIndex(ui->tableView->selectionModel()->currentIndex());

                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #38

                      @VRonin a couple questions, please:

                      1. EditDialog doesn't have a method setCurrentModelIndex(). Perhaps you meant:
                      QDataWidgetMapper *m_mapper;
                      ...
                      m_mapper.setCurrentModelIndex(ui->tableView->selectionModel()->currentIndex());
                      
                      1. How does loadIndex() get invoked?

                      Thanks...

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

                        Edited the answer above. editDialog.setCurrentModelIndex should have been editDialog.loadIndex sorry

                        "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
                        2
                        • mzimmersM Offline
                          mzimmersM Offline
                          mzimmers
                          wrote on last edited by
                          #40

                          Clever...very clever. So, to summarize:

                          1. it's generally a good idea to perform editing in a separate window/dialog.
                          2. create this window with new->Qt Designer Form Class; this create everything at once.
                          3. use the QDataWidgetMapper in the edit dialog to display the details from the model.
                          4. use VRonin's technique of setting the model index for the edit dialog by using the index from the parent window.

                          I'll edit the summary if anyone informs me of errata. Thanks for all the assistance.

                          1 Reply Last reply
                          1

                          • Login

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