1

我正在尝试将文本打印到 QTextEdit 字段中,但由于某种原因,图像首先出现。

这是我的代码:

import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        textEdit = QtGui.QTextEdit('',self)
        textEdit.setGeometry(QtCore.QRect(300, 300, 640, 480))
        textEdit.move(0, 0)
        self.setGeometry(300, 300, 640, 480)

        img = QImage('image.png','PNG')

        cursor = QTextCursor(textEdit.document())
        cursor.insertText("Hello World")
        cursor.insertImage(img)

        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

在我的 QTextEdit 字段中看起来像这样:

some image
Hello World

但我希望它看起来像:

Hello World
some image

图像位于字符串的顶部。此外,还有一个和我的图像一样高的丑陋光标(500 像素高)。我应该使用什么代码a)在图像之前打印字符串,b)在我完成插入后光标被隐藏?

4

1 回答 1

3

您需要将光标定位在要插入图像的位置。签出此代码:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonImage = QtGui.QPushButton(self)
        self.pushButtonImage.setText("Insert Image!")
        self.pushButtonImage.clicked.connect(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(self):
        filePath = QtGui.QFileDialog.getOpenFileName(
            self,
            "Select an image",
            ".",
            "Image Files(*.png *.gif *.jpg *jpeg *.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 will hide the cursor
        blankCursor = 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_())
于 2013-03-21T06:04:26.067 回答