Results 1 to 4 of 4

Thread: QTableWidgetItem setflags ..strange behavior

  1. #1
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default QTableWidgetItem setflags ..strange behavior

    Developing on QT 4.2.3

    I try to set an item of a table not editable:

    QTableWidget *mytable;
    QTableWidgetItem myvalue1;
    myvalue1 = mytable->item(0,1);

    2 ways

    if i try:
    myvalue1->setFlags(myvalue1->flags() | Qt::ItemIsEditable);
    doesen't work.

    if i try
    // myvalue1->setFlags(Qt::ItemIsEditable);
    this works.

    Why this ???

    Second Problem is that i want to unset the flag not editable,
    but if i try
    myvalue1->setFlags(myvalue1->flags() & ~Qt::ItemIsEditable);

    This doesen't work.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableWidgetItem setflags ..strange behavior

    Looks like you are using the flags the opposite way, check this out
    Qt Code:
    1. QTableWidget *tableWidget = new QTableWidget;
    2. QTableWidgetItem *item = new QTableWidgetItem("My Item");
    3. Qt::ItemFlags flags;
    4. tableWidget ->setRowCount(1);
    5. tableWidget ->setColumnCount(1);
    6. tableWidget ->setItem(0, 0, item);
    7.  
    8.  
    9. // set the item editable
    10. item = tableWidget->item(0, 0);
    11. flags = item->flags();
    12. flags |= Qt::ItemIsSelectable | Qt::ItemIsEditable; // set the flag
    13. item->setFlags(flags);
    14.  
    15.  
    16. // set the item non-editable (view only), but still selectable
    17. item = tableWidget->item(0, 0);
    18. flags = item->flags();
    19. flags |= Qt::ItemIsSelectable;
    20. flags &= ~Qt::ItemIsEditable; // reset/clear the flag
    21. item->setFlags(flags);
    22.  
    23.  
    24. // set the item non-editable (view only), and non-selectable
    25. item = tableWidget->item(0, 0);
    26. flags = item->flags();
    27. flags &= ~(Qt::ItemIsSelectable | Qt::ItemIsEditable); // reset/clear the flag
    28. item->setFlags(flags);
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: QTableWidgetItem setflags ..strange behavior

    Thank you !!! Very much I understand my errors with your code !!
    I notice only a strange thing...

    I've a table and a button, when clicks on the button the data in the rows is sent to the software.
    Using the flag
    // set the item non-editable (view only), and non-selectable

    this works well but if i click on the row, even if it is not selectable and editable,
    the item value are always set, and received by the software, when i click the button.

    Is there a way to discharge the value sent from an unselectable and editable item ?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableWidgetItem setflags ..strange behavior

    It is hard to suggest as you have not mentioned about the slot connected to button click, what is does and how it gets data from table?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 1
    Last Post: 7th December 2011, 01:04
  2. strange behavior of paintEvent
    By hashb in forum Qt Programming
    Replies: 3
    Last Post: 30th August 2010, 08:48
  3. QAudioInput example strange behavior
    By m15ch4 in forum Qt Programming
    Replies: 0
    Last Post: 13th August 2010, 07:55
  4. Strange behavior of QSqlTableModel
    By venomj in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2010, 04:29
  5. QTableWidgetItem's setFlags() ???
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2006, 16:07

Tags for this Thread

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.