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. QTableView spanned cells selection
Forum Updated to NodeBB v4.3 + New Features

QTableView spanned cells selection

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 629 Views 2 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.
  • A Offline
    A Offline
    Alexey Serebryakov
    wrote on last edited by Alexey Serebryakov
    #1

    Hi guys,
    let's me see small example
    53970f60-10d1-4a75-9d8e-17b8970c53e9-image.png

    #include <QApplication>
    #include <QTableView>
    #include <QLayout>
    #include <QStandardItemModel>
    #include <QHeaderView>
    
    int main (int argc, char **argv)
    {
        QApplication app (argc, argv);
    
        QWidget *window = new QWidget();
        QTableView *view = new QTableView();
    
        QStandardItemModel *model = new QStandardItemModel (4, 3, window);
    
        model->setData(model->index(0, 0), QVariant("A"));
        model->setData(model->index(1, 0), QVariant("B"));
        model->setData(model->index(2, 0), QVariant("C"));
        model->setData(model->index(3, 0), QVariant("D"));
    
        model->setData(model->index(0, 1), QVariant("a"));
        model->setData(model->index(1, 1), QVariant("b"));
        model->setData(model->index(2, 1), QVariant("c"));
        model->setData(model->index(3, 1), QVariant("d"));
    
        model->setData(model->index(0, 2), QVariant("1"));
        model->setData(model->index(1, 2), QVariant("1"));
        model->setData(model->index(2, 2), QVariant("3"));
        model->setData(model->index(3, 2), QVariant("4"));
    
        view->setSelectionBehavior(QAbstractItemView::SelectRows);
        view->setSelectionMode(QAbstractItemView::ExtendedSelection);
    
        view->setSpan(0, 2, 2, 1);
    
        view->setModel(model);
    
        QVBoxLayout *vblTop = new QVBoxLayout();
        vblTop->setMargin(5);
        vblTop->setSpacing(10);
        vblTop->setAlignment(Qt::AlignTop);
    
        vblTop->addWidget(view);
    
        window->setLayout(vblTop);
        window->show();
    
        return app.exec();
    
    }
    

    Please help me.
    I need when the user clicked on spanned cell then select ALL corresponding rows(cells).

    • If user clicked: A, a, 1, B, b (1 and 2 rows) make selection rows 1 and 2
      7f122226-ba95-45af-a9eb-a0afd40bb953-image.png

    • If user clicked C, c, 3 then select only 3 row, and if user clicked D, d, 4 then select only 4 row.

    1. Subclass QTableView? And handle selectedIndexes, selectionChanged, currentChanged?
    2. Change selection mode and behaviour?

    Thanks a lot,
    Alex.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      A custom QItemSelectionModel would likely be the way to go.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        A custom QItemSelectionModel would likely be the way to go.

        A Offline
        A Offline
        Alexey Serebryakov
        wrote on last edited by Alexey Serebryakov
        #3

        @SGaist seems to me that is complex way. I think there would be more simple. :-(
        I'm not found any references on how to subclass QItemSelectionModel.
        That is strange, I think that is very popular feature of QTableView widget. :-(

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          From your description, it seems you need to reimplement the select method.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          2
          • SGaistS SGaist

            From your description, it seems you need to reimplement the select method.

            A Offline
            A Offline
            Alexey Serebryakov
            wrote on last edited by
            #5

            @SGaist Oh thank you so much! That was good idea to subclass QItemSelectionModel. It works in my case.

            But I have notice I could not found any useful tutorials or samples about that feature. :-(

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You're welcome !

              Well, yours could become one :-)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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