To store QAction as static .. to use alround on code i use this piece of code....
but you must register on Q_OBJECT class to tell moc
typedef enum {
ID_NONE = 0,
WEB_PAGE_ACTUAL = 1000,
ID_ABOUT_QT = 1403
} CommandID;
struct Command {
Command() {
id = ID_NONE;
}
id = Id;
name = Name;
icon = Icon;
shortcut = Seq;
reciever = Reciever;
slot = Slot;
}
CommandID id;
};
class CommandStorage {
public:
static CommandStorage* instance();
void registerCommand(const Command&);
inline void clear() { cmds_.clear(); }
private:
CommandStorage() { }
static CommandStorage* st_;
};
#include <QtGui/QAction>
CommandStorage* CommandStorage::instance() {
if (st_ == 0)
st_ = new CommandStorage();
return st_;
}
void CommandStorage::registerCommand(const Command& cmd) {
CommandID id = cmd.id;
if (cmds_.contains(id))
delete cmds_[id];
if (!cmd.shortcut.isEmpty()) {
}
action->setShortcut(cmd.shortcut);
action->setData(id);
action->setIcon(cmd.icon);
QObject::connect(action,
SIGNAL(triggered
()), cmd.
reciever, qPrintable
(cmd.
slot));
cmds_[id] = action;
}
QAction* CommandStorage
::action(CommandID id
) { return cmds_[id];
}
CommandStorage* CommandStorage::st_ = 0;
typedef enum {
ID_NONE = 0,
WEB_PAGE_ACTUAL = 1000,
ID_ABOUT_QT = 1403
} CommandID;
struct Command {
Command() {
id = ID_NONE;
}
Command(CommandID Id, QString Name, QIcon Icon, QKeySequence Seq, QObject* Reciever, const QString& Slot) {
id = Id;
name = Name;
icon = Icon;
shortcut = Seq;
reciever = Reciever;
slot = Slot;
}
CommandID id;
QString name;
QIcon icon;
QKeySequence shortcut;
QObject* reciever;
QString slot;
};
class CommandStorage {
public:
static CommandStorage* instance();
void registerCommand(const Command&);
QAction* action(CommandID);
inline void clear() { cmds_.clear(); }
private:
CommandStorage() { }
static CommandStorage* st_;
QMap<CommandID, QAction*> cmds_;
};
#include <QtGui/QAction>
CommandStorage* CommandStorage::instance() {
if (st_ == 0)
st_ = new CommandStorage();
return st_;
}
void CommandStorage::registerCommand(const Command& cmd) {
CommandID id = cmd.id;
if (cmds_.contains(id))
delete cmds_[id];
QString keya = QString(); /* not Shortcut */
if (!cmd.shortcut.isEmpty()) {
keya = QString(" ") + cmd.shortcut.toString(QKeySequence::NativeText);
}
QAction* action = new QAction(cmd.icon, cmd.name + keya, 0);
action->setShortcut(cmd.shortcut);
action->setData(id);
action->setIcon(cmd.icon);
QObject::connect(action, SIGNAL(triggered()), cmd.reciever, qPrintable(cmd.slot));
cmds_[id] = action;
}
QAction* CommandStorage::action(CommandID id) {
return cmds_[id];
}
CommandStorage* CommandStorage::st_ = 0;
To copy to clipboard, switch view to plain text mode
register:
void PageEdit::createCommands()
{
CommandStorage* st = CommandStorage::instance(); /* new or exist? */
st->clear(); /* clear old command !!! or replace reciver....
CommandID id is on data from qaction ... to distinct its
*/
Command cmds[] = {
Command
(NEW_LAYER_AUTO,tr
("New flow text Layer"),
QIcon(":/img/view_remove.png"),
QKeySequence("F9"),ViewPanel,
SLOT(NewLayer
())),
Command
(NEW_LAYER_ABS,tr
("New absolute Layer"),
QIcon(":/img/view_sidetree.png"),
QKeySequence("F10"),ViewPanel,
SLOT(NewLayer
())),
Command()
};
for (unsigned i = 0; cmds[i].id != 0; i++) {
Command& cmd = cmds[i];
if (st->action(cmd.id) == 0) {
st->registerCommand(cmds[i]);
}
else {
st->action(cmd.id)->setIcon(cmd.icon);
}
}
}
void PageEdit::createCommands()
{
CommandStorage* st = CommandStorage::instance(); /* new or exist? */
st->clear(); /* clear old command !!! or replace reciver....
CommandID id is on data from qaction ... to distinct its
*/
Command cmds[] = {
Command(NEW_LAYER_AUTO,tr("New flow text Layer"),QIcon(":/img/view_remove.png"),QKeySequence("F9"),ViewPanel, SLOT(NewLayer())),
Command(NEW_LAYER_ABS,tr("New absolute Layer"),QIcon(":/img/view_sidetree.png"),QKeySequence("F10"),ViewPanel, SLOT(NewLayer())),
Command()
};
for (unsigned i = 0; cmds[i].id != 0; i++) {
Command& cmd = cmds[i];
if (st->action(cmd.id) == 0) {
st->registerCommand(cmds[i]);
}
else {
st->action(cmd.id)->setIcon(cmd.icon);
}
}
}
To copy to clipboard, switch view to plain text mode
append on a toolbar after toolbar-clear() or compose qmenu at live time....
CommandID viewMenu[] = { NEW_LAYER_AUTO , NEW_LAYER_ABS, NEW_LAYER_OOO , PRINT_CURRENT , SAVE_PAGE, OPEN_PAGE , CLEAR_PAGE , PASTE_LAYER , ID_NONE };
for (int j = 0; viewMenu[j] != ID_NONE; j++) {
CommandID id = viewMenu[j];
QAction* a_1
= CommandStorage
::instance()->action
(id
);
if (a_1) {
toolBar_save->addAction ( a_1 );
}
}
CommandID viewMenu[] = { NEW_LAYER_AUTO , NEW_LAYER_ABS, NEW_LAYER_OOO , PRINT_CURRENT , SAVE_PAGE, OPEN_PAGE , CLEAR_PAGE , PASTE_LAYER , ID_NONE };
for (int j = 0; viewMenu[j] != ID_NONE; j++) {
CommandID id = viewMenu[j];
QAction* a_1 = CommandStorage::instance()->action(id);
if (a_1) {
toolBar_save->addAction ( a_1 );
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks