PDA

View Full Version : Shortcut: CTRL + SHIFT + "letters"



smarinr
18th May 2009, 18:07
Hi,
im having a problem with 3 key shortcut. I'm trying to add to an action this:

action->->setShortcut(QKeySequence( (Qt::CTRL+Qt::SHIFT), Qt::Key_1)) but isn't trigeer, does anyone have an idea how to implement a 3 Key Shortcut??

THANKS

wysota
18th May 2009, 19:01
Shouldn't it be:

QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_1)
?

smarinr
18th May 2009, 19:07
OH YEAH, im sorry, wrong code :P but QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_1) also didn't work and QKeySequence(Qt::CTRL,Qt::SHIFT,Qt::Key_1). My concern is if it is able to do it... or somebody have same problems. I have also 2 keys shortcuts in the same class and there are fine.

wysota
18th May 2009, 19:55
Try:

QKeySequence("Ctrl+Shift+1")

smarinr
18th May 2009, 20:51
nothing... I changed it to
QKeySequence(tr("Ctrl+Shift+1")) but nothing has trigger. Put on it a breakpoint to verify that isn't triggering.

wysota
18th May 2009, 21:09
So obviously the reason is not here. Can we see a larger snippet of code? How do you create and use the action?

smarinr
18th May 2009, 21:12
void QtContactMenu::createSortMenu( QObject* parent )
{


createAction( &_actionSortAlpha, pMenu, tr("Alphabetically"), true, QtEnumSortOption::SortAlpha, "", QKeySequence(tr("Ctrl+Shift+1")) );
createAction( &_actionSortPresence, pMenu, tr("Presence"), true, QtEnumSortOption::SortPresence, "", QKeySequence(tr("Ctrl+Shift+2")) );
createAction( &_actionSortRandom, pMenu, tr("Random"), true, QtEnumSortOption::SortRandom, "", QKeySequence(tr("Ctrl+Shift+3")) );

}

void QtContactMenu2::createAction( QAction** ppAction, QMenu* pMenu, const QString& text, bool bCheckable, int data, const char* iconPath, QKeySequence & shortcut ){
*ppAction = new QAction( text, NULL ); //, parent );

(*ppAction)->setCheckable( bCheckable );
(*ppAction)->setData(qVariantFromValue(data) );

if ( iconPath && (strlen( iconPath ) > 0 ) )
{
QIcon icon1;
icon1.addPixmap(QPixmap( QString::fromUtf8(iconPath)), QIcon::Normal, QIcon::Off);
(*ppAction)->setIcon(icon1);
}
if (shortcut){
(*ppAction)->setShortcut(shortcut);
}
pMenu->addAction( *ppAction );

}

wysota
18th May 2009, 21:40
First of all please provide a parent for the action.
What is pMenu (I mean the base class of it)? Is it visible anywhere on the screen?

smarinr
18th May 2009, 21:46
pMenu is a QMenu object, thats the menu on the app.

What difference makes the parent?? I'm asking because if you set this:

createAction( &_actionSortAlpha, pMenu, tr("Alphabetically"), true, QtEnumSortOption::SortAlpha, "", QKeySequence(tr("Ctrl+1")) );

this shortcut works without any problem...

wysota
18th May 2009, 22:26
This works for me just fine:

#include <QtGui>


int main(int argc, char **argv){
QApplication app(argc, argv);
QToolButton pb;
QAction *act = new QAction(&app);
act->setText("text");
act->setShortcut(QKeySequence("Ctrl+Shift+1"));
pb.setDefaultAction(act);
QObject::connect(act, SIGNAL(triggered()), &app, SLOT(quit()));
pb.show();
return app.exec();
}

The only reason I can think of is that the menu is not the active widget and thus the action is inactive. But then the second snippet you posted shouldn't have worked as well.

smarinr
18th May 2009, 23:00
thats the wear part, 2 keys works but 3 dont.

wysota
18th May 2009, 23:13
Could you test if my example works for you?

smarinr
21st May 2009, 22:36
Hey Wisota, is working but only with keyboard numbers, the numbers on the numpad of the keyboard dont react on it, I do it just once but no more.... Are keyboard nums and numpad nums different for QT??

wysota
21st May 2009, 22:57
Do you have NumLock enabled on your keyboard?

smarinr
21st May 2009, 23:12
Yeap :p With or Without the NumLock, Keyboard nums works but NumPad don't

wysota
22nd May 2009, 00:00
Yeap :p With or Without the NumLock, Keyboard nums works but NumPad don't

It works for me just fine when using the numeric key pad with NumLock active. Or at least with a pseudo-keypad (I have a laptop without a real keypad, only one made of special function keys).

smarinr
22nd May 2009, 00:44
Maybe.... im not pressing CRTL + SHIFT + 1 but I'm pressing CTL + ! (which is SHIFT + 1)> That why sometimes works

wysota
22nd May 2009, 09:34
I don't know what you are pressing :) "!" is "!", "Shift+1" is "Shift+1". You can have different keyboard layouts and some of them won't have "!" as "Shift+1". It works for me when I press Ctrl, Shift and 1.