Copy selected rows of a table to a model
-
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.
-
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.
-
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());
-
@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. -
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 -
@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".
-
- 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