Copy selected rows of a table to a model
-
wrote on 23 Feb 2017, 07:49 last edited by
What you want is a proxy model and it's already available from the KDE libraries (no, they don't work only on linux, they are multi platform): KSelectionProxyModel
-
wrote on 23 Feb 2017, 08:01 last edited by
I don't know how to use it.
#include <kselectionproxymodel.h> I guess is not enough.
Should I download - install something? -
wrote on 23 Feb 2017, 08:31 last edited by
Isn't there some other way without having to use an external library?
-
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.
-
wrote on 23 Feb 2017, 08:51 last edited by PanossThis post is deleted!
-
wrote on 23 Feb 2017, 08:57 last edited by Panoss
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->??? }
-
use setItem(int row, int column, QStandardItem *item) method.
-
wrote on 23 Feb 2017, 09:34 last edited by Panoss
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?
-
wrote on 23 Feb 2017, 10:08 last edited by 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());
-
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());
wrote on 23 Feb 2017, 10:23 last edited by Panoss@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. -
wrote on 23 Feb 2017, 10:39 last edited by VRonin 9 Mar 2018, 09:40
For reference, to build the module containing KSelectionProxyModel on windows with MSVC:
- install git
- install cmake
- clone the repository git://anongit.kde.org/kitemmodels.git
- open the developer console (qtenv2.bat and vcvarsall.bat)
- 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 -
wrote on 23 Feb 2017, 10:47 last edited by
VRonin thank you, but I 'd rather live without KDE :).
I think it's too much trouble for something very simple. -
wrote on 23 Feb 2017, 10:49 last edited by
I get an error:
error: C2143: syntax error : missing ',' before ':'
at line
for(const QModelIndex &singleIdx : selectedIdx)
-
wrote on 23 Feb 2017, 10:54 last edited by VRonin
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
-
wrote on 25 Feb 2017, 08:58 last edited by Panoss
@VRonin I decided to follow your advice :) and try KDE:
- I installed git in G:\Program Files\Git.
- I installed cmake in G:\Program Files\CMake (and I put the G:\Program Files\CMake\bin in the PATH variable).
- I cloned the repository git://anongit.kde.org/kitemmodels.git in G:/kitemmodels
- 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? - 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".
-
@VRonin I decided to follow your advice :) and try KDE:
- I installed git in G:\Program Files\Git.
- I installed cmake in G:\Program Files\CMake (and I put the G:\Program Files\CMake\bin in the PATH variable).
- I cloned the repository git://anongit.kde.org/kitemmodels.git in G:/kitemmodels
- 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? - 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".
wrote on 27 Feb 2017, 08:11 last edited by- I installed git in G:\Program Files\Git.
- I installed cmake in G:\Program Files\CMake (and I put the G:\Program Files\CMake\bin in the PATH variable).
- I cloned the repository git://anongit.kde.org/kitemmodels.git in G:/kitemmodels
- 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 exampleC:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat x86
orC:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat amd64
- 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
11/17