Results 1 to 6 of 6

Thread: QTableView line edit clears the text on edit

  1. #1

    Default QTableView line edit clears the text on edit

    Hi all.

    Is there a way to alter the behavior of QTableView when editing a text cell to not clear the text in the cell when the cell is selected for editing?

    For instance, I have a QTableView where the first column contains long text fields. It is impractical to have the user re-enter the entire contents just to change a single character.

    I know I can do this by providing a custom widget for the column, but it seems like such standard behavior that there is a default implementation that I am missing.


    Thanks!

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView line edit clears the text on edit

    That is strange. What did you set for view properties?

    I have a table view like this:

    Qt Code:
    1. model = new QSqlTableModel(this, logdb);
    2. view = new QTableView();
    3. view->setModel(model);
    4. model->setTable("log");
    5. header = new QHeaderView(Qt::Horizontal, this);
    6. view->setHorizontalHeader(header);
    7. header->setMovable(TRUE);
    To copy to clipboard, switch view to plain text mode 

    And when I click in a table cell to edit, it highlights the cell, then if I double click, it highlights just the text for editing. Nothing goes away.

  3. #3

    Default Re: QTableView line edit clears the text on edit

    Thanks for the quick reply.

    So it should behave the way I want by default? Very strange.

    Here's my code which sets up the view:

    Qt Code:
    1. ui->tableView->setColumnWidth(0, 200);
    2. ui->tableView->setColumnWidth(1, 150);
    3. ui->tableView->verticalHeader()->hide();
    4. ui->tableView->horizontalHeader()->setStretchLastSection(true);
    5. ui->tableView->resizeColumnsToContents();
    6. ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    7. ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    To copy to clipboard, switch view to plain text mode 

    I didn't see anything strange in there, but I tried commenting out all of the above and got the same behavior as before.

    My platform is qt 4.6.2-1 running on kde 4.4.0. Do you think it could be a platform issue?

    Thanks again.

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTableView line edit clears the text on edit

    I am currently seeing the same problem/behavior on Linux running Qt 4.5.3. The contents of an editable table cell are erased when the cell is selected. I would like them to remain untouched unless the user explictly deletes them.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTableView line edit clears the text on edit

    Found a solution in this thread; add a clause to your model's data() routine that returns the item's data in editable form when called with a Qt::EditRole.

  6. The following 4 users say thank you to SixDegrees for this useful post:

    fede (15th March 2011), jkrienert (14th March 2015), Meladi (4th June 2014), ZyntaxError (11th July 2013)

  7. #6
    Join Date
    Dec 2014
    Posts
    48
    Thanks
    23
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    Windows

    Default Re: QTableView line edit clears the text on edit

    @SixDegrees:
    Understandable that since this thread is old an answer to this request might go unspoken.
    -Yet, do you have any example of the implementation you mentioned regarding the data() function in the model?
    [currently experiencing a similar problem where the cell contents are deleted upon selection rather then inherently editable.

Similar Threads

  1. line edit focus
    By addu in forum Qt Programming
    Replies: 3
    Last Post: 16th July 2009, 16:39
  2. Character encoding in text edit and llne edit
    By greenvirag in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 09:45
  3. Checking max text lenght to fit in line edit?
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 16th June 2008, 01:24
  4. mask line edit
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 3rd July 2007, 11:27
  5. Replies: 8
    Last Post: 15th May 2007, 10:21

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.