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
QtWS25 Last Chance

Copy selected rows of a table to a model

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 3 Posters 6.1k Views
  • 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 23 Feb 2017, 08:01 last edited by
    #3

    I don't know how to use it.
    #include <kselectionproxymodel.h> I guess is not enough.
    Should I download - install something?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Panoss
      wrote on 23 Feb 2017, 08:31 last edited by
      #4

      Isn't there some other way without having to use an external library?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dheerendra
        Qt Champions 2022
        wrote on 23 Feb 2017, 08:37 last edited by
        #5

        QModelIndex are transient and don't try to use them in some other model. Problem may be because you are inserting the same index. It is better you fetch the value from the model and build items and then insert the same in other model.

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

        1 Reply Last reply
        3
        • P Offline
          P Offline
          Panoss
          wrote on 23 Feb 2017, 08:51 last edited by Panoss
          #6
          This post is deleted!
          1 Reply Last reply
          0
          • P Offline
            P Offline
            Panoss
            wrote on 23 Feb 2017, 08:57 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
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 23 Feb 2017, 09:26 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 23 Feb 2017, 09:34 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
                • V Offline
                  V Offline
                  VRonin
                  wrote on 23 Feb 2017, 10:08 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 23 Feb 2017, 10:23
                  1
                  • V VRonin
                    23 Feb 2017, 10:08

                    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 23 Feb 2017, 10:23 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
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 23 Feb 2017, 10:39 last edited by VRonin 9 Mar 2018, 09:40
                      #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 23 Feb 2017, 10:47 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 23 Feb 2017, 10:49 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
                          • V Offline
                            V Offline
                            VRonin
                            wrote on 23 Feb 2017, 10:54 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 25 Feb 2017, 08:58 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".

                              V 1 Reply Last reply 27 Feb 2017, 08:11
                              0
                              • P Panoss
                                25 Feb 2017, 08:58

                                @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".

                                V Offline
                                V Offline
                                VRonin
                                wrote on 27 Feb 2017, 08:11 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

                                12/17

                                23 Feb 2017, 10:39

                                • Login

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