1

I'm in the process of porting a program from Qt4 to Qt5. I got it to the point where it compiles with both versions. Almost everything seems to work in the new version, excepted that I have lost the Ctrl+U keyboard shortcut in the Qt5 version in two different contexts:

  • in a dialog box, using a QShortcut assigned to the Ctrl+U key sequence
  • in the main window, when listening for evens in an event filter.

This seems specific to Ctrl+U. I know that in the dialog box, other shortcuts like Ctrl+F or Ctrl+W work as they used to, and that in the main window, Ctrl+P works as before too.

Is there anything special about Ctrl+U (and possibly others) in Qt5 that prevent me from seeing the event ?

Edit

It seems it is linked to the presence of a line edit. MWE:

#include "mw.hh"
#include <QApplication>
#include <QShortcut>
#include <QStatusBar>
#include <QKeySequence>
#include <QTextStream>
#include <QLineEdit>


static void registerShortCut(const QKeySequence & seq, QWidget * receiver, 
                             const char * fn,
                             Qt::ShortcutContext context = Qt::WindowShortcut)
{
  QShortcut * sc = new QShortcut(seq, receiver);
  sc->setContext(context);
  receiver->connect(sc, SIGNAL(activated()), fn);
}

MW::MW()
{
  statusBar()->showMessage("stuff");
  registerShortCut(QKeySequence(tr("Ctrl+T")),
                   this, SLOT(test()));
  registerShortCut(QKeySequence(tr("Ctrl+U")),
                   this, SLOT(test2()));

  QLineEdit * le = new QLineEdit("stuff");
  setCentralWidget(le);
}

void MW::test()
{
  statusBar()->showMessage("Test !");
}

void MW::test2()
{
  statusBar()->showMessage("Test 2 !");
}

int main(int argc, char ** argv)
{
  QApplication main(argc, argv);
  MW mw;
  mw.show();
  main.exec();
}

With mw.hh:

#include <QMainWindow>

class MW : public QMainWindow {
  Q_OBJECT;
public:
  MW();

public slots:
  void test();
  void test2();
};

Hitting Ctrl+T shows Test in the status bar, but Ctrl+U does not. Commenting out the setCentralWidget(le) line restores the expected behaviour.

Vincent Fourmond
  • 3,038
  • 1
  • 22
  • 24
  • 1
    Do you get any log messages that says the shortcut is defined multiple times? If that happens, the shortcut is disabled altogether; neither place where it's assigned will work. – goug Feb 09 '17 at 00:12
  • @goug No, I don't have any of those. Moreover, in one of the two cases, I'm not using a shortcut, just intercepting events. – Vincent Fourmond Feb 09 '17 at 00:17
  • It sounds like something higher up is intercepting Ctrl-U before it gets to your application or your dialog. Is Ctrl-U a shortcut on the dialog's system menu, or something along those lines? (You haven't said what platform you're on.) Do you run both the Qt4 version and the Qt5 version on the same box, but get these different results? – goug Feb 09 '17 at 00:41
  • @goug Platform is Linux; both versions are run in the exact same environment. – Vincent Fourmond Feb 09 '17 at 07:01

1 Answers1

0

I couldn't find the source of the problem, especially since Ctrl+U is not referenced as keyboard shortcut in QLineEdit. However, I found a nice workaround, inspired by this answer. I added the following code to my own subclass of QApplication:

  bool notify(QObject * receiver, QEvent * event) 
  {
    // We override keyboard shortcuts within QLineEdit
    if(event->type() == QEvent::ShortcutOverride &&
       dynamic_cast<QLineEdit*>(receiver)) {
      event->ignore();
      return false;
    }
    return QApplication::notify(receiver, event);
  }
Community
  • 1
  • 1
Vincent Fourmond
  • 3,038
  • 1
  • 22
  • 24