Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Cut, Copy, and Paste buttons in PyQt4 [ANSWERED]
Forum Updated to NodeBB v4.3 + New Features

Cut, Copy, and Paste buttons in PyQt4 [ANSWERED]

Scheduled Pinned Locked Moved Language Bindings
6 Posts 2 Posters 7.7k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    Nubby
    wrote on last edited by
    #1

    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, QtCore

    class 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!

    1 Reply Last reply
    0
    • jazzycamelJ Offline
      jazzycamelJ Offline
      jazzycamel
      wrote on last edited by
      #2

      [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.

      For the avoidance of doubt:

      1. All my code samples (C++ or Python) are tested before posting
      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nubby
        wrote on last edited by
        #3

        [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())@

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

          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)

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0
          • N Offline
            N Offline
            Nubby
            wrote on last edited by
            #5

            [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!

            1 Reply Last reply
            0
            • jazzycamelJ Offline
              jazzycamelJ Offline
              jazzycamel
              wrote on last edited by
              #6

              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, QtCore

              class 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_())
              @

              For the avoidance of doubt:

              1. All my code samples (C++ or Python) are tested before posting
              2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
              1 Reply Last reply
              0

              • Login

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