Results 1 to 3 of 3

Thread: qtableview QStandardItemModel read only

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qtableview QStandardItemModel read only

    Hello,

    How do I make a QTableView / QStandardItemModel read only?

    With QSqlTableModle it's in edtiStrategy, however I can't find a similar thing in QStandardItemModel.

    And a general question. A situation with a table and a delegate, how does one communicate between the two, for instance if the paint event in the delegate wants to know the current item in the table view.
    For now I use global vars / functions for this, but I bet there is a more object oriented way to do that.

    Greets, Jean.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qtableview QStandardItemModel read only

    Set QAbstractItemView::NoEditTriggers with QTableView::setEditTriggers.

    For the second problem, you either make the selection model available in the delegate(an easy solution), or for every item you add some custom boolean data(for a user role) which tells you if the item is selected or not.


    For the first solution, you can pass the view as the delegate's parent when you construct it. You can cast parent() inside the delegate back to the view and access its selection model.
    For the second solution, you can get the data for your role in the delegate's paint and see if it is selected.

    EDIT: I think you can use QStyleOptionViewItem::state variable. If it QStyle::State_selected, then the item is selected. I am not sure if it contains valid values when it is passed to you.

    EDIT2: I just had a look at the QItemDelegate::paint implementation. It seems that QStyleOptionView::state is valid, so you can just test for QStyle::State_Selected.
    Last edited by marcel; 9th February 2008 at 16:15.

  3. The following user says thank you to marcel for this useful post:

    JeanC (9th February 2008)

  4. #3
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qtableview QStandardItemModel read only

    Thanks marcel,

    I looked at QAbstractItemView, must have overlooked that.

    Quote Originally Posted by marcel View Post
    Set QAbstractItemView::NoEditTriggers with
    QTableView::setEditTriggers.
    I used the first solution. I was using table->setItemDelegate(new myDelegate); because I read that in some example and when I first tried to change to a new'd class it didn't work so I thought that was the only way. Fortunately it does work otherwise.

    For the first solution, you can pass the view as the delegate's parent when you construct it. You can cast parent() inside the delegate back to the view and access its selection model.
    This one also looks good yes.,

    EDIT: I think you can use QStyleOptionViewItem::state variable.
    Great help again.

Similar Threads

  1. Extract QStandardItemModel from QTableView
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2008, 08:34
  2. How to read CD with read?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 29th June 2007, 08:20
  3. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29

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.