如何从对话框更改重绘窗口小部件?

时间:2019-06-19 12:16:55

标签: python pyside

我对PySide编码非常陌生,并且遇到了麻烦。我正在构建一个小控件,该控件在屏幕上绘制标签。

我已经开始了冰箱磁铁的例子。我想知道如何从对话框中更新冷藏柜的颜色。

谢谢。

from PySide import QtCore, QtGui

import fridgemagnets_rc

class PropertyChangeWindow(QtGui.QDialog):
    " Property Change Window"
    def __init__(self, magnet_text, parent=None):

        super(PropertyChangeWindow, self).__init__(parent)
        self.block = magnet_text
        self.setWindowTitle("Properties of " + magnet_text)
        self.setMinimumSize(160, 160)

        buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)

        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)


    def accept(self):
        self.hide()

        print ("Change Magnet Color to Green")
        print ("Redraw all Magnets")


class DragLabel(QtGui.QLabel):
    def __init__(self, text, parent):
        super(DragLabel, self).__init__(parent)
        self.text = text

        metric = QtGui.QFontMetrics(self.font())
        size = metric.size(QtCore.Qt.TextSingleLine, text)

        image = QtGui.QImage(size.width() + 12, size.height() + 12,
                QtGui.QImage.Format_ARGB32_Premultiplied)
        image.fill(QtGui.qRgba(0, 0, 0, 0))

        font = QtGui.QFont()
        font.setStyleStrategy(QtGui.QFont.ForceOutline)

        painter = QtGui.QPainter()
        painter.begin(image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(QtCore.Qt.blue)
        painter.drawRoundedRect(QtCore.QRectF(0.1, 0.1, image.width()-1,
                image.height()-1), 25, 25, QtCore.Qt.RelativeSize)

        painter.setFont(font)
        painter.setBrush(QtCore.Qt.black)
        painter.drawText(QtCore.QRect(QtCore.QPoint(6, 6), size),
                QtCore.Qt.AlignCenter, text)
        painter.end()

        self.setPixmap(QtGui.QPixmap.fromImage(image))
        self.labelText = text

    def mousePressEvent(self, event):
        itemData = QtCore.QByteArray()
        dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
        dataStream << QtCore.QByteArray(str(self.labelText)) << QtCore.QPoint(event.pos() - self.rect().topLeft())

        mimeData = QtCore.QMimeData()
        mimeData.setData('application/x-fridgemagnet', itemData)
        mimeData.setText(self.labelText)

        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(event.pos() - self.rect().topLeft())
        drag.setPixmap(self.pixmap())

        self.hide()

        if drag.exec_(QtCore.Qt.MoveAction | QtCore.Qt.CopyAction, QtCore.Qt.CopyAction) == QtCore.Qt.MoveAction:
            self.close()
        else:
            self.show()

    def mouseReleaseEvent(self, event):
        if self._clickevent == "Single Click":
            QtCore.QTimer.singleShot(400, self.performSingleClickAction)
        else:
            self._clickevent = "Double Click"

    def mouseDoubleClickEvent(self, event):
        self._clickevent = "Double Click"
        self.new_window = PropertyChangeWindow(self.text)
        self.new_window.show()


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

        dictionaryFile = QtCore.QFile(':/dictionary/words.txt')
        dictionaryFile.open(QtCore.QFile.ReadOnly)

        x = 5
        y = 5

        for word in QtCore.QTextStream(dictionaryFile).readAll().split():
            wordLabel = DragLabel(word, self)
            wordLabel.move(x, y)
            wordLabel.show()
            x += wordLabel.width() + 2
            if x >= 245:
                x = 5
                y += wordLabel.height() + 2

        newPalette = self.palette()
        newPalette.setColor(QtGui.QPalette.Window, QtCore.Qt.white)
        self.setPalette(newPalette)

        self.setMinimumSize(400, max(200, y))
        self.setWindowTitle("Fridge Magnets")
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasFormat('application/x-fridgemagnet'):
            if event.source() in self.children():
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
        elif event.mimeData().hasText():
            event.acceptProposedAction()
        else:
            event.ignore()

    dragMoveEvent = dragEnterEvent

    def dropEvent(self, event):
        if event.mimeData().hasFormat('application/x-fridgemagnet'):
            mime = event.mimeData()
            itemData = mime.data('application/x-fridgemagnet')
            dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)

            text = QtCore.QByteArray()
            offset = QtCore.QPoint()
            dataStream >> text >> offset

            try:
                # Python v3.
                text = str(text, encoding='latin1')
            except TypeError:
                # Python v2.
                text = str(text)

            newLabel = DragLabel(text, self)
            newLabel.move(event.pos() - offset)
            newLabel.show()

            if event.source() in self.children():
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
        elif event.mimeData().hasText():
            pieces = event.mimeData().text().split()
            position = event.pos()

            for piece in pieces:
                newLabel = DragLabel(piece, self)
                newLabel.move(position)
                newLabel.show()

                position += QtCore.QPoint(newLabel.width(), 0)

            event.acceptProposedAction()
        else:
            event.ignore()


if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    window = DragWidget()
    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

每个类都有一个任务,在这种情况下,最好的事情是PropertyChangeWindow不会更改颜色而是返回颜色,并且在DragLabel中使用以前的信息来更改其颜色。

如果您分析初始代码,则会注意到QDataStream存储了相关信息,例如文本和位置,为此我们还必须添加QColor。

