特定のフォルダーを監視し、新しいファイルがフォルダーに追加されているかどうかを確認する必要があります。その後、ユーザーにファイルを印刷するかどうかを尋ねます。問題は QFileSystemWatcher にあります。追加されたファイルの名前を取得する方法がわかりません。このコードを試してみましたが (後述)、file_changed 関数はフォルダーの変更に反応しません。directory_changed 関数が機能します。
追加されたファイルの名前を取得する方法と、シグナル/ブール値を取得する方法を教えてください。ファイルが追加されたときに、メッセージ ボックスとシステム トレイ メッセージをトリガーできます (コードを参照)。どうもありがとうございました。お粗末なコードで申し訳ありません。私は Qt を初めて使用します。そして、私に C++ の例を教えないでください。私はそれらを理解していません。
システムをインポート
PyQt4.QtGui インポートから *
PyQt4インポートQtCoreから
# create a system tray message
class SystemTrayIcon(QSystemTrayIcon):
def __init__(self, parent=None):
QSystemTrayIcon.__init__(self, parent)
#self.setIcon(QIcon.fromTheme("document-save"))
self.setIcon(QIcon("C:\Icon2.ico"))
def welcome(self):
self.showMessage("New PayFile found!", "We found a new PayFile.")
def show(self):
QSystemTrayIcon.show(self)
QtCore.QTimer.singleShot(100, self.welcome)
# QFileSystemWatcher with signals
@QtCore.pyqtSlot(str)
def directory_changed(path):
print('Directory Changed: ', path)
@QtCore.pyqtSlot(str)
def file_changed(path):
print('File Changed: ', path)
'''
# QFileSystemWatcher without signals
def directory_changed(path):
print('Directory Changed: %s' % path)
def file_changed(path):
print('File Changed: %s' % path)
'''
if __name__ == '__main__':
a = QApplication(sys.argv)
# add a folder path here
fs_watcher = QtCore.QFileSystemWatcher(['C:\\Folder'])
# without signals
fs_watcher.directoryChanged.connect(directory_changed)
# this doesn't work, I don't get the name of the added file
fs_watcher.fileChanged.connect(file_changed)
# with signals
#fs_watcher.connect(fs_watcher, QtCore.SIGNAL('directoryChanged(QString)'), directory_changed)
# this doesn't work, I don't get the name of the added file
#fs_watcher.connect(fs_watcher, QtCore.SIGNAL('fileChanged(QString)'), file_changed)
# how can I find out whether a new file has been added ???
if fs_watcher.directoryChanged.connect(directory_changed):
tray = SystemTrayIcon()
tray.show()
print_msg = "Would you like to do something with the file?"
reply = QMessageBox.question(None, 'Print The File', print_msg, QMessageBox.Yes, QMessageBox.No)
if reply == QMessageBox.Yes:
print "I said yes"
# do stuff
# print the file
if reply == QMessageBox.No:
print "No"
sys.exit(a.exec_())