Results 1 to 13 of 13

Thread: QTableWidget

  1. #1
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableWidget

    Hi..
    Can i create a table with certain cells with a "read only" property...??? i did look up the documentation of QTableWidget but did not find any answers... Any clues anyone...?? thanks...

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    Each cell of QTableWidget is an object of QTableWidgetItem. You can use setFlags function of QTableWidgetItem class to make the cell read only.

  3. #3
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    thanks mate.... and also i wanted to know whether i can include a spinbox or a combobox in a cell...

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    Qt Code:
    1. void setCellWidget ( int row, int column, QWidget * widget )
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: QTableWidget

    J-P Nurmi

  6. #6
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    got it..!! thanks all...!!

  7. #7
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    Quote Originally Posted by munna View Post
    Each cell of QTableWidget is an object of QTableWidgetItem. You can use setFlags function of QTableWidgetItem class to make the cell read only.
    i cannot set the flags off...!!! please help...!!!

  8. #8
    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: QTableWidget

    Quote Originally Posted by rick_st3 View Post
    i cannot set the flags off...!!! please help...!!!
    Could you show us how you try to set them off?
    J-P Nurmi

  9. #9
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    Quote Originally Posted by jpn View Post
    Could you show us how you try to set them off?
    thats exactly what i cannot think of..!!
    I checked the documentation for setting the flag values but the problem is all of them are for enabling them...none of them are to disable them...!! i tried
    Qt Code:
    1. exht->setFlags(Qt::ItemIsEditable(false));
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. exht->setFlags(Qt::ItemIsEditable = 0);
    To copy to clipboard, switch view to plain text mode 
    and all possible combinations i could think of(Now i don't know if the code is right or no) but its just not helping...!!! please help...!!!

  10. #10
    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: QTableWidget

    Use bitwise operators:
    Qt Code:
    1. item->setFlags(item->flags() & ~Qt::ItemIsEditable);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. The following user says thank you to jpn for this useful post:

    frankiefrank (2nd January 2012)

  12. #11
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    Quote Originally Posted by jpn View Post
    Use bitwise operators:
    Qt Code:
    1. item->setFlags(item->flags() & ~Qt::ItemIsEditable);
    To copy to clipboard, switch view to plain text mode 
    guru indeed...!!! thanks a ton mate...!!

  13. #12
    Join Date
    Jun 2008
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    Quote Originally Posted by jpn View Post
    i went through the tutorial...and now i can include a spinbox or a doublespinbox using delegates... but now what i cannot understand is what if i wanted to include the spinbox(or any other widget) in just a few cells(keeping the other cells normal) instead of the all the cells..??? will have to make another delegate to implement the QTableWidgetItem ...?? or is there any way i can apply the delegate to just a few cells instead of making a model of the entire table and then including the delegates..??? please advice... thanks...

  14. #13
    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: QTableWidget

    There are several ways to achieve that. One extremely simple but not that dynamic way would be to check the column and row number in createEditor(). For example:
    Qt Code:
    1. QWidget* MyItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    2. {
    3. if (index.column() == 3 && index.row() > 2) {
    4. ...
    5. return customEditor;
    6. }
    7. return 0; // or return QItemDelegate::createEditor(parent, option, index);
    8. }
    To copy to clipboard, switch view to plain text mode 
    A bit more flexible way would be to use user roles to store the information:
    Qt Code:
    1. tableWidgetItem->setData(Qt::UserRole, true);
    2.  
    3. QWidget* MyItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    4. {
    5. if (index.data(Qt::UserRole).toBool() == true) {
    6. ...
    7. return customEditor;
    8. }
    9. return 0; // or return QItemDelegate::createEditor(parent, option, index);
    10. }
    To copy to clipboard, switch view to plain text mode 
    You can also consider using item/cell widgets as already suggested, but just be aware that they don't really integrate to the model-view framework. They're just something laid by hand on top of views.
    J-P Nurmi

Similar Threads

  1. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  2. Select columns from a QTableWidget
    By toglez in forum Qt Programming
    Replies: 10
    Last Post: 7th October 2007, 15:15
  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. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  5. Replies: 6
    Last Post: 5th March 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.