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. Select a Single Column in QTableView
QtWS25 Last Chance

Select a Single Column in QTableView

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 11.7k 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.
  • D Offline
    D Offline
    deepakkakkeel
    wrote on last edited by
    #1

    Hi,

    I have a QTableView with a QSelectionModel set on the QTableVoew.
    May i know how do i select a particular column only. If i select column number 1 , I should not be allowed to select column number 2. That is at a time more than one column cannot be selected.

    Thanks
    Deepak

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      i haven't tried this, but give it a shot:
      @
      tableView->setSelectionBehavior(QAbstractItemView::SelectColumns);
      tableView->setSelectionMode(QAbstractItemView::SingleSelection);
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deepakkakkeel
        wrote on last edited by
        #3

        Thanks.
        But tableView->setSelectionBehavior(QAbstractItemView::SelectColumns); selects the entire column i donot want to select the entire column. I just wanted to drag and select only needed elements.
        May i know how to do it?

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          so you just want to select single cells or do you want to exclude certain cells from a selected row?

          for first case just use:
          @
          tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deepakkakkeel
            wrote on last edited by
            #5

            I just wanted to select some values from a single column.
            If i selected some values from a column i should not be allowed to select values from another column. That is values from more than one column should not be allowed to select at the same time.

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              sorry i don't understand you ... i have no idea what you exactly want.
              Maybe you can create some kind of graphic which illustrates what you want?

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Skh1002
                wrote on last edited by
                #7

                Do you want to let the user select an entire column or just (possibly multiple) individual cells in it? If it is individual cells, then you should detect if a user clicks on a cell in a different column. To do that, I would probably try subclassing QTableView and reimplementing selectionChanged() or track mouse clicks by connecting to the clicked() signal. But, as others pointed out, it would help if you provided more information.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  deepakkakkeel
                  wrote on last edited by
                  #8

                  Exactly i dont want to select entire column. Just multiple individaual cells in a column. If i select some cells from column number 1 and along with that i try to select cells from column number 2 it should not be allowed.

                  1 Reply Last reply
                  0
                  • raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    is there any pattern in your selection logic? for example that every row can only contain 1 selected col/cell?

                    i would go for subclassing the model and reimplement "QAbstractItemModel::flags()":http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html#flags
                    @
                    Qt::ItemFlags MyModel::flags(const QModelIndex ) const
                    {
                    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
                    //logic to determine if this cell/index should be selectable
                    if( ! selectable )
                    flags &= ~Qt::ItemIsSelectable;

                    return flags;
                    }
                    @

                    Note: the forum hast problems displaying the "tilde-sign":http://en.wikipedia.org/wiki/Tilde. The minus sign in "flags &= ~Qt::ItemIsSelectable" should be a tilde.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      It seems to me that you'll need to subclass [[doc:QItemSelectionModel]], and use that custom item selection model on your view. Reimplementing the virtual methods will give you full control over what gets selected and what doesn't.

                      Alternatively, you can approach it from the model-side of things: just return the right item flags for cells you don't want to be selectable.

                      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