Results 1 to 5 of 5

Thread: QTableWidget special selectionbehavior

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2011
    Posts
    195
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    49
    Thanked 4 Times in 4 Posts

    Default QTableWidget special selectionbehavior

    Hi,
    I have a QTableWidget with i.e 6 columns. One Dataobject consists of columns. So there are 2 Dataobjects and every object has 3 columns.
    Now I need I special selection behaviour for my Table. If the user clicks on an item at column 0 and some row, the items of column 0 to 3 at this row should get selsected. The selectionbehavior should be extended selection.

    I tried lots of solutions with the QItemSelectionModel, but I didn't get what I want:
    My best attempt was following:
    Qt Code:
    1. void MyTableWidget::sel_slot(QItemSelection selected, QItemSelection deselected)
    2. {
    3. qDebug() << "MyTableWidget::sel_slot() called from signal: QTableWidget::itemSelectionChanged()";
    4. qDebug() << "selected: " << QString::number(selected.indexes().size()) << " deselected: " << QString::number(deselected.indexes().size());
    5. QModelIndexList sel_indexes = selected.indexes();
    6. QModelIndex index;
    7. foreach(index,sel_indexes){
    8. int row_num = index.row();
    9. int col_num = index.column();
    10.  
    11. int range1, range2;
    12. if(col_num <= 2){
    13. range1 = 0;
    14. range2 = 2;
    15. }
    16. else if(col_num <= 5){
    17. range1 = 3;
    18. range2 = 5;
    19. }
    20.  
    21. QModelIndex topleft = indexFromItem(item(row_num,range1));
    22. QModelIndex bottomright = indexFromItem(item(row_num,range2));
    23. QItemSelectionModel* model = this->selectionModel();
    24. QItemSelection item_sel = model->selection();
    25. QItemSelection n_itemsel(topleft,bottomright);
    26. item_sel.merge(n_itemsel,QItemSelectionModel::Select);
    27. model->select(item_sel,QItemSelectionModel::Select);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    It works nearly, but only if ctrl is pressed. What should I do with the indexes of deselected? If I do iterate over the deselected-indexes and deselect them, it doesn't work. Maybe somebody can help me....Thanks

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 106 Times in 103 Posts

    Default Re: QTableWidget special selectionbehavior

    If I understood you right, you have 6 columns and you want to select whole rows by clicking on any item but only for columns 0-3 and leave 4&5 unselected?
    Is that constant or selected columns change? Does click on the item in column 4 or 5 should select the row as well?

    If this odd behaviour doesn't change you can split the table into two views. In first show columns 0-3, in second show columns 4&5.
    Use QAbstractItemView::SelectRows in the first column and QAbstractItemView::NoSelection in the second.
    Connect scroll signals in both views to synchronize scrolling and you're good to go.

  3. #3
    Join Date
    Apr 2011
    Posts
    195
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    49
    Thanked 4 Times in 4 Posts

    Default Re: QTableWidget special selectionbehavior

    Thank u for your return.
    Sorry 0-3 is incorrect(my fault). The table have 6 columns. If the user click on an item at row(0 to 2) the row should be selected at column 0 to 2. If the user clicks an item(3 - 5) the row should be selected at column 3 - 5.
    The selection should be extended selection. You say I should use 2 tableviews? So I cannot use QTableWidget...

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 106 Times in 103 Posts

    Default Re: QTableWidget special selectionbehavior

    You can use two QTableWidget(s) without any problems.

    Just set Qt::NoFrame on the widgets so they will look like one large table and put them in a layout with spacing set to 0.

  5. The following user says thank you to Spitfire for this useful post:

    Qiieha (14th March 2012)

  6. #5
    Join Date
    Apr 2011
    Posts
    195
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    49
    Thanked 4 Times in 4 Posts

    Default Re: QTableWidget special selectionbehavior

    Ok, thank u,
    I did something similar in past, because I needed a sortable frozen column for QTableWidget.
    I will try it...

Similar Threads

  1. special characters in xml
    By khcbabu in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 22:10
  2. Using special characters in QT dialogs
    By hvengel in forum Qt Tools
    Replies: 2
    Last Post: 10th April 2007, 00:32
  3. Special Dialog
    By avis_phoenix in forum Qt Programming
    Replies: 2
    Last Post: 29th September 2006, 06:05
  4. Special Widget
    By iGoo in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2006, 09:21
  5. Any special .dll for images ?
    By probine in forum Installation and Deployment
    Replies: 4
    Last Post: 5th May 2006, 13:04

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
  •  
Qt is a trademark of The Qt Company.