Qt-interest Archive, May 2007
Example of moving QMenus into separate Class
Message 1 in thread
I am just starting to create my application and have read through a few
of the Qt books (4.x) based and I have my first question. I all the
examples when the menus are created, all the creation code and QAction
instantiation code is in the QMainWIndow derived class. This seems like
a lot of code to put into the QMainWindow class and I was wondering if
someone had an example of how to put all that code into a separate
class?
Thanks
Mike Jackson
--
[ signature omitted ]
Message 2 in thread
What kind of class are you thinking of? Just a worker class? You
*could* create a static worker function, passing in everything. If the
actions are to be shared, then some thought needs to go into who retains
the actions. You could create a "MyGui" singleton class which has
accessor methods for the actions which lazily create them (and retains
them in private member variables). Then your main window, toolbars, etc
can share the actions.
It really depends on what you're modeling.
HTH,
Susan
Mike Jackson wrote:
> I am just starting to create my application and have read through a
> few of the Qt books (4.x) based and I have my first question. I all
> the examples when the menus are created, all the creation code and
> QAction instantiation code is in the QMainWIndow derived class. This
> seems like a lot of code to put into the QMainWindow class and I was
> wondering if someone had an example of how to put all that code into a
> separate class?
>
> Thanks
> Mike Jackson
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
---
This communication contains confidential information. If you are not the intended recipient please return this email to the sender and delete it from your records.
Diese Nachricht enthält vertrauliche Informationen. Sollten Sie nicht der beabsichtigte Empfänger dieser E-mail sein, senden Sie bitte diese an den Absender zurück und löschen Sie die E-mail aus Ihrem System.
--
[ signature omitted ]
Message 3 in thread
Hi Mike.
Typically I subclass QMainWindow and define the following functions that
are called in the constructor: createActions(), createMenus(),
createToolbars(), createControls(), and initialize(). I generally don't
subclass QMenu unless I need to do something that isn't easily done with
QMenu (self-contained dynamic menus or whatnot). However, I attached a
file with a simple example of a self-contained subclass of QMenu. Keep
in mind, though, that your main window is now going to create an
instance of MyMenu but will have no knowledge of the actions it
contains, which rules out using them in a toolbar or elsewhere. In my
mind, subclassing QMenu makes more sense if you're going to add to or
change the functionality, but not as much sense if you're just looking
to separate code.
HTH.
Darrik
Mike Jackson wrote:
> I am just starting to create my application and have read through a
> few of the Qt books (4.x) based and I have my first question. I all
> the examples when the menus are created, all the creation code and
> QAction instantiation code is in the QMainWIndow derived class. This
> seems like a lot of code to put into the QMainWindow class and I was
> wondering if someone had an example of how to put all that code into a
> separate class?
>
> Thanks
> Mike Jackson
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]
mymenu.h:
#include <QAction>
#include <QMenu>
class MyMenu : public QMenu
{
Q_OBJECT
public:
MyMenu(QWidget *parent = 0);
signals:
void action1triggered();
void action2triggered();
private:
QAction *action1;
QAction *action2;
};
mymenu.cpp:
#include "mymenu.h"
MyMenu::MyMenu(QWidget *parent) :
QMenu(parent)
{
action1 = new QAction(tr("action 1"), this);
addAction(action1);
connect(action1, SIGNAL(triggered()), this, SIGNAL(action1triggered()));
action2 = new QAction(tr("action 2"), this);
addAction(action2);
connect(action2, SIGNAL(triggered()), this, SIGNAL(action2triggered()));
}
Message 4 in thread
I was actually thinking of a separate class that _contains_ all the
menu/toolbar QActions and QMenus and such. I was just thinking that if
I had 30 or 40 menus then that is a lot of code to put into the
QMainWindow class and seemed like it should lend it self to some sort
of private class or friend class that could contain the actual
implementation.
So inside of the QMainWindow constructor I would have:
{
MyMenuContainter containter = new MyMenuContainer(this);
container->setupMenus();
container->setupActiosn();
}
But I am not sure of the ramifications of this design in terms of Signals/Slots
Thanks
Mike Jackson
On 2007-05-17 23:37:09 -0400, darrik mazey <darrik@xxxxxxxxxxxxxxxxxxx> said:
>
> Hi Mike.
>
> Typically I subclass QMainWindow and define the following functions that
> are called in the constructor: createActions(), createMenus(),
> createToolbars(), createControls(), and initialize(). I generally don't
> subclass QMenu unless I need to do something that isn't easily done with
> QMenu (self-contained dynamic menus or whatnot). However, I attached a
> file with a simple example of a self-contained subclass of QMenu. Keep
> in mind, though, that your main window is now going to create an
> instance of MyMenu but will have no knowledge of the actions it
> contains, which rules out using them in a toolbar or elsewhere. In my
> mind, subclassing QMenu makes more sense if you're going to add to or
> change the functionality, but not as much sense if you're just looking
> to separate code.
>
> HTH.
>
> Darrik
>
> Mike Jackson wrote:
>> I am just starting to create my application and have read through a
>> few of the Qt books (4.x) based and I have my first question. I all
>> the examples when the menus are created, all the creation code and
>> QAction instantiation code is in the QMainWIndow derived class. This
>> seems like a lot of code to put into the QMainWindow class and I was
>> wondering if someone had an example of how to put all that code into a
>> separate class?
>>
>> Thanks
>> Mike Jackson
>>
>> --
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
>> "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 5 in thread
It should be easy enough to create a top level container class (not a
QObject derivation) to hold the actions, provided they are shared.
If these menus and actions aren't shared, I don't see any benefit to
having a container class like this. Using methods as was previously
suggested is the way to go IMHO.
Mike Jackson wrote:
> I was actually thinking of a separate class that _contains_ all the
> menu/toolbar QActions and QMenus and such. I was just thinking that if
> I had 30 or 40 menus then that is a lot of code to put into the
> QMainWindow class and seemed like it should lend it self to some sort
> of private class or friend class that could contain the actual
> implementation.
>
> So inside of the QMainWindow constructor I would have:
>
> {
> MyMenuContainter containter = new MyMenuContainer(this);
> container->setupMenus();
> container->setupActiosn();
> }
>
> But I am not sure of the ramifications of this design in terms of
> Signals/Slots
>
> Thanks
> Mike Jackson
>
> On 2007-05-17 23:37:09 -0400, darrik mazey
> <darrik@xxxxxxxxxxxxxxxxxxx> said:
>
>>
>> Hi Mike.
>>
>> Typically I subclass QMainWindow and define the following functions that
>> are called in the constructor: createActions(), createMenus(),
>> createToolbars(), createControls(), and initialize(). I generally don't
>> subclass QMenu unless I need to do something that isn't easily done with
>> QMenu (self-contained dynamic menus or whatnot). However, I attached a
>> file with a simple example of a self-contained subclass of QMenu. Keep
>> in mind, though, that your main window is now going to create an
>> instance of MyMenu but will have no knowledge of the actions it
>> contains, which rules out using them in a toolbar or elsewhere. In my
>> mind, subclassing QMenu makes more sense if you're going to add to or
>> change the functionality, but not as much sense if you're just looking
>> to separate code.
>>
>> HTH.
>>
>> Darrik
>>
>> Mike Jackson wrote:
>>> I am just starting to create my application and have read through a
>>> few of the Qt books (4.x) based and I have my first question. I all
>>> the examples when the menus are created, all the creation code and
>>> QAction instantiation code is in the QMainWIndow derived class. This
>>> seems like a lot of code to put into the QMainWindow class and I was
>>> wondering if someone had an example of how to put all that code into a
>>> separate class?
>>>
>>> Thanks
>>> Mike Jackson
>>>
>>> --
>>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
>>> "unsubscribe" in the subject or the body.
>>> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
---
This communication contains confidential information. If you are not the intended recipient please return this email to the sender and delete it from your records.
Diese Nachricht enthält vertrauliche Informationen. Sollten Sie nicht der beabsichtigte Empfänger dieser E-mail sein, senden Sie bitte diese an den Absender zurück und löschen Sie die E-mail aus Ihrem System.
--
[ signature omitted ]