Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt5 show hide is not working when shortcut is used

PyQt5 show hide is not working when shortcut is used

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 89 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MLua
    wrote on 21 Feb 2025, 19:41 last edited by
    #1

    I am writing a QT application that can call up by short cut when I need, and hide when I no longer need it.
    I have come with a sample code, it totally work for what I want. But the strange thing is when I change the input to QLineEdit (see line 28), show is not working.

    So when I run the sample code with QLineEdit (self.input = QLineEdit), pressing "ctrl + o" can't properly re-show the windows, the windows popup but content is missing. Instead I need to click show from system tray.

    In short, when QCheckBox is used, both shortcut and system tray click show can properly re-show. But when QLineEdit is used, shortcut re-show is not working.

    Any idea of why? I am using Windows 11.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget, QLineEdit, QSystemTrayIcon, \
        QSpacerItem, QSizePolicy, QMenu, QAction, QStyle, QCheckBox
    from PyQt5.QtCore import QSize
    import keyboard
    
    class MainWindow(QMainWindow):
        input = None
        tray_icon = None
    
        def __init__(self):
            QMainWindow.__init__(self)
    
            self.setMinimumSize(QSize(480, 80))
            self.setWindowTitle("System Tray Application")
            central_widget = QWidget(self)
            self.setCentralWidget(central_widget)
    
            grid_layout = QGridLayout(self)
            central_widget.setLayout(grid_layout)
            grid_layout.addWidget(
                QLabel("Application, which can minimize to Tray", self), 0, 0)
    
            # ! QLineEdit is not working for show, but QCheckBox work !
            self.input = QLineEdit('Enter text here')
            # self.input = QCheckBox('Enter text here')
            grid_layout.addWidget(self.input, 1, 0)
            grid_layout.addItem(QSpacerItem(
                0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding), 2, 0)
    
            self.tray_icon = QSystemTrayIcon(self)
            self.tray_icon.setIcon(
                self.style().standardIcon(QStyle.SP_ComputerIcon))
    
            show_action = QAction("Show", self)
            quit_action = QAction("Exit", self)
            hide_action = QAction("Hide", self)
            show_action.triggered.connect(self.show)
            hide_action.triggered.connect(self.hide)
            quit_action.triggered.connect(QApplication.instance().quit)
            tray_menu = QMenu()
            tray_menu.addAction(show_action)
            tray_menu.addAction(hide_action)
            tray_menu.addAction(quit_action)
            self.tray_icon.setContextMenu(tray_menu)
            self.tray_icon.show()
    
            keyboard.add_hotkey('ctrl+o', self.show_hotkey)
    
        def closeEvent(self, event):
            event.ignore()
            self.hide()
            self.input.hide()
    
        def show_hotkey(self):
            self.show
            self.activateWindow()
            self.raise_()
            self.input.setFocus()
    
        # def show_hotkey(self):
        #     self.input.setParent(None)  # Detach the widget from its parent
        #     self.show()
        #     self.input.setParent(self.centralWidget())  # Re-attach the widget to its parent
        #     self.input.show()  # Show the widget explicitly
        #     self.activateWindow()
        #     self.raise_()
        #     self.input.setFocus()  # Set focus to the QLineEdit
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    
    1 Reply Last reply
    0

    1/1

    21 Feb 2025, 19:41

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved