PDA

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



blazedaces
30th August 2008, 00:43
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 (http://doc.trolltech.com/4.4/mainwindows-application.html) 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):


self.cutAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editcut.png'), 'Cu&t', self)
self.cutAct.setShortcut('Ctrl+X')
self.cutAct.setStatusTip('Cut the current selection\'s contents to the\nclipboard')
self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.textEdit, QtCore.SLOT('cut()'))

self.copyAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editcopy.png'), '&Copy', self)
self.copyAct.setShortcut('Ctrl+C')
self.copyAct.setStatusTip('Copy the current selection\'s contents to the\nclipboard')
self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.textEdit, QtCore.SLOT('copy()'))

self.pasteAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editpaste.png'), '&Paste', self)
self.pasteAct.setShortcut('Ctrl+V')
self.pasteAct.setStatusTip('Paste the clipboard\'s contents into the current\nselection')
self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.textEdit, QtCore.SLOT('paste()'))

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):


self.cutAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editcut.png'), 'Cu&t', self)
self.cutAct.setShortcut('Ctrl+X')
self.cutAct.setStatusTip('Cut the current selection\'s contents to the\nclipboard')
self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.cut)

self.copyAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editcopy.png'), '&Copy', self)
self.copyAct.setShortcut('Ctrl+C')
self.copyAct.setStatusTip('Copy the current selection\'s contents to the\nclipboard')
self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.copy)

self.pasteAct = QtGui.QAction(QtGui.QIcon('/usr/share/icons/crystalsvg/22x22/actions/editpaste.png'), '&Paste', self)
self.pasteAct.setShortcut('Ctrl+V')
self.pasteAct.setStatusTip('Paste the clipboard\'s contents into the current\nselection')
self.connect(self.cutAct, QtCore.SIGNAL('triggered()'), self.paste)

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


def cut(self):
self.textEdit.cut()

def copy(self):
self.textEdit.copy()

def paste(self):
self.textEdit.paste()

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

gillian
27th November 2014, 10:11
Hello,

your self must be your widget and you may need two more lines. Example with a copy from tableview:


self.ui.tableView.copyAction = QtGui.QAction('&Copy', self.ui.tableView)
self.ui.tableView.copyAction.setShortcutContext(Qt Core.Qt.WidgetShortcut)
self.ui.tableView.copyAction.setShortcut(QtCore.Qt .CTRL + QtCore.Qt.Key_C)
self.ui.tableView.copyAction.setStatusTip('Copy to the Clipboard')
self.ui.tableView.addAction(self.ui.tableView.copy Action)
self.ui.tableView.copyAction.triggered.connect(sel f.copy)