def dragFile(widget, filename, icon=None, dropactions=Qt.CopyAction): """Starts dragging the given local file from the widget.""" if icon is None or icon.isNull(): icon = QFileIconProvider().icon(QFileInfo(filename)) drag = QDrag(widget) data = QMimeData() data.setUrls([QUrl.fromLocalFile(filename)]) drag.setMimeData(data) drag.setPixmap(icon.pixmap(32)) drag.exec_(dropactions)
def dragFile(widget, filename, icon=None, dropactions=Qt.CopyAction): """Starts dragging the given local file from the widget.""" if icon is None or icon.isNull(): icon = QFileIconProvider().icon(QFileInfo(filename)) drag = QDrag(widget) data = QMimeData() data.setUrls([QUrl.fromLocalFile(filename)]) drag.setMimeData(data) drag.setPixmap(icon.pixmap(32)) drag.exec_(dropactions)
def paint(self, painter, option, index): filePath = self.model.filePath(index) fileName = self.model.fileName(index) r = option.rect img = QPixmap(filePath) if img.isNull(): # If not image file, try to load icon with QFileIconProvider # according to file type (extension name). # Currently not work as intended. fileInfo = self.model.fileInfo(index) icon = QFileIconProvider().icon(fileInfo) img = icon.pixmap(QSize(32, 32)) # Scale to height, align center horizontally, align bottom vertically. if img.height() > self.thumbHeight: img = img.scaledToHeight(self.thumbHeight, Qt.SmoothTransformation) if img.width() > self.thumbHeight: img = img.scaledToWidth(self.thumbHeight, Qt.SmoothTransformation) imgLeft = (self.width - img.width()) / 2 imgTop = self.thumbHeight - img.height() painter.drawPixmap(r.left() + imgLeft, r.top() + imgTop, img) rect = QRect(r.left(), r.top() + self.thumbHeight, self.width, self.nameHeight) flag = Qt.AlignHCenter | Qt.TextWrapAnywhere # get the bounding rectangle of the fileName bdRect = painter.boundingRect(rect, flag, fileName) if bdRect.height() < rect.height(): rect = bdRect if option.state & QStyle.State_Selected: painter.setBrush(self.parent().palette().highlight()) painter.drawRoundedRect(rect, 5, 5) pen = QPen(self.parent().palette().highlightedText(), 1, Qt.SolidLine) else: pen = QPen(self.parent().palette().text(), 1, Qt.SolidLine) painter.setPen(pen) painter.drawText(rect, flag, fileName)
def paint(self, painter, option, index): filePath = self.model.filePath(index) fileName = self.model.fileName(index) r = option.rect img = QPixmap(filePath) if img.isNull(): # If not image file, try to load icon with QFileIconProvider # according to file type (extension name). # Currently not work as intended. fileInfo = self.model.fileInfo(index) icon = QFileIconProvider().icon(fileInfo) img = icon.pixmap(QSize(32, 32)) # Scale to height, align center horizontally, align bottom vertically. if img.height() > self.thumbHeight: img = img.scaledToHeight(self.thumbHeight, Qt.SmoothTransformation) if img.width() > self.thumbHeight: img = img.scaledToWidth(self.thumbHeight, Qt.SmoothTransformation) imgLeft = (self.width - img.width()) / 2 imgTop = self.thumbHeight - img.height() painter.drawPixmap(r.left()+imgLeft, r.top()+imgTop, img) rect = QRect(r.left(), r.top()+self.thumbHeight, self.width, self.nameHeight) flag = Qt.AlignHCenter | Qt.TextWrapAnywhere # get the bounding rectangle of the fileName bdRect = painter.boundingRect(rect, flag, fileName) if bdRect.height() < rect.height(): rect = bdRect if option.state & QStyle.State_Selected: painter.setBrush(self.parent().palette().highlight()) painter.drawRoundedRect(rect, 5, 5) pen = QPen(self.parent().palette().highlightedText(), 1, Qt.SolidLine) else: pen = QPen(self.parent().palette().text(), 1, Qt.SolidLine) painter.setPen(pen) painter.drawText(rect, flag, fileName)