PDA

View Full Version : How to insert/delete in QTableView



arcull
24th July 2015, 07:19
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.

anbu01
24th July 2015, 11:03
@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).

arcull
18th August 2015, 10:41
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.


ui->tableWidget->verticalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->tableWidget->verticalHeader(),SIGNAL(customContextMenuRequested (QPoint)),this,SLOT(SetCustomContMenu()));


void MainWindow::SetCustomContMenu() {

QMenu menu(this);

QAction *actdelete = new QAction(tr("&Delete"),this);
actdelete->setStatusTip(tr("delete record"));
actdelete->setShortcut(tr("Ctrl+D"));
connect(actdelete,SIGNAL(triggered()),this,SLOT(De leteRec(menu&))); // here trying to pass menu, but no go

QAction *actinsert = new QAction(tr("&Insert"),this);
actinsert->setStatusTip(tr("insert record"));
actinsert->setShortcut(tr("Ctrl+I"));
connect(actinsert,SIGNAL(triggered()),this,SLOT(In sertRec()));

menu.addAction(actdelete);
menu.addAction(actinsert);
menu.exec(QCursor::pos());
}

bool MainWindow::DeleteRec(QMenu *menu) {
qDebug() << "will delete rec now";
menu->close();
}

bool MainWindow::InsertRec() {
qDebug() << "will insert rec now";
}


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.

anda_skoa
18th August 2015, 11:14
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,
_

arcull
18th August 2015, 11:39
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:confused:. 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.