1

How I can right justify the QKeySequence in PyQt5?

copy_absolute_path_action = (
    create_action(self, _("Copy Absolute Path"), QKeySequence(
        get_shortcut('explorer', 'copy absolute path')),
                  triggered=self.copy_absolute_path))
copy_relative_path_action = (
    create_action(self, _("Copy Relative Path"), QKeySequence(
        get_shortcut('explorer', 'copy relative path')),
                  triggered=self.copy_relative_path))
copy_file_clipboard_action = (
    create_action(self, _("Copy File to Clipboard"),
                  QKeySequence(get_shortcut('explorer', 'copy file')),
                  icon=ima.icon('editcopy'),
                  triggered=self.copy_file_clipboard))
save_file_clipboard_action = (
    create_action(self, _("Paste File from Clipboard"),
                  QKeySequence(get_shortcut('explorer', 'paste file')),
                  icon=ima.icon('editpaste'),
                  triggered=self.save_file_clipboard))

enter image description here

I want the key shortcuts to be right justified and the rest unchanged.

Thanks in advance

Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
  • 1
    Please do not use the pyqt4 tag since there is no complete compatibility between PyQt4 and PyQt5 (I suspect that there is no solution for PyQt4 but maybe for PyQt5) – eyllanesc Jan 09 '19 at 20:59

1 Answers1

2

In this case the solution is to implement a QProxyStyle:

from PyQt5 import QtCore, QtGui, QtWidgets

class MenuProxyStyle(QtWidgets.QProxyStyle):
    def drawControl(self, element, option, painter, widget=None):
        shortcut = ""
        if element == QtWidgets.QStyle.CE_MenuItem:
            vals = option.text.split("\t")
            if len(vals) == 2:
                text, shortcut = vals
                option.text = text
        super(MenuProxyStyle, self).drawControl(element, option, painter, widget)
        if shortcut:
            margin = 10 # QStyleHelper::dpiScaled(5)
            self.proxy().drawItemText(painter, option.rect.adjusted(margin, 0, -margin, 0), 
                QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter,
                option.palette, option.state & QtWidgets.QStyle.State_Enabled, 
                shortcut, QtGui.QPalette.Text)

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        menu = QtWidgets.QMenu("File", self)
        self._proxy = MenuProxyStyle(menu.style())
        menu.setStyle(self._proxy)
        self.menuBar().addMenu(menu)

        # create icons
        data = [("Copy Absolute Path", "Ctrl+Alt+C"),
                 ("Copy Relative Path", "Ctrl+Shift+C"),
                 ("Copy File to Clipboard", "Ctrl+C")]

        for text, shortcut in data:
            action = QtWidgets.QAction(self, 
                text=text, 
                shortcut=QtGui.QKeySequence(shortcut))
            menu.addAction(action)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Amazing, Genius, Thank you very much. Really appreciate your help. – Khalil Al Hooti Jan 09 '19 at 21:50
  • Can this be applied to a context menus?? I could not make it work on context menus!! – Khalil Al Hooti Jan 10 '19 at 22:45
  • 1
    @KhalilAlHooti I see that in the case of contexts menus shorcuts are not shown – eyllanesc Jan 10 '19 at 22:52
  • Thanks again, your suggestion works perfect. Do you know How I can preserve the original appearance. The suggested method changes the color of the menus – Khalil Al Hooti Jan 25 '19 at 18:26
  • 1
    @KhalilAlHooti try changing `self._proxy = MenuProxyStyle(menu.style())` to `self._proxy = MenuProxyStyle(QtWidgets.QApplication.style())`, if it does not work then you must provide a **[mcve]** to analyze where the problem is, in my case I do not observe the problem – eyllanesc Jan 25 '19 at 18:57
  • Actually it is working perfect as you suggested with your original answer. It only does not work with dark theme. https://stackoverflow.com/a/48256803/6814154. Thanks again for your support. – Khalil Al Hooti Jan 26 '19 at 19:05