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. Copy selected rows of a table to a model
Forum Updated to NodeBB v4.3 + New Features

Copy selected rows of a table to a model

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 6.5k 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.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by Panoss
    #7

    I create a QStandardItem . How do I insert it in selected_rows_model?

    QItemSelectionModel *selectionModel = ui.devices_finder_tbl->selectionModel();
    
    QModelIndexList list = selectionModel->selectedRows();
    
    for(int i = 0; i < list.size(); i++)
    {
      QModelIndex index = list.at(i);
      QStandardItem  *qsti = new QStandardItem(index.data().toString());
      selected_rows_model->???
    }
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #8

      use setItem(int row, int column, QStandardItem *item) method.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      3
      • P Offline
        P Offline
        Panoss
        wrote on last edited by Panoss
        #9

        As row I use the rowCount().

        row = selected_rows_model->rowCount();
        selected_rows_model->setItem(row, column, qsti);
        

        But as column? What should I use?

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

          Again, you don't need a new model, you need a proxy. While I still suggest KSelectionProxyModel (it's super easy to build with cmake and link) an alternative way is:

          const int selectedRole = Qt::UserRole + 124;
          connect(ui.devices_finder_tbl->selectionModel(),&QItemSelectionModel::selectionChanged,[=](const QItemSelection &selected, const QItemSelection &deselected)->void{
          const QModelIndexList selectedIdx = selected.indexes();
          const QModelIndexList deselectedIdx = deselected.indexes();
          for(const QModelIndex& singleIdx : selectedIdx)
          ui.devices_finder_tbl->model()->setData(ui.devices_finder_tbl->model()->index(singleIdx.row(),0,singleIdx.parent()),1,selectedRole);
          for(const QModelIndex& singleIdx : deselectedIdx )
          ui.devices_finder_tbl->model()->setData(ui.devices_finder_tbl->model()->index(singleIdx.row(),0,singleIdx.parent()),QVariant(),selectedRole);
          });
          
          QSortFilterProxyModel* selected_rows_model=new QSortFilterProxyModel(this);
          selected_rows_model->setFilterRole(selectedRole);
          selected_rows_model->setFilterFixedString("1");
          selected_rows_model->setsourceModel(ui.devices_finder_tbl->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

          P 1 Reply Last reply
          1
          • VRoninV VRonin

            Again, you don't need a new model, you need a proxy. While I still suggest KSelectionProxyModel (it's super easy to build with cmake and link) an alternative way is:

            const int selectedRole = Qt::UserRole + 124;
            connect(ui.devices_finder_tbl->selectionModel(),&QItemSelectionModel::selectionChanged,[=](const QItemSelection &selected, const QItemSelection &deselected)->void{
            const QModelIndexList selectedIdx = selected.indexes();
            const QModelIndexList deselectedIdx = deselected.indexes();
            for(const QModelIndex& singleIdx : selectedIdx)
            ui.devices_finder_tbl->model()->setData(ui.devices_finder_tbl->model()->index(singleIdx.row(),0,singleIdx.parent()),1,selectedRole);
            for(const QModelIndex& singleIdx : deselectedIdx )
            ui.devices_finder_tbl->model()->setData(ui.devices_finder_tbl->model()->index(singleIdx.row(),0,singleIdx.parent()),QVariant(),selectedRole);
            });
            
            QSortFilterProxyModel* selected_rows_model=new QSortFilterProxyModel(this);
            selected_rows_model->setFilterRole(selectedRole);
            selected_rows_model->setFilterFixedString("1");
            selected_rows_model->setsourceModel(ui.devices_finder_tbl->model());
            
            P Offline
            P Offline
            Panoss
            wrote on last edited by Panoss
            #11

            @VRonin said in Copy selected rows of a table to a model:

            Again, you don't need a new model, you need a proxy. While I still suggest KSelectionProxyModel

            I tried it,but I had to include KSelectionProxyModel.h, KSelectionProxyModel.cpp, kdemacros.h etc etc. (and find - create these files)
            Every time I added a file, ten more were needed.
            I 'll try your solution.

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

              For reference, to build the module containing KSelectionProxyModel on windows with MSVC:

              1. install git
              2. install cmake
              3. clone the repository git://anongit.kde.org/kitemmodels.git
              4. open the developer console (qtenv2.bat and vcvarsall.bat)
              5. run
              set KDELIBPATH = C:\KDE
              mkdir build
              cd build
              cmake -G "NMake Makefiles" -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_INSTALL_PREFIX=%KDELIBPATH% -DCMAKE_BUILD_TYPE=DEBUG ../
              cmake --build .
              cmake --build . --target install
              cmake -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX=%KDELIBPATH% -DCMAKE_BUILD_TYPE=RELEASE ../
              cmake --build .
              cmake --build . --target install
              

              this will install the module in C:\KDE

              to link it, in your project add to the .pro file:

              LIBS += -LC:/KDE/lib
              INCLUDEPATH +=  C:/KDE/include/KF5/KItemModels
              CONFIG(release, debug|release) {
              LIBS += -lKF5ItemModels
              }
              CONFIG(debug, debug|release) {
              LIBS += -lKF5ItemModelsd
              }
              

              to install in other platforms/compilers you'll need to change the cmake generator (the part after -G) a list can be found here: https://cmake.org/cmake/help/v3.0/manual/cmake-generators.7.html

              "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
              • P Offline
                P Offline
                Panoss
                wrote on last edited by
                #13

                VRonin thank you, but I 'd rather live without KDE :).
                I think it's too much trouble for something very simple.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Panoss
                  wrote on last edited by
                  #14

                  I get an error:

                  error: C2143: syntax error : missing ',' before ':'
                  

                  at line

                  for(const QModelIndex &singleIdx : selectedIdx)
                  
                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #15

                    try with foreach(const QModelIndex &singleIdx , selectedIdx)

                    VRonin thank you, but I 'd rather live without KDE :).
                    I think it's too much trouble for something very simple.

                    We agree to disagree then

                    "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
                    • P Offline
                      P Offline
                      Panoss
                      wrote on last edited by Panoss
                      #16

                      @VRonin I decided to follow your advice :) and try KDE:

                      1. I installed git in G:\Program Files\Git.
                      2. I installed cmake in G:\Program Files\CMake (and I put the G:\Program Files\CMake\bin in the PATH variable).
                      3. I cloned the repository git://anongit.kde.org/kitemmodels.git in G:/kitemmodels
                      4. I opened the developer console (qtenv2.bat )
                        Here, I don't understand what you mean by "and vcvarsall.bat", isn't my developer console the:
                        G:\Qt\Qt5.0.2\5.0.2\msvc2010\bin\qtenv2.bat?
                      5. I run (in developer console, my drive letter is G:)
                      set KDELIBPATH = G:\KDE
                      mkdir build
                      cd build
                      cmake -G "NMake Makefiles" -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_INSTALL_PREFIX=%KDELIBPATH% -DCMAKE_BUILD_TYPE=DEBUG ../
                      

                      Here I get the error: "CMAke error: the source directory G:/Qt/Qt5.0.2/5.0.2/msvc2010 does not appear to contain CMakeLists.txt".

                      VRoninV 1 Reply Last reply
                      0
                      • P Panoss

                        @VRonin I decided to follow your advice :) and try KDE:

                        1. I installed git in G:\Program Files\Git.
                        2. I installed cmake in G:\Program Files\CMake (and I put the G:\Program Files\CMake\bin in the PATH variable).
                        3. I cloned the repository git://anongit.kde.org/kitemmodels.git in G:/kitemmodels
                        4. I opened the developer console (qtenv2.bat )
                          Here, I don't understand what you mean by "and vcvarsall.bat", isn't my developer console the:
                          G:\Qt\Qt5.0.2\5.0.2\msvc2010\bin\qtenv2.bat?
                        5. I run (in developer console, my drive letter is G:)
                        set KDELIBPATH = G:\KDE
                        mkdir build
                        cd build
                        cmake -G "NMake Makefiles" -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_INSTALL_PREFIX=%KDELIBPATH% -DCMAKE_BUILD_TYPE=DEBUG ../
                        

                        Here I get the error: "CMAke error: the source directory G:/Qt/Qt5.0.2/5.0.2/msvc2010 does not appear to contain CMakeLists.txt".

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #17
                        1. I installed git in G:\Program Files\Git.
                        2. I installed cmake in G:\Program Files\CMake (and I put the G:\Program Files\CMake\bin in the PATH variable).
                        3. I cloned the repository git://anongit.kde.org/kitemmodels.git in G:/kitemmodels
                        4. I opened the developer console (qtenv2.bat )

                        All good

                        Here, I don't understand what you mean by "and vcvarsall.bat", isn't my developer console the:
                        G:\Qt\Qt5.0.2\5.0.2\msvc2010\bin\qtenv2.bat?

                        What compiler are you using? my guide is for MSVC, when you call qtenv2.bat for an MSVC compiler it prints out a reminder "Don't forget to run vcvarsall", that is the compiler environment setup, for MSVC2013 it's somewhere looking like C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat here you have pass an extra argument to determine the platform, for example C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat x86 or C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat amd64

                        1. I run (in developer console, my drive letter is G:)
                        set KDELIBPATH = G:\KDE
                        mkdir build
                        cd build
                        cmake -G "NMake Makefiles" -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_INSTALL_PREFIX=%KDELIBPATH% -DCMAKE_BUILD_TYPE=DEBUG ../
                        

                        Here I get the error: "CMAke error: the source directory G:/Qt/Qt5.0.2/5.0.2/msvc2010 does not appear to contain CMakeLists.txt".

                        you just are in the wrong folder, use cd G:\kitemmodels

                        "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

                        • Login

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