Results 1 to 3 of 3

Thread: Auto text select in QTableWidgetItem

  1. #1
    Join Date
    Sep 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Red face Auto text select in QTableWidgetItem

    Does anybody know how to have all of the text "selected" in a QTableWidgetItem after a double mouse click?

    Currently, the local editor is invoked, the cursor shows up, but you still have to use the mouse to select the existing text.

    The starting point for my code is...

    void SpreadSheet::mouseDoubleClickEvent(QMouseEvent* pevent)
    {
    QTableWidget::mouseDoubleClickEvent(pevent);

    } // SpreadSheet::mouseDoubleClickEvent(

  2. #2
    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: Auto text select in QTableWidgetItem

    There might be "hackish" ways to accomplish this the way you've already started, but I think item delegates are the proper way to go..

    Items are not edited "in place". A temporary editor widget is provided each time an item is edited. And it's the item delegate who provides this temporary editor widget used for editing items content.

    One "hackish" way I was talking about would require somehow indentifying the temporary line edit object. This can most likely be done even quite easily by using QObject::findChild().

    So, a better approach is to use item delegates. Delegates work like this:


    QItemDelegate is the default implementation for delegates and that's what QTableWidget like any other view uses. You could subclass QItemDelegate and extend the setEditorData() behaviour so that in addition to letting the base class implementation to fill the editor you select the content as well.

    Qt Code:
    1. void MyItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
    2. {
    3. // let base class fill the editor
    4. QItemDelegate::setEditorData(editor, index);
    5.  
    6. // select all if it's a line edit
    7. QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor);
    8. if (lineEdit)
    9. lineEdit->selectAll();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Edit: Oh, and you set the extended item delegate for example like this:
    Qt Code:
    1. tableWidget->setItemDelegate(new MyItemDelegate(tableWidget));
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 3rd October 2006 at 21:07. Reason: updated contents
    J-P Nurmi

  3. #3
    Join Date
    Sep 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Auto text select in QTableWidgetItem

    Thanks much!
    That worked fine.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Interesting little Segfault w/r to signal/slot connection
    By Hydragyrum in forum Qt Programming
    Replies: 24
    Last Post: 12th September 2006, 19:22
  3. visible text of textedit
    By regix in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2006, 09:02

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.