openSUSE Forums > Programming/Scripting » Context Menu on QTreeWidget Items

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-Sep-2009, 11:25
ganeshbiyer's Avatar
Puzzled Penguin
 
Join Date: Sep 2009
Location: Bangalore, India
Posts: 3
ganeshbiyer hasn't been rated much yet
Default Context Menu on QTreeWidget Items

Hi,

How do we create context menu's for QTreeWidget Item. This popup menu should be visible only on the selected item.

The Tree Creation

QTreeWidgetItem *item;
QFont font;
font.setBold(true);
font.setWeight(75);

item = new QTreeWidgetItem;
item->setText(0,"ITEMS");
item->setFont(0,font);

treeMain = new QTreeWidget;
treeMain->setHeaderItem(item);
treeMain->setSortingEnabled(true);
treeMain->sortByColumn(1,Qt::AscendingOrder);

item = new QTreeWidgetItem;
item->setText(0,"New");
treeMain->addTopLevelItem(item);
treeMain->setContextMenuPolicy(Qt::CustomContextMenu);
txtMain = new QTextBrowser;

boxMain = new QHBoxLayout;
boxMain->addWidget(treeMain);
boxMain->addWidget(txtMain);
boxMain->setStretchFactor(treeMain,1);
boxMain->setStretchFactor(txtMain,3);

centralWidget = new QWidget;
centralWidget->setLayout(boxMain);

this->setCentralWidget(centralWidget);

The Actions for the menu
nodeMenu = new QMenu(treeMain);

insertNodeChild = new QAction("Insert Child",nodeMenu);
insertNodeAfter = new QAction("Insert Node After",nodeMenu);
insertNodeBefore = new QAction("Insert Node Before",nodeMenu);
deleteNode = new QAction("Delete Node",nodeMenu);

nodeMenu->addAction(insertNodeChild);
nodeMenu->addAction(insertNodeAfter);
nodeMenu->addAction(insertNodeBefore);
nodeMenu->addAction(deleteNode);

How do I show the above actions in context menu for the tree item.
Please help me.

Thanks
Ganesh
Reply With Quote
  #2 (permalink)  
Old 01-Sep-2009, 13:00
Monex's Avatar
Busy Penguin
 
Join Date: Jun 2008
Location: Germany
Posts: 256
Monex hasn't been rated much yet
Default Re: Context Menu on QTreeWidget Items

Hi,

just set the context menu policy for the tree widget should do the trick. Then simply add the actions to the tree widget and they should be shown on right click.

For example:
treeMain = new QTreeWidget;
treeMain->setContextMenuPolicy(Qt::ActionsContextMenu);

insertNodeChild = new QAction("Insert Child",nodeMenu);
treeMain->addAction(insertNodeChild);


Hope this helps
Reply With Quote
  #3 (permalink)  
Old 01-Sep-2009, 13:43
ganeshbiyer's Avatar
Puzzled Penguin
 
Join Date: Sep 2009
Location: Bangalore, India
Posts: 3
ganeshbiyer hasn't been rated much yet
Default Re: Context Menu on QTreeWidget Items

Hi Monex,

It works like charm.

Thanks
Ganesh
Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2