python - 在 QTextEdit 中的字符串后插入 QImage

标签 python qt pyqt pyqt4 pyside

我正在尝试将文本打印到 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) 插入完成后隐藏光标?

最佳答案

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

#!/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_())

关于python - 在 QTextEdit 中的字符串后插入 QImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15539075/

相关文章:

python - 如何获取从 QListView 中选择的项目?

c++ - 使用 StyleSheet 悬停时 qt 加宽 QScrollBar

python - 从 QClipboard 复制/粘贴文本会卡住程序

python - 获取单词的上下文

python - Manage.py sqlall <myapp> 不创建列

python - Tweepy 库中存储的 next_cursor 值在哪里?

c++ - 如何使用 Qt/c++ 为所有 UNIX 操作系统创建托盘图标?

python - pyqt:如何在外部变量值更改时动态更新小部件属性?

python - QLineEdit 中的文本不显示

python - 如何使用jinja在没有flask的情况下渲染HTML?