Results 1 to 4 of 4

Thread: Selection in QTableWidget

  1. #1
    Join Date
    Apr 2008
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Selection in QTableWidget

    I like to color all selected cells in a QTablewidget. But this should be done if the user releases the mouse button. At the moment I have the following code in the table's itemSelectionChanged signal:

    Qt Code:
    1. QList<QTableWidgetItem *> selections = table->selectedItems();
    2.  
    3. for(int i=0;i<selections.size();i++) {
    4. int row=selections.at(i)->row();
    5. int col=selections.at(i)->column();
    6.  
    7. newItem->setBackgroundColor(QColor("#0000FF"));
    8. table->setItem(row, col, newItem);
    9. }
    To copy to clipboard, switch view to plain text mode 
    The code is working fine so far, but if e.g. the user moves over the rows 0-3 but actually he only wants to select row 0-2 and he moves back to row 2 and releases the mouse button, row 3 is already colored because he moved already over it. I don't want that, in this case only row 0-2 should be colored.

    How can I do that?

    Thanks for answers!
    Last edited by jpn; 29th April 2008 at 13:20. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Selection in QTableWidget

    How about storing the selection as a member variable? Once the selection changes, you clear the previously stored selection and colorize & store the new selection.
    J-P Nurmi

  3. #3
    Join Date
    Apr 2008
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Selection in QTableWidget

    Yes, that would work if I only want to color the actual selection. But I like to color every selection I make, so don't delete older colored fields.

    My problem is that I don't want to color the fields already if I hover over the cells, I want to color the actual selection if I release the mouse button. So maybe something with mouse event...?!

  4. #4
    Join Date
    Apr 2008
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Smile Re: Selection in QTableWidget

    Problem solved:

    I installed an event filter on the viewport of tablewidget and put the code I hade in itemSelectionChanged into the eventfilter method:

    bool DialogClass::eventFilter(QObject* target, QEvent* event)
    {
    if (target==table->viewport()){
    if(event->type() == QEvent::MouseButtonRelease){
    //Code from first thread (look above)
    }
    }
    return false;
    }

Similar Threads

  1. Dragging mouse selection in QTableWidget
    By yartov in forum Qt Programming
    Replies: 1
    Last Post: 12th April 2008, 15:03
  2. Selection of cell with QWidget in QTableWidget
    By Tamara in forum Qt Programming
    Replies: 7
    Last Post: 17th February 2007, 14:11
  3. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 01:57
  4. Model sorting vs. selection
    By VlJE in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 16:46
  5. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 21:05

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.