Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Qt 4.2 Shortcut Management

  1. #1
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Qt 4.2 Shortcut Management

    Is there a good tutorial on how to create a simple shortcut manager in Qt 4.2?

    I found this one on google, but it's using a lot of deprecated functions, and I don't know enough yet to update all of it: http://doc.trolltech.com/qq/qq14-actioneditor.html

    Thanks for the help,
    dave

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    I've written such a component myself for use with my IDE project. As soon as I'll have enough time I'll release a proper package but in the meantime you can get the sources (licensed under GPL) from Edyuk's SVN trunk or source packages

    It includes a translation-aware shortcut manager class (QShortcutManager) which can register one or more actions under a given context and assign them a shorcut (either the default one or another set by the user) and a configuration dialog, QShortcutDialog which shows a tree of registered shortcuts and allow the user to set shortcuts through a custom input dialog (double click on tree item, type your shortcut, click on the OK button and you're done!).
    The shortcuts are stored in XML files in a predefined location (you may need to change the source code to fit your needs in this domain...)

    Hope this helps you.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    wow this sounds like exactly what i've been looking for. I will check it out and let you know how it goes. Thanks so much for the quick reply,

    Dave

  4. #4
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    I downloaded edjuk from sourceforge the other day but I don't see those files anywhere in there. Have they been renamed or moved?

    it looks like they are supposed to be in trunk/src/lib/qcumber but I don't see them there in the local copy i have.

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Quote Originally Posted by dvmorris View Post
    I downloaded edyuk from sourceforge the other day but I don't see those files anywhere in there. Have they been renamed or moved?

    it looks like they are supposed to be in trunk/src/lib/qcumber but I don't see them there in the local copy i have.
    Are you sure you get the right version??? If you used SVN the files must be here (unless the checkout failed someway), if you downloaded packages they must be these of version 0.8.0-rc1.

    the path to these files is : $PATH_TO_EDYUK/src/lib/qcumber where PATH_TO_EDYUK corresponds to the place where you extracted the archive or did your SVN checkout...
    the files you're interested in are : qshortcutmanager.h qshortcutmanager.cpp qshortcutdialog.h qshortcutdialog.cpp

    Alternatively, you can always use the SVN browse option on Sourceforge, access these files and copy there content by hand.
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #6
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    is there an example in the svn checkout that uses the shortcut manager? like a hello world kind of example or something?

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Quote Originally Posted by dvmorris View Post
    is there an example in the svn checkout that uses the shortcut manager? like a hello world kind of example or something?
    Well I'm afraid I did not make any example... However it is really simple to use and here is a sample code :

    Qt Code:
    1. #include "qshortcutmanager.h"
    2.  
    3. #include <QMenu>
    4. #include <QAction>
    5. #include <QMenuBar>
    6. #include <QMainWindow>
    7. #include <QMessageBox>
    8. #include <QApplication>
    9.  
    10. int main(int argc, char **argv)
    11. {
    12. QApplication app(argc, argv);
    13.  
    14. QShortcutManager sm;
    15.  
    16. QMessageBox ms(&main);
    17. ms.setText( "This is a stub message box. The\n"
    18. "only nice thing about it is that\n"
    19. "it is triggered via a configurable\n"
    20. "shortcut\n\n"
    21. "So long buddy!");
    22.  
    23. QMenu help("&Help", &main);
    24.  
    25. QAction *a = new QAction("Show message box", &main);
    26. ms.connect( a , SIGNAL( triggered() ),
    27. SLOT ( exec() ) );
    28.  
    29. sm.registerAction(a, "Help", "CTRL+M");
    30. help.addAction(a);
    31.  
    32. QMenu settings("&Settings", &main);
    33.  
    34. QAction *b = new QAction("Configure &shortcuts", &main);
    35. sm.connect( b , SIGNAL( triggered() ),
    36. SLOT ( configure() ) );
    37.  
    38. sm.registerAction(b, "Settings", "CTRL+S");
    39. settings.addAction(b);
    40.  
    41. main.menuBar()->addMenu(&settings);
    42. main.menuBar()->addMenu(&help);
    43. main.show();
    44.  
    45. return app.exec();
    46. }
    To copy to clipboard, switch view to plain text mode 

    Of course to compile this you need to link against Edyuk core library or to pack the need components in your project (no path relocation in the example below : it is assumed that you copied the files in your project folder...) :

    Qt Code:
    1. QT *= xml
    2.  
    3. HEADERS += qshortcutmanager.h qshortcutdialog.h
    4. SOURCES += qshortcutmanager.cpp qshortcutdialog.cpp
    5.  
    6. FORMS += shortcutdialog.ui
    To copy to clipboard, switch view to plain text mode 

    Hope this helps.

    P.S : I just checked Edyuk sources and the settings storage path is incorrectly set. If you wish to use QShortcutManager within one of your application you should change some code in qshortcutmanager.cpp Replace the content of the QShortcutManager::file(const QString& lang) function with the code below :
    Qt Code:
    1. QString QShortcutManager::file(const QString& lang)
    2. {
    3. return QDir::homePath()
    4. + QDir::separator()
    5. + "."
    6. + QApplication::applicationName()
    7. + QDir::separator()
    8. + "shortcuts_"
    9. + lang
    10. + ".xml";
    11. }
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  8. #8
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Wow this example is really great. I think I have most of it integrated into my code properly but I am getting an error:

    Qt Code:
    1. qshortcutmanager.cc: In member function 'void QShortcutManager::destroyed(QObject*)':
    2. qshortcutmanager.cc:396: error: 'quintptr' was not declared in this scope
    To copy to clipboard, switch view to plain text mode 

    I have put qcumber.h into my directory, and the four files for the qshortcut stuff, but I don't really see any other files I'm supposed to include.

  9. #9
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Quote Originally Posted by dvmorris View Post
    Wow this example is really great. I think I have most of it integrated into my code properly but I am getting an error:

    Qt Code:
    1. qshortcutmanager.cc: In member function 'void QShortcutManager::destroyed(QObject*)':
    2. qshortcutmanager.cc:396: error: 'quintptr' was not declared in this scope
    To copy to clipboard, switch view to plain text mode 
    I have put qcumber.h into my directory, and the four files for the qshortcut stuff, but I don't really see any other files I'm supposed to include.
    This is weird... quintptr is a very convinient typedef defined in qglobal.h
    I have no idea why it wouldn't compile properly on your box but you can always replace any occurence with : (void*)
    Last edited by fullmetalcoder; 28th February 2007 at 17:35. Reason: typo
    Current Qt projects : QCodeEdit, RotiDeCode

  10. #10
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    I just tried adding

    #include <QtGlobal>

    to my qshortcutmanager.cpp file, but it didn't seem to work. It looks like quintptr is declared in QtGlobal, but that change gives the same error.

    http://stuff.mit.edu/afs/athena/soft.../qtglobal.html

  11. #11
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    you know what? It may be a problem that I am actually using Qt 4.1 and not Qt 4.2.

    Qt 4.2 will not install on my mac for some reason, it gets two errors right at the end:

    http://www.qtcentre.org/forum/f-inst...os-x-5867.html

  12. #12
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Quote Originally Posted by dvmorris View Post
    I just tried adding

    #include <QtGlobal>

    to my qshortcutmanager.cpp file, but it didn't seem to work. It looks like quintptr is declared in QtGlobal, but that change gives the same error.

    http://stuff.mit.edu/afs/athena/soft.../qtglobal.html
    it is documented here : http://stuff.mit.edu/afs/athena/soft...intptr-typedef

    and should be defined in any context provided that you have ANY Qt header included in the source. This is the case in qshortcutmanager.cpp so my only suggestion is to try #include "qglogal.h" and if it still doesn't work replace the occurence or add the typedef by hand in the source file :
    Qt Code:
    1. typedef void* quintptr;
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  13. #13
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Quote Originally Posted by dvmorris View Post
    you know what? It may be a problem that I am actually using Qt 4.1 and not Qt 4.2.
    That's absolutely right and I hadn't spotted it... I'll place some macro'ed typedef to avoid troubles.
    Current Qt projects : QCodeEdit, RotiDeCode

  14. #14
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    does it save and load the shortcut config files automatically, or do i need to write that in somewhere as another action?

    Also, I just had the strangest problem, and I have no idea what really was happening. When I create the configureShortcuts action, if I use the word "config" or "configure" in the text, it won't load the menu into the menuBar.

    Qt Code:
    1. manageShortcutsAct = new QAction(tr("confshortcuts"),this);
    2. sm->connect( manageShortcutsAct , SIGNAL( triggered() ), SLOT ( configure() ) );
    3. sm->registerAction(manageShortcutsAct, "Configure Shortcuts", "CTRL+M");
    To copy to clipboard, switch view to plain text mode 
    that works.
    Qt Code:
    1. manageShortcutsAct = new QAction(tr("Config Shortcuts"),this);
    2. sm->connect( manageShortcutsAct , SIGNAL( triggered() ), SLOT ( configure() ) );
    3. sm->registerAction(manageShortcutsAct, "Configure Shortcuts", "CTRL+M");
    To copy to clipboard, switch view to plain text mode 
    that does not. I guess it's because I'm using the part of the phrase from the registerAction function. does that make sense?

  15. #15
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Quote Originally Posted by dvmorris View Post
    does it save and load the shortcut config files automatically, or do i need to write that in somewhere as another action?
    If you modified qshortcutmanager.cpp, called QApplication::setApplicatioName() and ensured that the settings directory ( QDir::homePath + "/." + QApplication::applicationName() ) exists then saving/reloading should be done flawlessly but as the code you got doesn't do all these steps automatically I guess it does not work properly yet...

    Quote Originally Posted by dvmorris View Post
    Also, I just had the strangest problem, and I have no idea what really was happening. When I create the configureShortcuts action, if I use the word "config" or "configure" in the text, it won't load the menu into the menuBar.

    Qt Code:
    1. manageShortcutsAct = new QAction(tr("confshortcuts"),this);
    2. sm->connect( manageShortcutsAct , SIGNAL( triggered() ), SLOT ( configure() ) );
    3. sm->registerAction(manageShortcutsAct, "Configure Shortcuts", "CTRL+M");
    To copy to clipboard, switch view to plain text mode 
    that works.
    Qt Code:
    1. manageShortcutsAct = new QAction(tr("Config Shortcuts"),this);
    2. sm->connect( manageShortcutsAct , SIGNAL( triggered() ), SLOT ( configure() ) );
    3. sm->registerAction(manageShortcutsAct, "Configure Shortcuts", "CTRL+M");
    To copy to clipboard, switch view to plain text mode 
    that does not. I guess it's because I'm using the part of the phrase from the registerAction function. does that make sense?
    You may want to take a look at the doc (only Doxygen style comments in the source ATM) for this... Actually QShortcutManager doesn't interfere with the action names but only modify the assigned shortcuts. the parameters passed to registerAction() are as follow :
    • a QAction to be managed
    • a QString indicating the context of the action (this is used when storing settings and populating the tree widget of the shortcut dialog ( a '/' in the name indicate a category separator i.e nested categories visible in the tree)
    • a QString indicating the default shortcut. When no shortcuts is set in the configuration it is used. Also when conflicts are encountered when setting shortcuts through the dialog, it may be used as a fallback value.
    The action name is set through the QAction constructor or the setText() method. The menu name is set through the QMenu constructor (and possibly a method but I can't remember which one...)

    Do you mean that using the same text as action name and context causes a bug? Well, I guess I have to check this. Anyway you should indeed avoid using the same text in action name and context because the shortcut dialog would get ugly... As explained above context is a category (meant to contain several actions)...

    Hope this helps you.
    Current Qt projects : QCodeEdit, RotiDeCode

  16. #16
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    I tried setting the applicationName to "topmod" in my main function, and I created the directory

    /Users/dave/.topmod/shortcuts/

    with no files in it. I have one shortcut set to register into the shortcutmanager class, and it gives me an error when i debug saying:

    "Unable to access shortcuts..."

    I even tried this line instead of all that Qt::homeDir() stuff...

    return "/Users/dvmorris/.topmod/shortcuts/shortcuts_en_us.xml";

    none of that seemed to work either.

    *sorry. it works fine now. Thanks for your help again...
    Last edited by dvmorris; 1st March 2007 at 00:36.

  17. #17
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    How do I use the numbers as shortcuts? I can't seem to get them to work by themselves without adding a CTRL or ALT key before them.

    For example, I have a QTabWidget with five pages and I setup five modes like this

    Qt Code:
    1. modeBasicsAct = new QAction(QIcon(":/images/mode_basics.png"), tr("&Basics"), this);
    2. sm->registerAction(modeBasicsAct, "Modes", "1");
    3. modeBasicsAct->setStatusTip(tr("Switch to Basics Mode"));
    4. connect(modeBasicsAct, SIGNAL(triggered()), this, SLOT(switchMode(0)));
    To copy to clipboard, switch view to plain text mode 

    and I have a slot function defined like this:

    Qt Code:
    1. void MainWindow::switchMode(int pageId){
    2. modesTabWidget->setCurrentIndex(pageId);
    3. }
    To copy to clipboard, switch view to plain text mode 

    The 1,2,3,4,5 shortcuts I set with registerAction do not switch the TabWidget's currentIndex to 0,1,2,3,or 4, like I write in the slot function.

    Do you have any ideas as to why that won't work? I also noticed that it doesn't seem to work with any other shortcuts like CTRL+L. Maybe this has nothing to do with the numbers problem. anyways, sorry to bother you so much. I really appreciate your help. Your shortcut manager is phenomenal.

  18. #18
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Quote Originally Posted by dvmorris View Post
    How do I use the numbers as shortcuts? I can't seem to get them to work by themselves without adding a CTRL or ALT key before them.

    For example, I have a QTabWidget with five pages and I setup five modes like this

    Qt Code:
    1. modeBasicsAct = new QAction(QIcon(":/images/mode_basics.png"), tr("&Basics"), this);
    2. sm->registerAction(modeBasicsAct, "Modes", "1");
    3. modeBasicsAct->setStatusTip(tr("Switch to Basics Mode"));
    4. connect(modeBasicsAct, SIGNAL(triggered()), this, SLOT(switchMode(0)));
    To copy to clipboard, switch view to plain text mode 
    and I have a slot function defined like this:

    Qt Code:
    1. void MainWindow::switchMode(int pageId){
    2. modesTabWidget->setCurrentIndex(pageId);
    3. }
    To copy to clipboard, switch view to plain text mode 
    The 1,2,3,4,5 shortcuts I set with registerAction do not switch the TabWidget's currentIndex to 0,1,2,3,or 4, like I write in the slot function.

    Do you have any ideas as to why that won't work? I also noticed that it doesn't seem to work with any other shortcuts like CTRL+L. Maybe this has nothing to do with the numbers problem. anyways, sorry to bother you so much. I really appreciate your help. Your shortcut manager is phenomenal.
    The shortcut management provided is just a (very ) convinient wrapper over Qt shortcut mechanism but it doesn't modify it... AFAIK shortcuts NEED modifiers to work because other keys are generally forwarded to other widgets and this is especially true if you have, say, any kind of input widget in your current tab...

    Besides your slots registration is wrong : the SLOT() macro is passed a qualified function name ( name + parameters types) and no arguments... That's probably why your code is failing. If you use a special slot for each mode switching and make sure that the child widgets don't accept number keys input it should work...
    Current Qt projects : QCodeEdit, RotiDeCode

  19. #19
    Join Date
    Feb 2007
    Posts
    81
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    ok I understand a little better now. I rewrote the slot functions so there is a unique one for each of the five TabWidget Pages. here is one of them:

    Qt Code:
    1. //inside MainWindow class in .h file
    2. private slots:
    3. void switchBasicsMode();
    4.  
    5. //in cpp file
    6. void MainWindow::switchBasicsMode(){
    7. modesTabWidget->setCurrentIndex(0);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. modeBasicsAct = new QAction(QIcon(":/images/mode_basics.png"), tr("&Basics"), this);
    2. sm->registerAction(modeBasicsAct, "Modes", "CTRL+9");
    3. modeBasicsAct->setStatusTip(tr("Switch to Basics Mode"));
    4. connect(modeBasicsAct, SIGNAL(triggered()), this, SLOT(switchBasicsMode()));
    To copy to clipboard, switch view to plain text mode 

    but that doesn't seem to work either. the signals slots demo here: http://doc.trolltech.com/4.2/signalsandslots.html is not very helpful either.

    The shortcut doesn't seem to do anything. But all the shortcuts for actions that are attached to menuBar items work fine (except single numbers and CTRL+Q). Do I just need to create a dummy menuBar for the five modes so that the shortcuts will do something?

  20. #20
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.2 Shortcut Management

    Quote Originally Posted by dvmorris View Post
    The shortcut doesn't seem to do anything. But all the shortcuts for actions that are attached to menuBar items work fine (except single numbers and CTRL+Q). Do I just need to create a dummy menuBar for the five modes so that the shortcuts will do something?
    I forgot to point this out as well... Qt, as far as I tested, forward shortcuts to VISIBLE actions only... If you don't attach your actions to menubars, toolbars, buttons or anything that can manage them, then the shortcut will never trigger the actions (except maybe if you play with the shortcut context but even that may fail...)
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. Replies: 1
    Last Post: 9th February 2007, 09:41
  2. shortcut with more than one key
    By ChasW in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2007, 06:38
  3. Trying to set shortcut
    By mikro in forum Newbie
    Replies: 2
    Last Post: 30th November 2006, 09:55
  4. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 07:21
  5. Multi frame management ... is it possible ?
    By yellowmat in forum Newbie
    Replies: 8
    Last Post: 25th January 2006, 10:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.