簡體   English   中英

如何使用pyqt4將像素圖和文本設置為Qlabel

[英]How to set the pixmap and text to Qlabel by using pyqt4

在這里我想使用listdir將Pixmap和文本添加到QLabel,我得到了我的文件名,但是文件名正在循環合並,所以任何人都可以幫助我如何將我的文件名放在QLabel中。請提前幫助我。

import os
from PyQt4 import QtCore, QtGui
class MyLabel(QtGui.QLabel):
    def __init__(self):
        super(MyLabel, self).__init__()

    def paintEvent(self, event):
        super(MyLabel, self).paintEvent(event)
        pos = QtCore.QPoint(100, 100)
        painter = QtGui.QPainter(self)

        s = iter(os.listdir("/home/cioc/Documents/pos/images"))
        # file1 = next(s)
        painter.drawText(pos,"vegitables")#here in place of vegitables i want to print the my filenames
        painter.setPen(QtGui.QColor(168, 34, 3))


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        url = '/home/cioc/Documents/pos/images'
        highlight_dir = url

        self.scroll_area = QtGui.QScrollArea()

        self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.setCentralWidget(self.scrollArea)
        content_widget = QtGui.QWidget()
        self.scrollArea.setWidget(content_widget)
        self.layout = QtGui.QGridLayout(content_widget)

        self.files_it = iter([os.path.join(highlight_dir, file) for file in os.listdir(highlight_dir)])
        try:
            for row in range(3):
                for column in range(3):
                    file = next(self.files_it)
                    pixmap = QtGui.QPixmap(file)
                    self.label = MyLabel()
                    self.label.setPixmap(pixmap)
                    self.layout.addWidget(self.label,row,column)
        except StopIteration:
            pass

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.setGeometry(500,300,800,400)
    w.show()
    sys.exit(app.exec_())

似乎text和pixmap屬性不能共存,因為在建立一個屬性時就消除了前一個屬性。 因此,可能的解決方案是保存一個新屬性。

對於這種情況,我將使用QDirIterator來獲取文件,並使用QTimer來稍微加載圖像,因為如果您有很多圖像,則應用程序示例中將有延遲或凍結。

from PyQt4 import QtCore, QtGui

class MyLabel(QtGui.QLabel):
    def paintEvent(self, event):
        super(MyLabel, self).paintEvent(event)
        painter = QtGui.QPainter(self)
        painter.setPen(QtGui.QColor(168, 34, 3))
        painter.drawText(self.rect(), QtCore.Qt.AlignCenter, self.my_text)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        highlight_dir = '/home/cioc/Documents/pos/images'
        scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.setCentralWidget(scrollArea)
        content_widget = QtGui.QWidget()
        scrollArea.setWidget(content_widget)
        self._layout = QtGui.QGridLayout(content_widget)
        self._it = QtCore.QDirIterator(highlight_dir)
        self._row, self._col = 0, 0
        QtCore.QTimer(self, interval=10, timeout=self.load_image).start()

    @QtCore.pyqtSlot()
    def load_image(self):
        if self._it.hasNext():
            pixmap = QtGui.QPixmap(self._it.next())
            if not pixmap.isNull():
                label = MyLabel(alignment=QtCore.Qt.AlignCenter)
                label.my_text = self._it.fileName()
                label.setPixmap(pixmap)
                self._layout.addWidget(label, self._row, self._col)
                self._col += 1      
                if self._col == 3:
                    self._row += 1
                    self._col = 0

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.setGeometry(500, 300, 800, 400)
    w.show()
    sys.exit(app.exec_())

更新:

@QtCore.pyqtSlot()
def load_image(self):
    if self._it.hasNext():
        pixmap = QtGui.QPixmap(self._it.next())
        if not pixmap.isNull():
            vlay = QtGui.QVBoxLayout()
            label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
            label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
            vlay.addWidget(label_pixmap)
            vlay.addWidget(label_text)
            vlay.addStretch()
            self._layout.addLayout(vlay, self._row, self._col)
            self._col += 1      
            if self._col == 3:
                self._row += 1
                self._col = 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM