...
// set context menu policy
projectTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
...
// initialize the model with three empty nodes
for (int i=0; i<3; ++i) {
model->appendRow(m_dm[i]);
}
m_ui.projectTreeView->setModel(model);
...
// create an add action and connect it to a signal
m_addAction
= new QAction(tr
("Add new"), m_ui.
projectTreeView);
connect(m_addAction, SIGNAL(triggered()), this, SLOT(addObject()));
// connect custom context menu
connect(m_ui.
projectTreeView,
SIGNAL(customContextMenuRequested
( const QPoint
& )),
this,
SLOT(showContextMenu
(const QPoint &)));
...
void showContextMenu(const QPoint& pnt)
{
QList<QAction *> actions;
if (m_ui.projectTreeView->indexAt(pnt).isValid()) {
actions.append(m_addAction);
}
if (actions.count() > 0)
QMenu::exec(actions, m_ui.
projectTreeView->mapToGlobal
(pnt
));
}
...
void MainWindow::addObject()
{
// HOW am I going to get access to the node's index to insert a new child row???
}
...
// set context menu policy
projectTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
...
// initialize the model with three empty nodes
QStandardItemModel *model = new QStandardItemModel();
model->setHorizontalHeaderItem(0, new QStandardItem());
m_dm[0] = new QStandardItem(QIcon(":/images/myIcon.png"), "Rectangles");
m_dm[1] = new QStandardItem(QIcon(":/images/myIcon.png"), "Circles");
m_dm[2] = new QStandardItem(QIcon(":/images/myIcon.png"), "Squares");
for (int i=0; i<3; ++i) {
model->appendRow(m_dm[i]);
}
m_ui.projectTreeView->setModel(model);
...
// create an add action and connect it to a signal
m_addAction = new QAction(tr("Add new"), m_ui.projectTreeView);
connect(m_addAction, SIGNAL(triggered()), this, SLOT(addObject()));
// connect custom context menu
connect(m_ui.projectTreeView, SIGNAL(customContextMenuRequested( const QPoint& )), this, SLOT(showContextMenu(const QPoint &)));
...
void showContextMenu(const QPoint& pnt)
{
QList<QAction *> actions;
if (m_ui.projectTreeView->indexAt(pnt).isValid()) {
actions.append(m_addAction);
}
if (actions.count() > 0)
QMenu::exec(actions, m_ui.projectTreeView->mapToGlobal(pnt));
}
...
void MainWindow::addObject()
{
// HOW am I going to get access to the node's index to insert a new child row???
}
To copy to clipboard, switch view to plain text mode
Bookmarks