Results 1 to 6 of 6

Thread: QTableWidget, one column editable

  1. #1
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question QTableWidget, one column editable

    I want my tablewidget has these functionality:
    1) one column can be editable, the rest not;
    2) row select

    I tried it out in this way:

    Qt Code:
    1. tableWidget->horizontalHeader()->setClickable(false);
    2. tableWidget->horizontalHeader()->setStretchLastSection(true);
    3. tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    4. tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    To copy to clipboard, switch view to plain text mode 

    tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers ) will forbiden all the edition. I couldn't find out the correct method for enable edition only for on column. The method of setFlags() to TableWidgetItem with Qt::ItemIsEditable will disable the edition. I am confused. Any one can help? Thanks.

  2. #2
    Join Date
    Oct 2007
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: QTableWidget, one column editable

    Hi,

    It can be done by catching itemClicked or double clicked event.
    Compare the column of clicked item and set the flag as editable or un-editable.

    Firstly for all items you can set edig triggers as NoEditTriggers
    Darshan

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

    Vidhya (31st July 2012)

  4. #3
    Join Date
    Feb 2009
    Location
    libya
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget, one column editable

    hi
    use 'void cellClicked ( int row, int column )' signal
    and Put yor code in this signal,

    for example:
    first connect signal to table
    ok
    void MyClass::cellClickedMyTable(int _row, int _col){
    if( _col == 2 ){
    myTable->item( _row, _col )->setFlags(Qt::ItemIsEditable); // or another code
    }
    }

  5. #4
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget, one column editable

    Quote Originally Posted by gmaa45 View Post
    hi
    use 'void cellClicked ( int row, int column )' signal
    and Put yor code in this signal,

    for example:
    first connect signal to table
    ok
    void MyClass::cellClickedMyTable(int _row, int _col){
    if( _col == 2 ){
    myTable->item( _row, _col )->setFlags(Qt::ItemIsEditable); // or another code
    }
    }
    Thanks for your reply.

    I am using your way to test it out.
    I think it is working. But the 'Flag' thing is not working as I expected.

    After set one cell with
    Qt Code:
    1. myTable->item( _row, _col )->setFlags(Qt::ItemIsEditable);
    To copy to clipboard, switch view to plain text mode 
    this cell is actually not editable any more. I tried also with flag Qt::ItemIsSelectable, after setting this, the cell is not selectable. Is this totally working like this, or other things related to this parameter have conflicts with it?

    What I expected is, after set it with ItemIsSelectable, the cell can be selected, isn't it? The testing result is something opposite....

  6. #5
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget, one column editable

    After some search here, I found the answer:
    Qt Code:
    1. void MyTableWidget::click_cell(int row, int column)
    2. {
    3. if( column != 1 )
    4. tableWidget->item(row, column)->setFlags(tableWidget->item(row, column)->flags() & ~Qt::ItemIsEditable);
    5. }
    To copy to clipboard, switch view to plain text mode 


  7. #6
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget, one column editable

    I used the above solution to make the cells uneditable except for a specific column. The problem now is that every click also triggers the following SIGNAL-SLOT

    connect(ui->tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(recalculate(QTableWidgetItem*)));

    This causes the recalculation to occur on every click on a non-editable field. Any idea on how to stop this from happening? Is there a way to state which column the itemChanged SIGNAL will look at?

    Thanks,

    Pericles

    EDIT* I found the solution by disconnecting the SIGNAL-SLOT right before the change and connecting again after the change.
    Last edited by pcheng; 11th July 2012 at 09:44.

Similar Threads

  1. QTableWidget stretch a column other than the last one
    By roleroz in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2015, 06:35
  2. Replies: 0
    Last Post: 9th February 2009, 20:57
  3. QTableWidget column width and resizing
    By shooogun in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2008, 22:31
  4. QTreeWidget - Making a Column editable.
    By Preeteesh in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2007, 12:02
  5. QTableWidget column and row sizes
    By Arthur in forum Qt Programming
    Replies: 4
    Last Post: 27th January 2006, 11:03

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.