Cut, Copy, and Paste buttons in PyQt4 [ANSWERED]
-
Answered by: JazzyCamel
Hi, I am working on a text editor to release with Cut, Paste, Copy, Exit buttons 'n' all. I have made the buttons in my toolbar and Edit menu, with this code for the them:
@ exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit Application')
exitAction.triggered.connect(QtGui.qApp.quit)copyAction = QtGui.QAction(QtGui.QIcon('copy.png'), '&Copy', self) copyAction.setShortcut('Ctrl+C') copyAction.setStatusTip('Copy to the Clipboard') copyAction.triggered.connect(QtGui.QTextEdit.copy(self)) pasteAction = QtGui.QAction(QtGui.QIcon('paste.png'), '&Paste', self) pasteAction.setShortcut('Ctrl+V') pasteAction.setStatusTip('Paste from the Clipboard') pasteAction.triggered.connect(QtGui.QTextEdit.paste(self))@
Unfortunately This does not work, when I have searched the internet for hours and had hours of trial and error, still nothing.
My code is as following:
@import sys
import os
from PyQt4 import QtGui, QtCoreclass Window(QtGui.QMainWindow):
def __init__(self): super(Window, self).__init__() self.initUI() def center(self): qr = self.frameGeometry() cp = QtGui.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def initUI(self): exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit Application') exitAction.triggered.connect(QtGui.qApp.quit) copyAction = QtGui.QAction(QtGui.QIcon('copy.png'), '&Copy', self) copyAction.setShortcut('Ctrl+C') copyAction.setStatusTip('Copy to the Clipboard') copyAction.triggered.connect(QtGui.QTextEdit.copy(self)) pasteAction = QtGui.QAction(QtGui.QIcon('paste.png'), '&Paste', self) pasteAction.setShortcut('Ctrl+V') pasteAction.setStatusTip('Paste from the Clipboard') pasteAction.triggered.connect(QtGui.QTextEdit.paste(self)) cutAction = QtGui.QAction(QtGui.QIcon('cut.png'), '&Cut', self) cutAction.setShortcut('Ctrl+X') cutAction.setStatusTip('Copy text to the clipboard and delet from editor') cutAction.triggered.connect(QtGui.QTextEdit.cut(self)) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAction) editMenu = menubar.addMenu('&Edit') editMenu.addAction(copyAction) editMenu.addAction(pasteAction) editMenu.addAction(cutAction) toolbar = self.addToolBar('Exit') toolbar.addAction(exitAction) toolbar.addAction(copyAction) toolbar.addAction(pasteAction) toolbar.addAction(cutAction) self.setGeometry(10, 10, 500, 500) self.setWindowTitle('Text Editor') self.setWindowIcon(QtGui.QIcon('favicon.png')) self.show() self.center() edit = QtGui.QTextEdit('', self) edit.setStatusTip('Input Text Here') edit.resize(480, 400) edit.move(10, 60) edit.setToolTip('Input Text Here') edit.show() def closeEvent(self, event): reply = QtGui.QMessageBox.question(self, 'Message', "Are you sure you would like to quit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: event.accept() else: event.ignore()
def main():
app = QtGui.QApplication(sys.argv) ex = Window() sys.exit(app.exec_())
if name == 'main':
main()
@If you could get back to me as soon as possible it would be great! I am really looking forwards to seeing the finished product!
-
[quote author="Nubby" date="1364683740"]Unfortunately This does not work...[/quote]
What, in particular, doesn't work? An explanation of the exact problem will help us to help you.
-
[quote author="jazzycamel" date="1365754676"][quote author="Nubby" date="1364683740"]Unfortunately This does not work...[/quote]
What, in particular, doesn't work? An explanation of the exact problem will help us to help you.
[/quote]
I thought it was clear, but what didn't work was cut() copy() and paste() when I put them straight into @copyAction.triggered.connect(QtGui.QTextEdit.copy())@
-
Your problem is with the way you are connecting signals to slots. Firstly, you need to connect to an instance of textedit and then you need to pass the connect method a reference to the slot not the result of it, therefore your example should be:
@
copyAction.triggered.connect(myTextEdit.copy)
@i.e. no parenthesis after the copy method. You don't need to pass self to a slot either. If you want an argument passed to a slot when its called then you'll need to use lambda as follows:
@
myObject.mySignal.connect(lambda: mySlot(myArgument))
@Hope this helps ;o)
-
[quote author="jazzycamel" date="1365756334"]Your problem is with the way you are connecting signals to slots. Firstly, you need to connect to an instance of textedit and then you need to pass the connect method a reference to the slot not the result of it, therefore your example should be:
@
copyAction.triggered.connect(myTextEdit.copy)
@i.e. no parenthesis after the copy method. You don't need to pass self to a slot either. If you want an argument passed to a slot when its called then you'll need to use lambda as follows:
@
myObject.mySignal.connect(lambda: mySlot(myArgument))
@Hope this helps ;o)[/quote]
What exactly would I do? I don't quite understand what you mean, can you show me EXACTLY what do to for copyAction please. I tried doing:
@ copyAction.triggered.connect(lambda: edit(copy()))@
but that didn't work.
Wait, I got it!!! I did:
@copyAction.triggered.connect(lambda: edit.copy())@
AND IT WORKED!!! Three cheers to success!
-
You kinda got it: you only need to lambda the slot if you want to pass an argument, otherwise you just pass a reference to the slot. The following is your example corrected as necessary:
@
from PyQt4 import QtGui, QtCoreclass Window(QtGui.QMainWindow):
def init(self):
super(Window, self).init()self.initUI() def center(self): qr = self.frameGeometry() cp = QtGui.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) def initUI(self): edit = QtGui.QTextEdit(self) edit.setStatusTip('Input Text Here') edit.setToolTip('Input Text Here') self.setCentralWidget(edit) exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit Application') exitAction.triggered.connect(QtGui.qApp.quit) copyAction = QtGui.QAction(QtGui.QIcon('copy.png'), '&Copy', self) copyAction.setShortcut('Ctrl+C') copyAction.setStatusTip('Copy to the Clipboard') copyAction.triggered.connect(edit.copy) pasteAction = QtGui.QAction(QtGui.QIcon('paste.png'), '&Paste', self) pasteAction.setShortcut('Ctrl+V') pasteAction.setStatusTip('Paste from the Clipboard') pasteAction.triggered.connect(edit.paste) cutAction = QtGui.QAction(QtGui.QIcon('cut.png'), '&Cut', self) cutAction.setShortcut('Ctrl+X') cutAction.setStatusTip('Copy text to the clipboard and delet from editor') cutAction.triggered.connect(edit.cut) self.statusBar() menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(exitAction) editMenu = menubar.addMenu('&Edit') editMenu.addAction(copyAction) editMenu.addAction(pasteAction) editMenu.addAction(cutAction) toolbar = self.addToolBar('Exit') toolbar.addAction(exitAction) toolbar.addAction(copyAction) toolbar.addAction(pasteAction) toolbar.addAction(cutAction) self.setGeometry(10, 10, 500, 500) self.setWindowTitle('Text Editor') self.setWindowIcon(QtGui.QIcon('favicon.png')) self.center() def closeEvent(self, event): reply = QtGui.QMessageBox.question( self, 'Message', "Are you sure you would like to quit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No ) if reply == QtGui.QMessageBox.Yes: event.accept() else: event.ignore()
if name == 'main':
import sys
app = QtGui.QApplication(sys.argv)
ex = Window()
ex.show()
ex.raise_()
sys.exit(app.exec_())
@