Results 1 to 7 of 7

Thread: QTableView and marking a cell exclusively

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableView and marking a cell exclusively

    Hello,

    I have a QTableView with each cell being selectable individually, 4 columns and a runtime number of rows.
    I wish to be able to mark 1 of the rows (exclusively of all others), in column 4.

    I suppose there are different ways to do that, but let's say right clicking on the cell marks that cell (exclusively).
    The marked cell then should show the string "Implied"

    1. How do I change the QTableView to handle the righ-click command?
    2. In terms of implementation, is there an easier way than "right-click" ?
    3. If T is the data type for those cells in the data model, that is currently translated to Qstring before display, would I add a member variable to my instance of QAbstractTableModel to hold which cell is marked?


    Regards,

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: QTableView and marking a cell exclusively

    Look into delegates

  3. #3
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView and marking a cell exclusively

    I already have a delegate.
    I'd appreciate some pointers as to which part of the delegate will help with this?

    rds,

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: QTableView and marking a cell exclusively

    In your view, you handle the right mouse click.
    Keep track of the currently exclusive index.
    When right clicking, check if the currently exclusive index = the new exclusive index. If not, set it and reset the other.
    Use setData of your model and a custom role for example.

    The delegate looks at the custom role and paints a different background.

  5. #5
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView and marking a cell exclusively

    I'm trying to pick the right button mouse click event, so I connected the
    Qt Code:
    1. clicked(const QModelIndex &)
    To copy to clipboard, switch view to plain text mode 
    signal to a private slot to my view.
    In my slot, I have:
    Qt Code:
    1. const Qt::MouseButtons mb = QApplication::mouseButtons() ;
    2. if (mb.testFlag(Qt::LeftButton))
    3. std::cout<< "LeftButton clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
    4. if (mb.testFlag(Qt::RightButton))
    5. std::cout<< "Right button clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
    6. if (mb.testFlag(Qt::MidButton))
    7. std::cout<< "MidButton clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
    8. if (mb.testFlag(Qt::XButton1))
    9. std::cout<< "XButton1 clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
    10. if (mb.testFlag(Qt::XButton2))
    11. std::cout<< "XButton2 clicked on row="<< index.row()<<" col="<<index.column()<<std::endl;
    To copy to clipboard, switch view to plain text mode 

    I click on the cells in the table view and my slot is called, however, none of the flags are set.

    Is there a different way to find out which mouse button was clicked?

  6. #6
    Join Date
    Dec 2009
    Posts
    52
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView and marking a cell exclusively

    I found another way: I've implemented the contextMenuEvent and that works

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTableView and marking a cell exclusively

    Quote Originally Posted by hml View Post
    I found another way: I've implemented the contextMenuEvent and that works
    That's not a good idea because a context menu event can be triggered also by means other than right clicking a cell. A better choice would be to use mousePressEvent() or mouseReleaseEvent() for that. Just remember to call the base class implementation when it makes sense to avoid loosing existing functionality.

    But I would probably do it all in the delegate by overriding editorEvent(). I would also store the mark ("implied") in the view and not in the model if it makes sense that you have two views of the same model with different implications in each of them.

    Also, don't use QApplication::mouseButtons(). Use QMouseEvent::button() instead.
    Last edited by wysota; 12th June 2010 at 02:41.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to tell if QTableView cell is visible ?
    By steviekm3 in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2009, 10:04
  2. How to tell if cell in QTableView is visible ??
    By steviekm3 in forum Qt Programming
    Replies: 3
    Last Post: 27th February 2009, 17:57
  3. how to get data from cell of QTableview
    By yleesun in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 14:31
  4. cell selection in QTableView
    By user in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2008, 01:01
  5. How to deactivate the marking in a QTable?
    By mantra in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2008, 15:51

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.