首页 > 代码库 > pyqt 学习基础5 - 笔记软件的编辑器
pyqt 学习基础5 - 笔记软件的编辑器
笔记软件的编辑器
几个疑问
今天在试着为笔记软件写个编辑器,然后参考qt自带的textedit的示例。下面是一个撤销操作的一段代码,我就疑惑了,这跟用QPushButton应该差不多吧。
self.actionUndo = QtGui.QAction(
QtGui.QIcon.fromTheme(‘edit-undo‘, QtGui.QIcon(rsrcPath + ‘/editundo.png‘)), "&Undo", self, shortcut=QtGui.QKeySequence.Undo) tb.addAction(self.actionUndo) menu.addAction(self.actionUndo)
代码比较没有注释
# -*- coding:utf8 -*- import sip sip.setapi(‘QString‘, 2) from PyQt4 import QtCore, QtGui from PyQt4.QtCore import * from PyQt4.QtGui import * import mainQss # NoDragCombo这个类是为了防止拖动Combobox所带来的bug class NoDragCombo(QComboBox): def __init__(self, parent=None): QComboBox.__init__(self, parent) def mouseMoveEvent(self, QMouseEvent): pass def mouseReleaseEvent(self, QMouseEvent): pass class NoDragFontCombo(QFontComboBox): def __init__(self, parent=None): QFontComboBox.__init__(self, parent) def mouseMoveEvent(self, QMouseEvent): pass def mouseReleaseEvent(self, QMouseEvent): pass class CenterWidget(QWidget): def __init__(self, fileName=None, parent=None): QWidget.__init__(self, parent) self.initAllObjects() self.setupFileActions() self.setupEditActions() self.setupTextActions() self.setupTextEdit() self.setupStyleSheet() self.setupMainUi() self.fontChanged(self.textEdit.font()) self.colorChanged(self.textEdit.textColor()) self.alignmentChanged(self.textEdit.alignment()) self.textEdit.document().modificationChanged.connect( self.actionSave.setEnabled) self.textEdit.document().modificationChanged.connect( self.setWindowModified) self.textEdit.document().undoAvailable.connect( self.actionUndo.setEnabled) self.textEdit.document().redoAvailable.connect( self.actionRedo.setEnabled) self.actionSave.setEnabled(self.textEdit.document().isModified()) self.actionUndo.setEnabled(self.textEdit.document().isUndoAvailable()) self.actionRedo.setEnabled(self.textEdit.document().isRedoAvailable()) self.actionUndo.triggered.connect(self.textEdit.undo) self.actionRedo.triggered.connect(self.textEdit.redo) self.actionCut.setEnabled(False) self.actionCopy.setEnabled(False) self.actionCut.triggered.connect(self.textEdit.cut) self.actionCopy.triggered.connect(self.textEdit.copy) self.actionPaste.triggered.connect(self.textEdit.paste) self.textEdit.copyAvailable.connect(self.actionCut.setEnabled) self.textEdit.copyAvailable.connect(self.actionCopy.setEnabled) QtGui.QApplication.clipboard().dataChanged.connect( self.clipboardDataChanged) def initAllObjects(self): self.oneStyle = QStyleFactory.create("plastique") self.textEdit = QTextEdit(self) self.layout = QVBoxLayout(self) self.topLayout = QHBoxLayout() self.topWidget = QWidget(self) self.topWidgetLayout = QHBoxLayout(self.topWidget) self.fileBtn = QPushButton("File",self.topWidget) self.editBtn = QPushButton("Edit",self.topWidget) self.textBtn = QPushButton("Text",self.topWidget) self.comboFont = NoDragFontCombo(self.topWidget) self.comboSize = NoDragCombo(self.topWidget) self.comboStyle = NoDragCombo(self.topWidget) def setupStyleSheet(self): # self.fileBtn.setStyleSheet(mainQss.writeButton) # self.fileBtn.setGeometry(2, 1, 24, 24) # self.editBtn.setStyleSheet(mainQss.writeButton) # self.editBtn.setGeometry(28, 1, 24, 24) # self.textBtn.setStyleSheet(mainQss.writeButton) # self.textBtn.setGeometry(54, 1, 24, 24) # self.comboFont.setGeometry(80, 1, 100, 24) # self.comboSize.setGeometry(182, 1, 100, 24) # self.comboStyle.setGeometry(284, 1, 100, 24) self.fileBtn.setStyle(self.oneStyle) self.editBtn.setStyle(self.oneStyle) self.textBtn.setStyle(self.oneStyle) self.comboStyle.setStyle(self.oneStyle) self.comboSize.setStyle(self.oneStyle) self.comboFont.setStyle(self.oneStyle) self.topWidget.setMinimumHeight(26) # self.topWidget.setMaximumHeight(26) self.topWidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum) self.topLayout.setContentsMargins(0, 0, 0, 0) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(0) def setupTextEdit(self): self.textEdit.setFocus() oneFont = QFont("Times New Romane", 13, QFont.Normal, False) self.textEdit.setFont(oneFont) self.textEdit.currentCharFormatChanged.connect( self.currentCharFormatChanged) self.textEdit.cursorPositionChanged.connect(self.cursorPositionChanged) self.setCurrentFileName() def setupMainUi(self): self.topLayout.addWidget(self.topWidget) self.layout.addLayout(self.topLayout) self.layout.addWidget(self.textEdit) self.setLayout(self.layout) self.topWidgetLayout.addWidget(self.fileBtn) self.topWidgetLayout.addWidget(self.editBtn) self.topWidgetLayout.addWidget(self.textBtn) self.topWidgetLayout.addWidget(self.comboFont) self.topWidgetLayout.addWidget(self.comboSize) self.topWidgetLayout.addWidget(self.comboStyle) def setupFileActions(self): menu = QMenu("&File", self) self.fileBtn.setMenu(menu) self.actionNew = QAction("&New", self, priority=QAction.LowPriority, shortcut=QKeySequence.New, triggered=self.fileNew) menu.addAction(self.actionNew) self.actionNew = QtGui.QAction( "&New", self, priority=QtGui.QAction.LowPriority, shortcut=QtGui.QKeySequence.New, triggered=self.fileNew) menu.addAction(self.actionNew) self.actionOpen = QtGui.QAction( "&Open...", self, shortcut=QtGui.QKeySequence.Open, triggered=self.fileOpen) menu.addAction(self.actionOpen) menu.addSeparator() self.actionSave = QtGui.QAction( "&Save", self, shortcut=QtGui.QKeySequence.Save, triggered=self.fileSave, enabled=False) menu.addAction(self.actionSave) self.actionSaveAs = QtGui.QAction("Save &As...", self, priority=QtGui.QAction.LowPriority, shortcut=QtCore.Qt.CTRL + QtCore.Qt.SHIFT + QtCore.Qt.Key_S, triggered=self.fileSaveAs) menu.addAction(self.actionSaveAs) menu.addSeparator() self.actionPrint = QtGui.QAction( "&Print...", self, priority=QtGui.QAction.LowPriority, shortcut=QtGui.QKeySequence.Print, triggered=self.filePrint) menu.addAction(self.actionPrint) self.actionPrintPreview = QtGui.QAction( "Print Preview...", self, shortcut=QtCore.Qt.CTRL + QtCore.Qt.SHIFT + QtCore.Qt.Key_P, triggered=self.filePrintPreview) menu.addAction(self.actionPrintPreview) self.actionPrintPdf = QtGui.QAction( "&Export PDF...", self, priority=QtGui.QAction.LowPriority, shortcut=QtCore.Qt.CTRL + QtCore.Qt.Key_D, triggered=self.filePrintPdf) menu.addAction(self.actionPrintPdf) menu.addSeparator() self.actionQuit = QtGui.QAction("&Quit", self, shortcut=QtGui.QKeySequence.Quit, triggered=self.close) menu.addAction(self.actionQuit) def setupEditActions(self): menu = QMenu("&Edit", self) self.editBtn.setMenu(menu) self.actionUndo = QtGui.QAction( "&Undo", self, shortcut=QtGui.QKeySequence.Undo) menu.addAction(self.actionUndo) self.actionRedo = QtGui.QAction( "&Redo", self, priority=QtGui.QAction.LowPriority, shortcut=QtGui.QKeySequence.Redo) menu.addAction(self.actionRedo) menu.addSeparator() self.actionCut = QtGui.QAction( "Cu&t", self, priority=QtGui.QAction.LowPriority, shortcut=QtGui.QKeySequence.Cut) menu.addAction(self.actionCut) self.actionCopy = QtGui.QAction( "&Copy", self, priority=QtGui.QAction.LowPriority, shortcut=QtGui.QKeySequence.Copy) menu.addAction(self.actionCopy) self.actionPaste = QtGui.QAction( "&Paste", self, priority=QtGui.QAction.LowPriority, shortcut=QtGui.QKeySequence.Paste, enabled=(len(QtGui.QApplication.clipboard().text()) != 0)) menu.addAction(self.actionPaste) def fileNew(self): print "create new file" if self.maybeSave(): self.textEdit.clear() self.setCurrentFileName() def fileOpen(self): print "open file" fn = QFileDialog.getOpenFileName(self, "Open File...", None, "HTML-Files (*.htm *html);;ALL Files (*)") if fn: self.load(fn) def filePrint(self): printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution) dlg = QtGui.QPrintDialog(printer, self) if self.textEdit.textCursor().hasSelection(): dlg.addEnabledOption(QtGui.QAbstractPrintDialog.PrintSelection) dlg.setWindowTitle("Print Document") if dlg.exec_() == QtGui.QDialog.Accepted: self.textEdit.print_(printer) del dlg def filePrintPdf(self): print "print pdf" fn = QtGui.QFileDialog.getSaveFileName(self, "Export PDF", None, "PDF files (*.pdf);;All Files (*)") if fn: if QtCore.QFileInfo(fn).suffix().isEmpty(): fn += ‘.pdf‘ printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution) printer.setOutputFormat(QtGui.QPrinter.PdfFormat) printer.setOutputFileName(fileName) self.textEdit.document().print_(printer) def printPreview(self, printer): self.textEdit.print_(printer) def filePrintPreview(self): print "print Preview" printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution) preview = QtGui.QPrintPreviewDialog(printer, self) preview.paintRequested.connect(self.printPreview) preview.exec_() def fileSave(self): print "save file" if not self.fileName: return self.fileSaveAs() writer = QTextDocumentWriter(self.fileName) success = writer.write(self.textEdit.document()) if success: self.textEdit.document().setModified(False) return success def fileSaveAs(self): print "file save as" fn = QtGui.QFileDialog.getSaveFileName(self, "Save as...", "C:/", "ODF files (*.odt);;HTML-Files (*.htm *.html);;All Files (*)") if not fn: return False lfn = fn.lower() if not lfn.endswith((‘.odt‘, ‘.htm‘, ‘.html‘)): # The default. fn += ‘.odt‘ self.setCurrentFileName(fn) return self.fileSave() def load(self, f): if not QtCore.QFile.exists(f): return False fh = QtCore.QFile(f) if not fh.open(QtCore.QFile.ReadOnly): return False data = http://www.mamicode.com/fh.readAll()>上图
pyqt 学习基础5 - 笔记软件的编辑器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。