Qt QTreeWidget Context Menu: Add items under another o delete items. - qt

I managed to create a context menu that gets activated after a right click on each item of a QTreeWidget tree:
contextMenu = new QMenu(ui->treeWidget);
ui->treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
addElement = new QAction("Add Element",contextMenu);
deleteElement = new QAction("Delete Element",contextMenu);
ui->treeWidget->addAction(addElement);
ui->treeWidget->addAction(deleteElement);
connect(addElement, SIGNAL(triggered()), this, SLOT(addElementHandler()));
connect(deleteElement, SIGNAL(triggered()), this, SLOT(deleteElementHandler()));
My intention is to add new items under another in the tree or delete them by right clicking on a specific item using this context menu.
However I'm not sure how to realize from the handlers on exactly which item of the tree the right click was made.
Could you please give me a clue?
Thanks in advance!

If you are not going to change the TreeWidget selection behavior or set the current item by your own - you can use just the native behavior. While context menu requesting the tree selects the item, on which the right click was performed and that is the currentItem. So in addElementHandler slot the currentItem() will give you exact item you want.

Related

How to set icon of the line edit in a QCombobox widget without inserting the item into the list?

I want to set the icon on the left side of the QCombobox widget. I know I can insert a item first and then set the icon of the inserted item and then select this newly inserted item. However, I would like to do that without inserting a new item into the drop down list for special reasons. Windows ComboBox control allows us to change the icon of the edit box by using an index of -1. I don't know how to achieve that with QCombobox.
Thanks for any comments!
Never tried it myself, but here is an idea.
QComboBox is based on Qt's model/view framework, so the items are contained into a QStandardItemModel which can be accessed with QComboBox::model().
The steps would be:
Instantiate a QStandardItem
Use setIcon() and setText() on the QStandardItem (or use the proper ctor)
When you want to add the item to the Combo list, append it thru the model.
Example:
QStandardItem* item = new QStandardItem(theIcon, theText);
[...]
QStandardItemModel* comboModel = qobject_cast<QStandardItemModel*>(theCombo->model());
comboModel->appendRow(item);

Application Crash problem on second time click on Menu Arrow of QToolButton

