Results 1 to 5 of 5

Thread: How to find if a specific row is included in a selection?

  1. #1
    Join Date
    Sep 2011
    Posts
    24
    Qt products
    Qt4
    Platforms
    Windows

    Default How to find if a specific row is included in a selection?

    Hi,

    I'm new to Qt programming and struggling with QTableView to do a custom selection. What I need to achieve is enabling some buttons depending on how rows are selected.

    I've set selectionBehavior to SelectItems & selectionMode to ContiguousSelection. This is to easily find if a row is selected.

    I'm using the selectionChanged SIGNAL to get the current selection and use following code fragment to get the list of selected rows.

    //Initial setup - 3 columns, 3 empty rows

    p_TableFields = new QStandardItemModel(this);

    p_TableFields->insertColumns(0, 3);
    p_TableFields->insertRows(0, 3);

    p_TableFields->setHeaderData(0, Qt::Horizontal, tr("Field Name"));
    p_TableFields->setHeaderData(1, Qt::Horizontal, tr("Data Type"));
    p_TableFields->setHeaderData(2, Qt::Horizontal, tr("Description"));

    tblFields->setModel(p_TableFields);

    connect(tblFields->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(OnSelect(const QItemSelection&, const QItemSelection&)));

    //OnSelect
    QModelIndexList RowsSelected = tblFields->selectionModel()->selectedRows();


    Now the question is how can find if the first row is included in the selection? This is basically to decide on enabling a Move Rows Up button only if first row is not selected.

    Similar logic need to implement for Move Rows Down button if last row is not selected.

    Table.png

  2. #2
    Join Date
    Aug 2011
    Posts
    44
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows Symbian S60

    Default Re: How to find if a specific row is included in a selection?

    QModelIndexList RowsSelected = tblFields->selectionModel()->selectedRows();

    Now the question is how can find if the first row is included in the selection? This is basically to decide on enabling a Move Rows Up button only if first row is not selected.
    Do you mean the first row of the table?
    If so you could just iterate RowsSelected to determine whether the index with row()==0 is in the list.
    Doesn't it work for you?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,555
    Thanks
    13
    Thanked 1,581 Times in 1,510 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to find if a specific row is included in a selection?

    The selectionChanged() signal gives you the new QItemSelection. If you have set the view to select rows then you can see if the first/last row is in the selection with by using QItemSelection::contains() with the QModelIndex of the first/last row and a visible column. If you allow selecting arbitrary items then you would have to check each item in the first/last row or you could also grab the selectionModel() of the view and use: QItemSelectionModel::isRowSelected().

    You need to be careful to handle the case when both the first and last row are in the selection or when the view is empty.

  4. #4
    Join Date
    Sep 2011
    Posts
    24
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find if a specific row is included in a selection?

    Hi llev,

    Actually I'm looking for a method to check if first/last row in the table is included within a selection. Above code fragment is used for get the list of rows which are entirely selected.

    Is there any function to check whether first row is selected without iterating the list?


    Added after 16 minutes:


    Hi Chris,

    I'm using the selection as you mentioned. I never allow arbitrary selections. As I'm new to Qt I need to know how can I get the QModelIndex of the first/last row to be passed on QItemSelection::contains( const QModelIndex & index ) ???
    Last edited by IndikaU; 14th September 2011 at 03:36.

  5. #5
    Join Date
    Sep 2011
    Posts
    24
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to find if a specific row is included in a selection?

    Hi,

    I could manage to achieve what I expected. I'm putting the code here.

    //Initial setup - 3 columns, 3 empty rows

    p_TableFields = new QStandardItemModel(this);

    p_TableFields->insertColumns(0, 3);
    p_TableFields->insertRows(0, 3);

    p_TableFields->setHeaderData(0, Qt::Horizontal, tr("Field Name"));
    p_TableFields->setHeaderData(1, Qt::Horizontal, tr("Data Type"));
    p_TableFields->setHeaderData(2, Qt::Horizontal, tr("Description"));

    tblFields->setModel(p_TableFields);

    connect(tblFields->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(OnSelect(const QItemSelection&, const QItemSelection&)));

    //OnSelect
    QModelIndexList RowsSelected = tblFields->selectionModel()->selectedRows();

    //Check if first row selected
    QModelIndex firstElement = p_TableFields->index(0, 0);
    bool hasFirstRow = RowsSelected.contains(firstElement);

    //Check if last row selected
    QModelIndex lastElement = p_TableFields->index((p_TableFields->rowCount()-1), 0);
    bool hasLastRow = RowsSelected.contains(lastElement);

    btnMoveUp->setEnabled(!hasFirstRow);
    btnMoveDown->setEnabled(!hasLastRow);


    FirstRowSelected.pngLastRowSelected.png

Similar Threads

  1. Replies: 5
    Last Post: 14th February 2011, 15:06
  2. Replies: 0
    Last Post: 2nd February 2010, 05:38
  3. header files not getting included
    By nimmyj in forum Installation and Deployment
    Replies: 1
    Last Post: 19th December 2006, 07:18
  4. Function ioperm() is included into Qt4
    By rud_1023 in forum Qt Programming
    Replies: 2
    Last Post: 26th October 2006, 11:41
  5. math.h included, but fmod not available
    By jamadagni in forum General Programming
    Replies: 1
    Last Post: 8th January 2006, 07:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.