ttvo
1st April 2009, 16:48
Hi all,
I have a QTreeView and QStandardItemModel and want to add a context menu so that new child node(s) can be added to the current node.
At construction, I'd like to have three empty tree nodes
<checkbox><icon>Rectangles
<checkbox><icon>Circles
<checkbox><icon>Squares
and to enable a right click on each of those nodes to add/delete/modify. I started with an ActionsContextMenu but then I need the node's index, so I successfully created a custom context menu.
...
// 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???
}
I got the code to compile and the addObject() method is invoked properly. My question is how am I going to get access to the corresponding node index to insert a child node appropriately?
Thanks in advance.
I have a QTreeView and QStandardItemModel and want to add a context menu so that new child node(s) can be added to the current node.
At construction, I'd like to have three empty tree nodes
<checkbox><icon>Rectangles
<checkbox><icon>Circles
<checkbox><icon>Squares
and to enable a right click on each of those nodes to add/delete/modify. I started with an ActionsContextMenu but then I need the node's index, so I successfully created a custom context menu.
...
// 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???
}
I got the code to compile and the addObject() method is invoked properly. My question is how am I going to get access to the corresponding node index to insert a child node appropriately?
Thanks in advance.