In my APPlication I have to show filter option (as filter option in xls)in Qtable widget for this
I have used tool Button( with property "QToolButton::MenuButtonPopup") to display Menu List and
Upon First Click upon Menu arrow it should show Menu list and selection of any of the Menu it should show only row having the text.
This functionality works fine.
But if Nothing is selected from Menu list and user has clicked Menu arrow second time then list should be hidden but in my case
application crashes giving error:
ASSERT failure in QList::operator[]: "index out of range", file ........\Qt\2010.04\qt\include/QtCore/../../src/corelib/tools/qlist.h, line 447
I have written below code:
QToolButton *lToolButton = new QToolButton();
lToolButton->setPopupMode(QToolButton::MenuButtonPopup);
lToolButton->setAutoRaise(true);
lToolButton->setText("Filter");
QMenu *lMenu = new QMenu();
QAction *lAction = new QAction("All",this);
lMenu->addAction(lAction);
lToolButton->setMenu(lMenu);
Please let me know what is wrong in my coding.
Can you run your app in the debugger and find at what line in your code (not in Qt's code) the error is happening? The problem should then because more obvious.

Selected item in a Flex Tree is not highlighted after dragging and dropping it

I have a Flex (3.5) Tree with drag & drop support, but the problem is that after I drag an item (node) and drop it, it's supposed to be the selected item in the tree, but for some reason it's not highlighted. Does anyone know how I can fix it?
Thanks.
I'd try to set the tree's selectedItem property in the dragComplete event.
I ran into this problem too; and based on the fact that the tree had not re-settled visually when the onDragComplete breakpoint tripped, I placed the following in my onDragComplete handler method:
callLater(function():void{
treeGrid.selectedItem = draggedItem;
});
That worked-selected item was highlighted.

Qt Mac multiple menubars/modifiable menubar

I have an application that shows multiple subpanels & the client wants to show different menus for each subpanel.
Mac apps can only have one menuBar per system window, apparently, and it's minimally modifiable (if at all.) I need to remove/add or enable/disable menus on the menubar.
I've thought about making each of the subpanels a system window and attaching a menubar to each, but I don't see any provision for switching to a window's menubar. Besides, I suspect that doing so would create a state/positioning mess for the subpanels.
What I've Found
I've found that if I create the actions as children of the main window, I can add and remove them at will from the menus themselves. So, I can modify the menu contents, but I can't modify the menubar contents.
I've found I can also change the title of the menu to anything at any time. So, if I clear the contents and set the title to an empty string, it has the apparent effect of removing the menu (although it's still there and still highlights).
Barring another solution, have to do that, for now.
Is this possible at all on Mac? If I went down into Cocoa (don't know Cocoa), would I be able to maybe set up multiple menubars, or at least modify the menubar when the subpanel changes?
I was searching something else but as precisely I've just been working this one, what I do is
- delete the current menuBar if there's one
- menuBar=new QMenuBar(0);
- menuBar->setNativeMenuBar(true);
And it seems to work fine. Just for what it's worth.
A Cocoa application has only one menubar active at any given time, and you can modify and replace it. For instance, the (Cocoa) code below adds a new menu (with three items) to the menubar. It is also possible to edit and remove menus as well as menu items.
NSMenu *menubar = [NSApp mainMenu];
NSMenuItem *newBarMenuItem = [[[NSMenuItem alloc] initWithTitle:#"" action:NULL keyEquivalent:#""] autorelease];
NSMenu *newMenu = [[[NSMenu alloc] initWithTitle:#"New Menu"] autorelease];
NSMenuItem *menuItem1 = [[[NSMenuItem alloc] initWithTitle:#"Action 1" action:#selector(action1:) keyEquivalent:#""] autorelease];
NSMenuItem *menuItem2 = [[[NSMenuItem alloc] initWithTitle:#"Action 2" action:#selector(action2:) keyEquivalent:#""] autorelease];
[newMenu addItem:menuItem1];
[newMenu addItem:[NSMenuItem separatorItem]];
[newMenu addItem:menuItem2];
[menubar addItem:newBarMenuItem];
[menubar setSubmenu:newMenu forItem:newBarMenuItem];
[NSApp mainMenu] returns the application menu. A new menu item is added to the main menu/menubar, representing a submenu that contains three items, one of them being a separator.
It is also possible to replace the menubar by crafting an appropriate menu and sending [NSApp setMainMenu:menubarReplacement].
Yes, this is possible in a Qt app, and fairly common. :)
In your app you probably have code to build your menus, and install them into the menubar (using QMenuBar) in the first place. As Juan correctly points out, to alter the menu bar, you can delete that instance and regenerate a new menubar and its menus as needed.
In my own code, I just keep my original QMenuBar around, and call QMenuBar::clear() on the instance. This is an alternative to the delete/re-instantiate that Juan recommends, although either approach is likely valid. Then I repopulate the menubar with the currently needed menus.
I typically only rebuild the whole QMenuBar when the set of top-level menus, or the title of a top-level menu needs to change. More commonly, I am dynamically regenerating the actual menu items (QActions) and/or their state (like their text, whether they are enabled or not, checked or not, etc) within a given menu.
To dynamically regenerate a single menu's contents only, you can connect a method callback to that particular QMenu's aboutToShow signal, and rebuild the menu's items dynamically within that callback (don't forget to start with QMenu::clear() on the instance or you may end up with duplicate items in the menu!). When the QMenu pops up, it will show your dynamically rebuilt items/states. This method also works for dynamic regeneration of popup/context menus.

Flex ContextMenu Change the items dynamically

I am using a ContextMenu for an AdvancedDataGrid in my application. I could implement the normal context menu for the grid. Now, I am planning to make the context menu dynamic.
For example, if I click on a particular cell, I need to see only the items related to that cell in the Context Menu. Is there any way we can do that?
ContextMenu class contains a customItems property which is (quoting from Adobe livedocs):
An array of ContextMenuItem objects. Each object in the array represents a context menu item that you have defined. Use this property to add, remove, or modify these custom menu items.
To add new menu items, you create a ContextMenuItem object and then add it to the customItems array (for example, by using Array.push()). For more information about creating menu items, see the ContextMenuItem class entry.
I found the solution for this. Quite simple:
http://www.pubbs.net/flex/200905/73331/

Resources