Results 1 to 2 of 2

Thread: PyQt4: QTextEditor's Cut, Copy, and Paste Slots don't seem to function properly...

  1. #1
    Join Date
    Aug 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default PyQt4: QTextEditor's Cut, Copy, and Paste Slots don't seem to function properly...

    Hello. I am under the impression (based on a previous thread) that I can come here for help on PyQt as well as Qt. If I posted in the wrong location in the forum for this inquiry I apologize and ask that you direct me to the correct location.

    That being said, often enough to try and understand how to implement certain widgets I often seek one of the many examples provided on the Qt website. In this case, I am converting the Application Example from its native C++ Qt code to PyQt code. Usually I have no issues when doing this, but now I have come to a point where I can't figure out if there's anything more I can actually do.

    Just to make sure you're aware, the GUI itself comes up. I can type, save and open files, start a new one and even cut, copy, and paste through the keyboard, but not through the toolbar or menu bar.

    Simply put, the cut, copy, and paste slots in QTextEditor seem to not be responding. I have tried doing some minor debugging, discovering that the shortcuts for Ctr+C, Ctr+X, Ctr+V work regardless of whether they are included as shortcuts to the QActions. Another words, they are embedded in the computer or QTextEditor to work automatically.

    Another interesting thing that might give a clue: the paste and copy buttons literally do nothing. BUT, the cut button for some reason does copy that selection to the clipboard (I know because if I press Ctrl+V afterwards it pastes that selection), but it does not remove the text selected. It doesn't act exactly like copy does though because right after pressing the cut button the selected text is no longer selected, as in the cursor simply moves to the last character of the selection.

    I could post my code for the entire class and if you request it I can provide it, but I feel it might be a waste to do so, but here is my code for implimenting those QActions (I know the code to make them show up in the toolbars works because their icons appear and so do their tooltips):

    Qt Code:
    1. self.cutAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editcut.png'), 'Cu&t', self)
    2. self.cutAct.setShortcut('Ctrl+X')
    3. self.cutAct.setStatusTip('Cut the current selection\'s contents to the\nclipboard')
    4. self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.textEdit, QtCore.SLOT('cut()'))
    5.  
    6. self.copyAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editcopy.png'), '&Copy', self)
    7. self.copyAct.setShortcut('Ctrl+C')
    8. self.copyAct.setStatusTip('Copy the current selection\'s contents to the\nclipboard')
    9. self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.textEdit, QtCore.SLOT('copy()'))
    10.  
    11. self.pasteAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editpaste.png'), '&Paste', self)
    12. self.pasteAct.setShortcut('Ctrl+V')
    13. self.pasteAct.setStatusTip('Paste the clipboard\'s contents into the current\nselection')
    14. self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.textEdit, QtCore.SLOT('paste()'))
    To copy to clipboard, switch view to plain text mode 

    Now, I have tried to, instead of using QTextEdit's slot, to instead use the method cut(self), copy(self), etc. by changing my code to the following (it produces the same result):

    Qt Code:
    1. self.cutAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editcut.png'), 'Cu&t', self)
    2. self.cutAct.setShortcut('Ctrl+X')
    3. self.cutAct.setStatusTip('Cut the current selection\'s contents to the\nclipboard')
    4. self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.cut)
    5.  
    6. self.copyAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editcopy.png'), '&Copy', self)
    7. self.copyAct.setShortcut('Ctrl+C')
    8. self.copyAct.setStatusTip('Copy the current selection\'s contents to the\nclipboard')
    9. self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.copy)
    10.  
    11. self.pasteAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editpaste.png'), '&Paste', self)
    12. self.pasteAct.setShortcut('Ctrl+V')
    13. self.pasteAct.setStatusTip('Paste the clipboard\'s contents into the current\nselection')
    14. self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.paste)
    To copy to clipboard, switch view to plain text mode 

    and then lower below having three simple methods that look like the following:

    Qt Code:
    1. def cut(self):
    2. self.textEdit.cut()
    3.  
    4. def copy(self):
    5. self.textEdit.copy()
    6.  
    7. def paste(self):
    8. self.textEdit.paste()
    To copy to clipboard, switch view to plain text mode 

    Like I said though, this doesn't change anything (and it doesn't make sense that it should, but like I said, I feel like I've tried a lot and am stuck).

    Any and all help is greatly appreciated. If there's some other source of information you could point out that would help me understand the problem, that might also help. For example, I have tried, but I can not find the source codes for these modules anywhere. In the location where one thinks they exist on linux (/usr/lib/site-packages/PyQt4/) there are only .so files (shared files I believe) and I don't know if there's a way to open them.

    Perhaps if I could view the source code I could understand what the issue is here...

    I've also completely uninstalled and reinstalled PyQt4 just today in case the most recent update changed anything about this particular widget (was doubtful, but it couldn't hurt).

    Again, all help is appreciated. Thank you.

    -blazed

  2. #2
    Join Date
    Nov 2014
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: PyQt4: QTextEditor's Cut, Copy, and Paste Slots don't seem to function properly..

    Hello,

    your self must be your widget and you may need two more lines. Example with a copy from tableview:
    Qt Code:
    1. self.ui.tableView.copyAction = QtGui.QAction('&Copy', self.ui.tableView)
    2. self.ui.tableView.copyAction.setShortcutContext(QtCore.Qt.WidgetShortcut)
    3. self.ui.tableView.copyAction.setShortcut(QtCore.Qt.CTRL + QtCore.Qt.Key_C)
    4. self.ui.tableView.copyAction.setStatusTip('Copy to the Clipboard')
    5. self.ui.tableView.addAction(self.ui.tableView.copyAction)
    6. self.ui.tableView.copyAction.triggered.connect(self.copy)
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.