Results 1 to 6 of 6

Thread: QListWidget + Delete Key

  1. #1
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QListWidget + Delete Key

    I was wondering what the best way to delete a QListWidget item when you press the delete key.

    Should I write my own class that inherits QListWidget and overwrite the keyPressEvent() method to capture the delete key? There must be a better design.

    Thanks!
    Brandon P.

  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: QListWidget + Delete Key

    Either that or use an event filter.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget + Delete Key

    Or QShortcut.

  4. The following user says thank you to jacek for this useful post:

    bpetty (16th August 2006)

  5. #4
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget + Delete Key

    Jacek, I like the idea of using QShortcut.

    I noticed the example from the documentation:
    Qt Code:
    1. shortcut = QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), parent);
    To copy to clipboard, switch view to plain text mode 

    So instead of Ctrl+O I would use Qt::Key_Delete...
    and instead of parent, I would use the name of my QListWidget object...
    I would also most likely have to change the context to that of a Qt::WidgetShortcut

    Is this correct? I guess I don't understand then what it returns and where the correct place to put this QShortcut would be.

  6. #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: QListWidget + Delete Key

    Let's assume you have a list widget in a main window. Then, a natural place to create the shortcut would be in the constructor of the main window (or a function called from there). In addition, you'll need a slot where to react to the shortcut event.

    Qt Code:
    1. class MainWindow...
    2. {
    3. ...
    4. private slots:
    5. void deleteItem();
    6.  
    7. private:
    8. QListWidget* listWidget;
    9. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::MainWindow(...) : ...
    2. {
    3. // create the shortcut after the list widget has been created
    4.  
    5. // option A (pressing DEL anywhere in the main window activates the slot)
    6. new QShortcut(QKeySequence(Qt::Key_Delete), this, SLOT(deleteItem()));
    7.  
    8. // option B (pressing DEL activates the slots only when list widget has focus)
    9. QShortcut* shortcut = new QShortcut(QKeySequence(Qt::Key_Delete), listWidget);
    10. connect(shortcut, SIGNAL(activated()), this, SLOT(deleteItem()));
    11. }
    12.  
    13. void MainWindow::deleteItem()
    14. {
    15. delete listWidget->currentItem();
    16. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following 3 users say thank you to jpn for this useful post:

    sargeslash (17th January 2013), sector (16th August 2006), XRI-Vision (13th January 2015)

  8. #6
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget + Delete Key

    Thanks a lot JPN. I guess I took it too literally. They said "shortcut = QShorcut(..)" and I was atleast expecting to see a new. I was also thinking that the new "shortcut" pointer gets destoyed... but I guess Qt knows what I want.

    Thanks a lot.

Similar Threads

  1. Delete all members in a QGraphicsItemGroup
    By vmferreira in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2006, 18:47
  2. keypress while editing an item in QListWidget
    By Beluvius in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 09:56
  3. [Qt4]: Adding centered items in QListWidget
    By Jojo in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2006, 20:04
  4. QListWidget add QListWidgetItem
    By fellobo in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2006, 19:37
  5. How to explicitely delete a QDir?
    By alan in forum Newbie
    Replies: 2
    Last Post: 13th February 2006, 17:48

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.