python - Inserting QImage after string in QTextEdit -
I am trying to print the text in the QTextEdit field but for some reasons this image appears first.
Here is my code: PyQt4.QtGui import from *
* PyQt4.QtCore import from class example (QtGui.QWidget) * PyQt4 import QtCore, Import sys from QtGui: Def __init __ (self): super (for example, self) .__ init __ () self.initUI () def initUI (self): TextEdit = QtGui.QTextEdit (', self) textEdit.setGeometry (QtCore.QRect ( 300, 300, 640, 480)) TextEdit. The trick (0, 0) with self geometry (300, 300, 640, 480) img = QImage ('image.png', 'PNG') cursor = QTextCursor (textEdit.document ()) cursor.instatext ("Hello World" ) Cursor. Instant Image (IMG) Self. Show () DIF Main (): App = QTGui QAPScation (SISGR) Example = (for example) sys.exit (app.exec_ ()) if __name__ == '__main__': main () This looks like this in my QTextEdit field
some image Hello World but I want to show it:
Hello World some image The image is at the top of the string Besides, there is a big ugly cursor in the form of my image (500 pixels high). What code should I use a) Print before the string image and b) The cursor is hidden after I insert it?
You will need the cursor position where you want to insert the image. Check this code:
#! / Usr / bin / env python # - * - coding: UTF-8 - * - QtGui, QtCore class MyWindow (QtGui.QWidget) from PyQt4 import: def __init __ (self, parent = none): Super (MyWindow, Self) .__ init __ (parent) self.pushButtonImage = QtGui.QPushButton (self) self.pushButtonImage.setText self.pushButtonImage.clicked.connect (("Insert Image!") Self.on_pushButtonImage_clicked) self.textEditImage = QtGui.QtextEdit (Self) self.textEditImage.setPlainText ("Insert an image here:") self.layoutVertical = QtGui.QVBoxLayout (self) self.layoutVertical.addWidget (self.pushButtonImage) self LayoutVertical.addWidget (self.textEditImage) def on_pushButtonImage_clicked (auto) :. FilePath = QtGui.QFileDialog.getOpenFileName (self image files (* png * .gif * .jpg * jpeg, "select an image", "*". ".bmp)") If not filePath.isEmpty (): self .insertImage (filePath) def insertImage (self, filePath): imageUri = QtCore.QUrl (QtCore.QString ( "file: // {0}" .format (filePath))) image = QtGui.QImage (QtGui.QImageReader (filePath ) .read ()) self.textEditImage.document (). addResource (QtGui.QTextDocument.ImageResource, imageUri, QtCore.QVariant (image)) imageFormat = QtGui.QTextImageFormat () imageFormat.setWidth (image.width ()) imageFormat.setHeight (image.height ()) ImageFormat.setName (imageUri. ToString ()) textCursor = self.textEditImage.textCursor () textCursor.movePosition (QtGui.QTextCursor.End, QtGui.QTextCursor.MoveAnchor) textCursor.insertImage (imageFormat) # This cursor will hide the empty Caesar = QtGui.QCursor (QtCore.Qt. BlankCursor) self.textEditImage.setCursor (blankCursor) If __name__ == "__main__": import sys app = QtGui.QApplication (sys.argv) app.setApplicationName ( 'MyWindow') main = MyWindow (main). Show () sys.exit (app.exec_ ())
Comments
Post a Comment