This forum has been archived. All content is frozen. Please use KDE Discuss instead.

add actions to actioncollection from different classes

Tags: None
(comma "," separated)
xyfix
Registered Member
Posts
110
Karma
0
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?
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
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]
xyfix
Registered Member
Posts
110
Karma
0
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.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
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]
xyfix
Registered Member
Posts
110
Karma
0
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.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
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]
xyfix
Registered Member
Posts
110
Karma
0
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. ;)
xyfix
Registered Member
Posts
110
Karma
0
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.
Code: Select all
mymainwindow.h:

class myMainWindow : public KParts::MainWindow
{
private:
 struct sessionData
 {
   subSession* subsession;
   sessionData(): subsession(0){}
 }

 QToolBar *m_toolBar;

public:
 void createAllActions( sessionData &sessiondata );
}

mymainwindow.cpp:

myMainWindow::myMainWindow()
{
  m_toolBar = new QToolBar("filetoolbar", this );
  createAllActions( someRefTosessionData );
  createGUI(0L);
}

void myMainWindow::createAllActions( sessionData &sessionData )
{
  sessionData.subsession->addToMenuBar( menuBar());
  sessionData.subsession->addToToolBar( m_toolBar );

  sessionData.subsession->addToActionCollection( actionCollection());
}

subsession.h:

class subSession()
{
  void addToMenuBar( QMenuBar* menubar );
  void addToToolBar( QToolBar* toolbar );
  void addToActionCollection( KActionCollection* actioncollection );
}

subsession.cpp:

subSession::addToMenu( QMenuBar* menubar )
{
  QMenu* m_menu = new QMenu();

  m_menu->addAction( someAction );
 
  menubar->insertMenu( someOtherAction, m_menu );
}

subSession::addToToolBar( QToolBar* toolbar )
{
  toolbar->addAction( someAction );
}

subSession::addToActionCollection( KActionCollection *actioncollection )
{
  actioncollection->addAction( someAction );
}
xyfix
Registered Member
Posts
110
Karma
0
AH, I found it, I just need to run the createGUI after I had added the actions to the actionCollection. It works now.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]