pyqt通过鼠标单击关闭主窗口中的自定义对话框

问题描述 投票:0回答:1

Q.是否可能,对话框关闭自己并在用户点击或双击颜色项时返回颜色名称下面是工作示例(希望能够演示问题)。单击画布区域将弹出颜色对话框。当前用户必须选择颜色,然后点击“确定”按钮,当用户点击颜色时,意图完成。只是想节省用户时间,一点。

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic, QtMultimedia, QtMultimediaWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

colorsDict = { 'White': '0xFFFFFF','DarkGray': '0xA9A9A9','DarkSlateGray': '0x2F4F4F','LightSlateGray': '0x778899','DimGray': '0x696969','Gray': '0x808080','SlateGray': '0x708090','Black': '0x000000','DarkRed': '0x8B0000','Darkorange': '0xFF8C00','FireBrick': '0xB22222','Crimson': '0xDC143C','Salmon': '0xFA8072'}
def hexToQColor (h):
    h = h.lstrip('#') # general usage safety
    # h = h.lstrip('0x') # my use case
    if h.find('0x') == 0:
        h = h.split('0x')[1]
    rgb = tuple(int(h[i:i+2], 16) for i in (0, 2 ,4))
    return QColor(rgb[0],rgb[1],rgb[2])
class FfmpegColorDialog(QDialog):
    """
        Custom FFMPEG Color Picker class
    """
    resized = QtCore.pyqtSignal()
    def __init__(self, parent=None):
        super(FfmpegColorDialog, self).__init__(parent)
        # self.ui = uic.loadUi('ui/ffmpeg_colors.ui', self)
        self.setWindowTitle("FFMPEG Color Picker")
        self.listWidget = QListWidget()
        self.readPrefs()
        self.listWidget.setFlow(QListView.LeftToRight)
        self.listWidget.setResizeMode(QListView.Adjust)
        self.listWidget.setGridSize(QSize(32, 32))
        self.listWidget.setSpacing(5)
        self.listWidget.setViewMode(QListView.IconMode)
        self.listWidget.itemClicked.connect(self.itemClicked)
        self.listWidget.itemDoubleClicked.connect(self.itemDoubleClicked)
        layout = QVBoxLayout(self)
        layout.addWidget(self.listWidget)

        # OK and Cancel buttons
        buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self)
        buttons.accepted.connect(self.accept)
        buttons.rejected.connect(self.reject)
        layout.addWidget(buttons)

    def readPrefs(self):
        """
        reading preferences from module for Data in UI
        """
        for each in colorsDict.keys():
            item = colorsDict[each]
            listItem = QListWidgetItem()
            listItem.setToolTip(each)

            listItem.setSizeHint(QSize(30, 30))
            color = hexToQColor(item)
            listItem.setBackground(QBrush(color))
            self.listWidget.addItem(listItem)
    def itemClicked(self,item):
        self.listWidget.setCurrentItem(item)
        # self.accept()
    def itemDoubleClicked(self,item):
        c = item.background().color()
        self.listWidget.setCurrentItem(item)
        result = self.exec_()
        return(c,result==QDialog.Accepted)

    def getResults(self):
        if self.exec_() == QDialog.Accepted:
            item = self.listWidget.currentItem()
            # print (item.toolTip())
            return ( item.toolTip())
        else:
            return None
    def getUserColor(self):
        return (self.listWidget.currentItem().toolTip())
    @staticmethod
    def getFinalColor(parent=None):
        dialog = FfmpegColorDialog(parent)
        result = dialog.exec_()
        color = dialog.getUserColor()
        return(color,result==QDialog.Accepted)
class MainWindow(QMainWindow):

    central_widget = None
    layout_container = None

    def __init__(self):
        super(MainWindow, self).__init__()
        self.central_widget = QWidget()
        self.layout_container = QVBoxLayout()
        self.central_widget.setLayout(self.layout_container)
        self.setCentralWidget(self.central_widget)
        self.layout_container.addWidget(GraphicsView())

class GraphicsView(QGraphicsView):

    def __init__(self):
        super(GraphicsView, self).__init__()
        self.scene = QGraphicsScene()
        self.setScene(self.scene)
        self.text = None
        self.createText()

    def createText(self):
        self.text = QGraphicsTextItem()
        font = QFont()
        font.setPixelSize(40)
        self.text.setFont(font)
        self.text.setPlainText("Sample Text To Test")
        self.scene.addItem(self.text)
    def mousePressEvent(self, event):
        r,ok = FfmpegColorDialog.getFinalColor()
        hc = colorsDict[r]
        rgb = hexToQColor(hc)
        self.text.setDefaultTextColor(rgb)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # dia = FfmpegColorDialog()
    # dia.show()
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())
python pyqt pyqt5 qdialog
1个回答
2
投票

只需将QListWidget的单击信号连接到FfmpegColorDialog的接受槽:

class FfmpegColorDialog(QDialog):
    """
        Custom FFMPEG Color Picker class
    """
    resized = QtCore.pyqtSignal()
    def __init__(self, parent=None):
        super(FfmpegColorDialog, self).__init__(parent)
        # ...
        self.listWidget = QListWidget()
        self.listWidget.clicked.connect(self.accept) # <---
        # ...
© www.soinside.com 2019 - 2024. All rights reserved.