代码很简单,大家帮忙看下啦,多谢谢啊
我创建了一个文本编辑框,然后自定义了一个pop弹出菜单,想在这个菜单上创建快捷键,不成功。。。
import math, random, sys
import re
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore
class Test2(QWidget):
def __init__(self, parent = None):
super(Test2, self).__init__(parent)
self.edit = QTextEdit()
self.m = None
self.edit.setContextMenuPolicy(Qt.CustomContextMenu)
self.bt = QPushButton()
vbox = QVBoxLayout()
vbox.addWidget(self.edit)
vbox.addWidget(self.bt)
self.setLayout(vbox)
self.setVisible(True)
self.doc = self.edit.document()
self.txtcursor = QTextCursor(self.doc)
self.connect(self.edit, SIGNAL("selectionChanged()"), self.out)
self.connect(self.bt, SIGNAL("clicked()"), self.clear)
self.connect(self.edit, SIGNAL("customContextMenuRequested(QPoint)"), self.addMenu)
#self.txtcursor.select(QTextCursor.WordUnderCursor)
self.format = QTextCharFormat()
self.format.setForeground(Qt.blue)
self.m = QMenu()
a = self.m.addAction("print", self, SLOT(self.pr()), QKeySequence("CTRL+E"))
a.setShortcut(QKeySequence("Ctrl+P"))
self.connect(a, SIGNAL("triggered()"), self.pr)
def addMenu(self):
self.m.exec(QCursor.pos())
def pr(self):
print("print ok")
def out(self):
print("anchor = ", self.txtcursor.anchor())
print("selectstart= ")
print(self.txtcursor.selectionStart())
print(self.txtcursor.selectionEnd())
print(self.doc.characterCount())
str = self.edit.textCursor().selectedText()
if (len(str)):
print(str)
def clear(self):
list = (1, 2)
self.txtcursor.clearSelection()
if __name__ == "__main__":
app = QApplication(sys.argv)
test = Test2()
sys.exit(app.exec_())