考虑上述解决方案是:

from PySide import QtCore, QtGui

import fridgemagnets_rc


class PropertyChangeWindow(QtGui.QDialog):
    " Property Change Window"

    def __init__(self, magnet_text, parent=None):

        super(PropertyChangeWindow, self).__init__(parent)
        self.block = magnet_text
        self.setWindowTitle("Properties of " + magnet_text)
        self.setMinimumSize(160, 160)

        buttonBox = QtGui.QDialogButtonBox(
            QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel
        )

        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)
        self.m_color = QtGui.QColor()

    def accept(self):
        self.m_color = QtGui.QColor("green")
        super(PropertyChangeWindow, self).accept()

    @property
    def color(self):
        return self.m_color


class DragLabel(QtGui.QLabel):
    def __init__(self, text, color=QtGui.QColor(QtCore.Qt.blue), parent=None):
        super(DragLabel, self).__init__(parent)
        self.labelText = text
        self.color = color
        self.redraw()

    def redraw(self):
        metric = QtGui.QFontMetrics(self.font())
        size = metric.size(QtCore.Qt.TextSingleLine, self.labelText)

        image = QtGui.QImage(
            size.width() + 12,
            size.height() + 12,
            QtGui.QImage.Format_ARGB32_Premultiplied,
        )
        image.fill(QtGui.qRgba(0, 0, 0, 0))

        font = QtGui.QFont()
        font.setStyleStrategy(QtGui.QFont.ForceOutline)

        painter = QtGui.QPainter()
        painter.begin(image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(self.color)
        painter.drawRoundedRect(
            QtCore.QRectF(0.1, 0.1, image.width() - 1, image.height() - 1),
            25,
            25,
            QtCore.Qt.RelativeSize,
        )

        painter.setFont(font)
        painter.setBrush(QtCore.Qt.black)
        painter.drawText(
            QtCore.QRect(QtCore.QPoint(6, 6), size),
            QtCore.Qt.AlignCenter,
            self.labelText,
        )
        painter.end()

        self.setPixmap(QtGui.QPixmap.fromImage(image))

    def mousePressEvent(self, event):
        itemData = QtCore.QByteArray()
        dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
        dataStream << QtCore.QByteArray(str(self.labelText)) << QtCore.QPoint(
            event.pos() - self.rect().topLeft()
        )
        dataStream << self.color

        mimeData = QtCore.QMimeData()
        mimeData.setData("application/x-fridgemagnet", itemData)
        mimeData.setText(self.labelText)

        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(event.pos() - self.rect().topLeft())
        drag.setPixmap(self.pixmap())

        self.hide()

        if (
            drag.exec_(
                QtCore.Qt.MoveAction | QtCore.Qt.CopyAction,
                QtCore.Qt.CopyAction,
            )
            == QtCore.Qt.MoveAction
        ):
            self.close()
        else:
            self.show()

    def mouseReleaseEvent(self, event):
        if self._clickevent == "Single Click":
            QtCore.QTimer.singleShot(400, self.performSingleClickAction)
        else:
            self._clickevent = "Double Click"

    def mouseDoubleClickEvent(self, event):
        self._clickevent = "Double Click"
        new_window = PropertyChangeWindow(self.labelText)
        if new_window.exec_() == QtGui.QDialog.Accepted:
            self.color = new_window.color
            self.redraw()


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

        dictionaryFile = QtCore.QFile(":/dictionary/words.txt")
        dictionaryFile.open(QtCore.QFile.ReadOnly)

        x = 5
        y = 5

        for word in QtCore.QTextStream(dictionaryFile).readAll().split():
            wordLabel = DragLabel(word, parent=self)
            wordLabel.move(x, y)
            wordLabel.show()
            x += wordLabel.width() + 2
            if x >= 245:
                x = 5
                y += wordLabel.height() + 2

        newPalette = self.palette()
        newPalette.setColor(QtGui.QPalette.Window, QtCore.Qt.white)
        self.setPalette(newPalette)

        self.setMinimumSize(400, max(200, y))
        self.setWindowTitle("Fridge Magnets")
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasFormat("application/x-fridgemagnet"):
            if event.source() in self.children():
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
        elif event.mimeData().hasText():
            event.acceptProposedAction()
        else:
            event.ignore()

    dragMoveEvent = dragEnterEvent

    def dropEvent(self, event):
        if event.mimeData().hasFormat("application/x-fridgemagnet"):
            mime = event.mimeData()
            itemData = mime.data("application/x-fridgemagnet")
            dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)

            text = QtCore.QByteArray()
            offset = QtCore.QPoint()
            color = QtGui.QColor()
            dataStream >> text >> offset >> color

            try:
                # Python v3.
                text = str(text, encoding="latin1")
            except TypeError:
                # Python v2.
                text = str(text)

            newLabel = DragLabel(text, color, self)
            newLabel.move(event.pos() - offset)
            newLabel.show()

            if event.source() in self.children():
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
        elif event.mimeData().hasText():
            pieces = event.mimeData().text().split()
            position = event.pos()

            for piece in pieces:
                newLabel = DragLabel(piece, self)
                newLabel.move(position)
                newLabel.show()

                position += QtCore.QPoint(newLabel.width(), 0)

            event.acceptProposedAction()
        else:
            event.ignore()


if __name__ == "__main__":

    import sys

    app = QtGui.QApplication(sys.argv)
    window = DragWidget()
    window.show()
    sys.exit(app.exec_())