PDA

View Full Version : Help with QT shortcuts?



fluffaykitties
27th January 2011, 05:39
Hi there. I'm trying to make it so that the "aButton" will be pressed when the user presses the a key on the keyboard.

This is my code thus far. It compiles and runs, but pressing the a key on the keyboard does nothing.

QPushButton *aButton=new QPushButton("&aButton", this);

QShortcut aShortCut = new QShortcut(QKeySequence(Qt::Key_A), this);
QObject::connect(aShortCut, SIGNAL(clicked()), this, SLOT(toggle()));

Any ideas why it's not working?

Thanks!

nish
27th January 2011, 06:10
you are connecting the wrong slot of the wrong object. Infact everything is wrong.

try this.

QObject::connect(aShortCut, SIGNAL(activated()), aButton, SLOT(animateClick()));

by the way,, the "&aButton" should be working as auto shortcut

fluffaykitties
27th January 2011, 06:19
Mmmkay. I changed the QObject line like you said and it still isn't working.

Not sure what you mean by "should be working as auto shortcut."

I'm a newbie to QT, so I apologize ahead of time for my lack of knowledge.

nish
27th January 2011, 08:20
read the documentation of QShortCut and QAbstractButton, you just might be doing a little mistake here and there....
Can you post a minimal compilable program reproducing the problem?

totem
27th January 2011, 08:42
why not using QAbstractButton::setShortcut ?

BalaQT
27th January 2011, 08:47
hi,
as totem said,

aButton->setShortcut(Qt::Key_A)
is the easiest way

Bala

fluffaykitties
27th January 2011, 17:41
Okay, so I changed the lines to this:


QPushButton *aButton=new QPushButton("&aButton", &w);

aButton->setShortcut(Qt::Key_A);
// QObject::connect(aShortCut, SIGNAL(activated()), aButton, SLOT(animateClick()));

(I commented out that last line....I think I was supposed to do that now that I'm using the aButton->setShortcut. Still issue though. It compiles and runs, but the aButton isn't clicked when I press the letter a on my keyboard.

Any other ideas?

ComaWhite
27th January 2011, 17:45
are you connecting to any signals at all?

fluffaykitties
27th January 2011, 17:48
I was before with that last line that I commented out...

nish
28th January 2011, 11:01
You need to provide a minimal compilable example reproducing the problem.

fluffaykitties
28th January 2011, 15:47
I'd rather not just post it on here, as it is a school project. Perhaps I can private message it to you? Which files would you need to be able to see the problem?

fluffaykitties
29th January 2011, 06:46
Anyone else have any ideas?

I need to finish this soon. =/

I feel like I ALMOST have it...like I'm off by one tiny error or something.

DnyaneshwarMane
16th April 2018, 13:32
Working ..............

QPushButton *aButton=new QPushButton("&aButton", this);
QShortcut *aShortCut = new QShortcut(QKeySequence(Qt::Key_A), aButton);
QObject::connect(aShortCut, SIGNAL(activated()), aButton, SLOT(animateClick()));