QApplication.clipboard().dataChanged not work in background - python

I am trying to write a dictionary program that can auto search the word when I copy some words into the clipboard.
I need the program to detect the clipboard change and search in the background.(menubar app)
but my program only updates when I click it.
(for example no use in Mac OS fullscreen mode)
is there any way to lettuce program work even when it is in the background?
mac os catalina , PYQT5
I use QApplication.clipboard().dataChanged.connect() to detect the clipboard change
class MyMainWindow(QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
QApplication.clipboard().dataChanged.connect(self.clipboardChange)
def clipboardChange(self):
print(QApplication.clipboard().text())
self.raise_()
in main file:
if __name__ == "__main__":
app = QApplication(sys.argv)
#app.setQuitOnLastWindowClosed(False)
icon = QIcon("dic.png")
tray = QSystemTrayIcon()
tray.setIcon(icon)
tray.setVisible(True)
#menu = QMenu()
action = QAction("A menu item")
#menu.addAction(action)
#tray.setContextMenu(menu)
myWin = MyMainWindow()
myWin.show()
sys.exit(app.exec_())
the program only update when I click it

Related

Notification when QDockWidget's tab is clicked?

I need to execute a block of code when the user clicks on the tab of a tabbified QDockWidget. So far I've been doing this via a hack using the "visibilityChanged" event but this is now causing issues (for example, if I have several tabbified dock widgets and I drag one out so that it is floating, the tabbified one underneath will fire its "visibilityChanged" event which I will mistakenly interpret as the user clicking the tab). How can I receive proper notification when a user clicks on a QDockWidgets' tab? I've experimented with the "focusInEvent" of QDockWidget but it doesn't seem to fire when the tab is clicked.
When you use tabifyDockWidget() method QMainWindow creates a QTabBar, this is not directly accessible but using findChild() you can get it, and then use the tabBarClicked signal
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
first_dock = None
for i in range(10):
dock = QtGui.QDockWidget("title {}".format(i), self)
dock.setWidget(QtGui.QTextEdit()) # testing
self.addDockWidget(QtCore.Qt.TopDockWidgetArea, dock)
if first_dock:
self.tabifyDockWidget(first_dock, dock)
else:
first_dock = dock
dock.raise_()
tabbar = self.findChild(QtGui.QTabBar, "")
tabbar.tabBarClicked.connect(self.onTabBarClicked)
def onTabBarClicked(self, index):
tabbar = self.sender()
text = tabbar.tabText(index)
print("index={}, text={}".format(index, text))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Open window application in current window

I want to open a game application replacing the menu window.
So I have a game application which I can start with:
subprocess.call(["mygameprogram", "argument one"])
This opens up another window, but I want to replace the qt window with the game window without closing the menu window.
This is my MainWindow class:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initMenu()
self.show()
def initMenu(self):
# load ui file
uic.loadUi("somefile.ui", self)
# add pushbutton
btn = QPushButton()
# add click listener
btn.clicked.connect(self.startLevel)
# add button to layout
self.layout_buttons.addWidget(btn)
def startLevel(self):
# open game, put the right code in here :D
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
Is there any solution for that? And if not, what should I do instead?
Thanks in advance

Python PyQt4 how to open image using QFileDialog

I have to write a program with an option to open an image from a file. I have to use QFileDialog and display image in QLabel, using QPixmap. I am able to use them individually but I didn't manage to combine them.
I think I need to take my image name from dlg.selectedFiles but I don't know how to choose the moment when there is useful data in it. Do I need to make a loop in my main program, and constantly check if there is image to open? Can I send a signal to my label using openAction.triggered.connect(...)?
from PyQt4 import QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
dlg = QtGui.QFileDialog(self)
openAction = QtGui.QAction('Open', self)
openAction.triggered.connect(dlg.open)
fileMenu.addAction(openAction)
#label = QtGui.QLabel(self)
#pixmap = QtGui.QPixmap('')
#label.setPixmap(pixmap)
def main():
app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()
if __name__ == '__main__':
sys.exit(main())
You need to make your own slot and connect it to the openAction signal.
In your __init__ function do:
openAction.triggered.connect(self.openSlot)
In your class MainWindow define the following function:
def openSlot(self):
# This function is called when the user clicks File->Open.
filename = QtGui.QFileDialog.getOpenFileName()
print(filename)
# Do your pixmap stuff here.

How to reinitialize console when clicking the close button?

I am building a simple GUI for selecting files using PYQT, and I am working with the Spyder IDE. When I try to close the GUI with the red "X" button and run the file again, a warning message pops up that says:
No Python shell is currently selected to run **readFile.py**
Please select or open a new Python interpreter and try again
How can I make the red X button reinitialize the interpreter and console without having to manually do it every time? Here is my code:
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
vbox = QtGui.QVBoxLayout()
lbl = QtGui.QLabel('Please Choose Excel File',self)
openButton = QtGui.QPushButton('Choose File')
openButton.clicked.connect(self.showDialog)
vbox.addWidget(lbl)
vbox.addWidget(openButton)
self.setLayout(vbox)
self.setGeometry(300,300,350,300)
self.setWindowTitle('file Dialog')
self.show()
def showDialog(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
print(fname)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

Qt QSystemTrayIcon change menu items

I am using Pyqt however c++ code is fine. I am trying to change a menu item in QSystemTrayIcon using the QT framework in Linux (Ubuntu 11.10). Currently I have tried to reset the QMenu that I initially set:
self.tray = QSystemTrayIcon()
m = QMenu()
m.addAction('First')
m.addAction('Second')
tray.setContextMenu(m)
I place this in my class and make tray a class variable. I was thinking that if I just change the tray to set a new menu it would update:
new_m = QMenu()
new_m.addAction('First')
new_m.addAction('Third')
self.tray.setContextMenu(new_m)
However that doesn't work and the tray menu is still the same as it was initially made. How could I be able to rebuild the menu to change it?
I tested with the following code and it seems to work fine :
from PyQt4.QtGui import *
import sys
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.tray = QSystemTrayIcon(QApplication.style().standardIcon(QStyle.SP_DriveDVDIcon), self)
m = QMenu()
m.addAction('First')
m.addAction('Second')
self.tray.setContextMenu(m)
self.tray.show()
p = QPushButton("test", self)
self.setCentralWidget(p)
p.clicked.connect(self.onClick)
def onClick(self):
new_m = QMenu()
new_m.addAction('First')
new_m.addAction('Third')
self.tray.setContextMenu(new_m)
app = QApplication(sys.argv)
w = MainWindow()
w.show();
sys.exit(app.exec_())
Are you sure there is only one QSystemTrayIcon object ? (In your snippets, there is both self.tray and tray).

Categories