![]() Registered Member ![]()
|
I have a class 1 with a actioncollection and inside this class 1 I create an instance of another class 2 in which I define actions. How can I add the actions of class 2 to the actioncollection of class 1?
|
![]() Administrator ![]()
|
Assuming Class 2 has a parent of Class 1, you could access the actionCollection object of Class 1 from Class 2 either by using a function to return the pointer, or by making it a friend class.
KDE Sysadmin
[img]content/bcooksley_sig.png[/img] |
![]() Registered Member ![]()
|
not sure if I implementing it correctly, but here a short version of what I have:
Class1::Class1() { Class2 class2(this); } and then in Class2 Class2::Class2( Class1* class1) :m_class1(class1) { KAction* action = KStandardAction::print( this, SLOT(slotFilePrint()), m_class1->actionCollection()); } Class2::slotFilePrint(){} I'm using the pointer approach and not the fiend one. I can pastebin both classes if you like because they are LGPL. But I thought that the above representation of the classes is enough to solve the problem. |
![]() Administrator ![]()
|
Yes, that is correct, although you will need to add the actionCollection() function to Class1 if it is not a subclass of KXMLGUIWindow. It is unusual for applications to create their own action collection.
KDE Sysadmin
[img]content/bcooksley_sig.png[/img] |
![]() Registered Member ![]()
|
bcoocksley,
I already have some actions in class1 that are added to the actionCollection() Class1::Class1() { setupActions(); Class2 class2(this); } Class1::setupActions() { KAction* action = new KAction ..... actionCollection()->addAction("new_Picture", action ); } so I thought that I could add actions to this actionCollection from Class2. But nothing happens when I click on the menu item in the GUI. |
![]() Administrator ![]()
|
The likely problem is that Class2 is being created on the stack instead of on the heap.
Therefore, it is being deleted when it goes out-of-scope and the actions are destroyed. Change it like follows Class2 * class2 = new Class2(this); You probably want to create a private data member in Class1 though to store the pointer.
KDE Sysadmin
[img]content/bcooksley_sig.png[/img] |
![]() Registered Member ![]()
|
sorry, I forgot to post it but I had already found my error and you're right about the out of scope of the object. Any way thank you for pointing it out.
![]() |
![]() Registered Member ![]()
|
My previous problem is solved. But I still have one another question concerning the addition of actions from outside the KParts::MainWindow. Why is it that when I have the underneath code I get the actions inserted via addToMenuBar and addToToolBar but not from addToActionCollection.
|
![]() Registered Member ![]()
|
AH, I found it, I just need to run the createGUI after I had added the actions to the actionCollection. It works now.
|
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]