Results 1 to 5 of 5

Thread: How to insert/delete in QTableView

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to insert/delete in QTableView

    Hi.

    I use QTableView to display data from my QStandardItemModel, data is displayed ok. Now I wonder if the QTableView has some default contextmenu or anything similar to allow the user at runtime to insert or delete to/from it. I see there is a property contextMenuPolicy, but don't know if that is of any use for my purpose. I know I can write custom contextmenu and attach it to QTableView, but if there is a way to use the basic/default one, I would go with that one. Thanks for help.

  2. #2
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to insert/delete in QTableView

    @arcull
    you can use the customContextMenuRequested signal to get the event. Use QTableView::indexAt to find out what, if any, cell was clicked based on that you can perform your action(insert/delete).

  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to insert/delete in QTableView

    I came back to this issue after a wile and have added context menu via code, because I see no other simpler way, if there is please let me know Here is what I've got so far, well the relevant part only.
    Qt Code:
    1. ui->tableWidget->verticalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
    2. connect(ui->tableWidget->verticalHeader(),SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(SetCustomContMenu()));
    3.  
    4.  
    5. void MainWindow::SetCustomContMenu() {
    6.  
    7. QMenu menu(this);
    8.  
    9. QAction *actdelete = new QAction(tr("&Delete"),this);
    10. actdelete->setStatusTip(tr("delete record"));
    11. actdelete->setShortcut(tr("Ctrl+D"));
    12. connect(actdelete,SIGNAL(triggered()),this,SLOT(DeleteRec(menu&))); // here trying to pass menu, but no go
    13.  
    14. QAction *actinsert = new QAction(tr("&Insert"),this);
    15. actinsert->setStatusTip(tr("insert record"));
    16. actinsert->setShortcut(tr("Ctrl+I"));
    17. connect(actinsert,SIGNAL(triggered()),this,SLOT(InsertRec()));
    18.  
    19. menu.addAction(actdelete);
    20. menu.addAction(actinsert);
    21. menu.exec(QCursor::pos());
    22. }
    23.  
    24. bool MainWindow::DeleteRec(QMenu *menu) {
    25. qDebug() << "will delete rec now";
    26. menu->close();
    27. }
    28.  
    29. bool MainWindow::InsertRec() {
    30. qDebug() << "will insert rec now";
    31. }
    To copy to clipboard, switch view to plain text mode 

    It looks like working, but I wanted to add the Menu as parameter to DeleteRec method, so I can close it afterwards. The code compiles but at runtime it says there is no slot DeleteRec(menu&). So obviously I can't pass params when connecting signal, right? How do I close my contextmenu otherwise? Much thanks again.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to insert/delete in QTableView

    You could keep the menu in a member variable of the class.
    But why do you need to "close" it, in your current code it gets destroyed right after being used anyway?

    Cheers,
    _

  5. #5
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to insert/delete in QTableView

    Yes you are right, it should be destroyed right away.....but well I don't know what exactly happened then. The menu opened when I right clicked the records, but when I selected/clicked "Delete" option afterwards, the menu blinked and remained opened, or it may be it opened the second time,boooo. I removed menu param now. Hmmm I can't reproduce this again, it looks like ok now, but will do some further tests. Thanks again.

Similar Threads

  1. Replies: 15
    Last Post: 8th June 2013, 06:15
  2. Replies: 8
    Last Post: 17th June 2011, 21:36
  3. Insert/Delete points over image
    By sergio87 in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2011, 07:37
  4. How to know not insert or delete query?
    By ramazangirgin in forum Qt Programming
    Replies: 2
    Last Post: 29th June 2010, 13:43
  5. i need to know insert and delete methods in qsql for phone book application
    By gaurav purohit in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 15th September 2009, 14:04

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.