Results 1 to 7 of 7

Thread: Disable row/rows in QTableView

  1. #1
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Disable row/rows in QTableView

    This will be a softball to all you experts...

    I have found examples of code that will allow the disabling of certain columns within a QTableView populated by a QSqlTableModel but not much for a row and I'm wondering if I can follow this: http://www.qtcentre.org/threads/3606...e%2Ba%2Bcolumn

    What I'm trying to do is populate a tableview with non-editable data, but when the user clicks on a "New" pushbutton, a new row is created in the model that is editable. Rows will remain editable until a submitAll() is called at which time they can't be edited anymore....

    Thanks!


    scott

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable row/rows in QTableView

    The post you've found actually explains everything you need...View's EditTriggers allow you to control the global "editability" of the shared model.Since you don't want user to edit anything before row is created:
    Qt Code:
    1. void CustomWidget::setViews()
    2. {
    3. //...
    4.  
    5. QTableView customView;
    6.  
    7. customView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    8.  
    9. //...
    10. }
    To copy to clipboard, switch view to plain text mode 


    More precise control over editing can be achieved via the model class itself.

    Take a look at the post's code snippet (the one with the ManifestModel class).The flags(...) method is the one the does the primary job of setting items' "editability" and other properties. Your model's class flag() method might look like this:
    Qt Code:
    1. Qt::ItemFlags CustomModel::flags ( const QModelIndex & index ) const
    2. {
    3.  
    4. if( index.row() == ActiveRow ) return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    5.  
    6. else return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    Where ActiveRow is set when a slot connected with the " "New" pushButton " will be invoked:
    Qt Code:
    1. void CustomWidget::newButton_Slot()
    2. {
    3.  
    4. // create new rows //
    5.  
    6. customModel->setActiveRow(...);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

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

    scott_hollen (25th March 2011)

  4. #3
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Disable row/rows in QTableView

    Holy, cow, you mean I'm on to something for a change??? Thanks so much!


    scott

  5. #4
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Disable row/rows in QTableView

    Okay, maybe I've been watching too much basketball but I may be overworking this problem...While I understand completely what it is I'm *trying* to do I'm having understanding exactly what it is I need to do...

    You reference "ActiveRow" but is this just a shorthand reference to my current active row in my model? To keep things on the simple, what I'd like to do is on the newButton_slot() activate the row, and then whenever submitALL() is performed I can set NoEditTriggers on my tableview...

    Is it possible to attack this problem thru the view?

    Thanks and sorry for the ignorance...



    scott

  6. #5
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable row/rows in QTableView

    I used ActiveRow variable to store the last row number,but it's not really necessary.Don't quite understood your last question but...seems like you need a little bit more explanation.
    This is what your main widget class might look like,for time sake i'm gonna put all in constructor :
    Qt Code:
    1. MainWidget::MainWidget(QWidget *parent):QWidget(parent)
    2. {
    3.  
    4. view=new QTableView;
    5.  
    6. model=new CustomModel( rows , columns , this)
    7.  
    8. view->setModel(model);
    9.  
    10. createButton=new QPushButton(tr(" New Row "));
    11.  
    12. connect(createButton,SIGNAL( clicked() ),SLOT( createRow_Slot() ) )
    13.  
    14. submitButton=new QPushButton(tr(" Submit Row "));
    15.  
    16. connect(createButton,SIGNAL( clicked() ),SLOT( createRow_Slot() ) )
    17.  
    18. QVBoxLayout *mainLayout=new QVBoxLayout(this);
    19. mainLayout->addWidget(view);
    20. mainLayout->addWidget(createButton);
    21. mainLayout->addWidget(submitButton);
    22. }
    To copy to clipboard, switch view to plain text mode 
    Buttons' slots might look like this:
    Qt Code:
    1. void MainWidget::createRow_Slot()
    2. {
    3. if ( model->addRow( model->rowCount() ) )
    4. view->setEditTriggers(QAbstractItemView::DoubleClicked); // make sure that view will be editable once the row is added
    5.  
    6. else // call an error message or smth. to indicate a problem while inserting a new row
    7.  
    8. }
    9. void MainWidget::submitRow_Slot()
    10. {
    11.  
    12. view->setEditTriggers(QAbstractItemView::NoEditTriggers); // once submitted,the view will be no longer editable
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    And finally you CustomModel class.The only method you need is "flags",so this is it:
    Qt Code:
    1. Qt::ItemFlags flags(const QModelIndex &index) const
    2. {
    3. if(index.row()== ( rowCount() -1 ) )
    4. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; //only last row is editable
    5. else
    6. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to AlexSudnik for this useful post:

    scott_hollen (28th March 2011)

  8. #6
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Disable row/rows in QTableView

    Ahhhh...Now, I see...Looks like I was about 95% there -- the "if(index.row()== ( rowCount() -1 ) ) " part is where I was tripping up...I haven't really used ItemFlags yet (only been using Qt for about 3 months part time)...Some things are so obvious in hindsight...

    Throughout the rest of my app I've handled the enable/disabling of the data easily because I was using a master/detail design...Once data was submitted to the DB, then just disabled the entire tableview; a "new" button click resulted in me turning it back on, but this last section is just once large tableview...

    I appreciate the push!


    scott

  9. #7
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Disable row/rows in QTableView

    Works perfectly!!! Put in the line,
    Qt Code:
    1. if(index.row()== ( rowCount() -1 ) )
    To copy to clipboard, switch view to plain text mode 
    and everything worked great -- thanks for that direction!


    scott

Similar Threads

  1. Color the rows in QTableView
    By grub87 in forum Qt Programming
    Replies: 11
    Last Post: 17th June 2009, 20:41
  2. QTableView 2 rows inside every row
    By skuda in forum Qt Programming
    Replies: 3
    Last Post: 22nd April 2009, 09:23
  3. QTableWidget: Disable 'snapping' to rows and columns?
    By PolyVox in forum Qt Programming
    Replies: 2
    Last Post: 8th October 2008, 20:04
  4. [QTable] Disable centering of rows on mouseclick
    By BrainB0ne in forum Qt Programming
    Replies: 4
    Last Post: 30th October 2006, 12:47
  5. Copying QTableView rows
    By derrickbj in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2006, 00:00

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.