1

enter image description here

Like this, I wanna print the shortcut text.

But I don't wanna use code like this :

this->newAction->setShortcut(QKeySequence::New);

Because I handle shortcuts by using my KeyAction classes.

If I use setShortcut(), my KeyAction classes is ignored.

Is there any solution for only printing shortcut text?

Or Do I have to set text like "새 파일(&N)\t\tCtrl + N" ?

I wanna print text aligned.

Thanks for your help.

pasbi
  • 2,037
  • 1
  • 20
  • 32
chan
  • 75
  • 9
  • I spent quite a lot of time on this issue and eventually found that it's not possible directly. The remedy was to use [`QWidgetAction`](https://doc.qt.io/qt-5/qwidgetaction.html). You can format them as you like. However, it's not so easy and [brings new problems](https://stackoverflow.com/q/55086498/). Good luck, I'm looking forward to useful answers. – pasbi Apr 18 '20 at 07:53
  • You may want to use `setShortcut()` combined with overriding the associated shortcut event. See `QEvent::ShortcutOverride`. – Tfry Apr 18 '20 at 14:43
  • @Tfry If I understood your word, I could try to ignore event that deliver QShortcutEvent. Am i right? thx. – chan Apr 20 '20 at 02:06
  • @chan: Exactly. It's been a while since I've done this myself, but IIRC, the basic procedure is: 1. Install an event filter, 2. Accept the shortcut override event 3. Now Qt will deliver a regular key event. – Tfry Apr 20 '20 at 05:38

1 Answers1

0

Answer based on the comments of TFry.

Set the shortcut as usual (using QAction::setShortcut). To prevent triggering the action and to stop the event-propagation when the shortcut is pressed, you must accept all ShortcutOverride-events in the top-level window (i.e., MainWindow, usually):

MainWindow::MainWindow(...)
{
   installEventFilter(this);
}

bool MainWindow::eventFilter(QObject* o, QEvent* e)
{
  if (o == this && e->type() == QEvent::ShortcutOverride) {
    e->accept();
  }
  return QMainWindow::eventFilter(o, e);
}
pasbi
  • 2,037
  • 1
  • 20
  • 32
  • Thx. I'll try this solution not long after. Because I solved my problem in other way. I edited this question post with my solution for you but it seems not saved :( The solution was just using this code : `this->newAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));` – chan Apr 21 '20 at 09:11
  • I've removed that part from your question (you can see it in the edit history) because (a) it did not answer your question (although it might have worked for your special case) and (b) answers should go into the answer section, not into the question. I think TFry's solution is the right answer to your question in general (i.e., it helped me a lot and it may help many other people, too). – pasbi Apr 21 '20 at 09:20
  • I got it. I'm gonna try this solution. thanks for your help:) – chan Apr 22 '20 at 03:11