Copy selected rows of a table to a model



  • devices_finder_tbl is a QTableView.
    I want to make a new model containing only the selected rows of this table.
    Appending the selected rows to the empty model named "selected_rows_model" (QSqlTableModel).

        QItemSelectionModel *selectionModel = ui.devices_finder_tbl->selectionModel();
        if(selectionModel->hasSelection() ){       
                //QModelIndexList list = selectionModel->selectedRows();
                 QModelIndexList list = ui.devices_finder_tbl->selectedIndexes();
                for(int i = 0; i < list.size(); i++)
                {
                    QModelIndex index = list.at(i);
                    selected_rows_model->insertRow(selected_rows_model->rowCount(), index);
                }
    	}	
    
    

    But it's not working, this row:

    selected_rows_model->insertRow(selected_rows_model->rowCount(), index);
    

    is wrong.
    Ho can I fix it?



  • 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



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



  • 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.



  • This post is deleted!


  • 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.



  • 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?



  • 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:

    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 ../
    nmake
    nmake install
    cmake -G "NMake Makefiles" -DCMAKE_INSTALL_PREFIX=%KDELIBPATH% -DCMAKE_BUILD_TYPE=RELEASE ../
    nmake
    nmake 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 and replace nmake with your make command (make, mingw32-make, etc.)



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



  • I get an error:

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

    at line

    for(const QModelIndex &singleIdx : selectedIdx)
    


  • 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



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



    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.