Форум сайта python.su
Привет всем. Имеется программный код в котором, когда вы нажимаете на пиксель изображения, загруженного через OpenFileDialog в Pixmap QGraphicsScene, выводит значения цвета x, y и HEX в QLable. Кто-нибудь может подсказать, как HEX также можно вывести в ячейку QTableWidget?
def table(self): # создание таблицы self.table = QtWidgets.QTableWidget(self.centralwidget) self.table.setGeometry(QtCore.QRect(10, 50, 462, 950)) self.table.setStyleSheet("background-color:rgb(209, 209, 209)") self.headerLabels = list('A') n = 3000 self.table.setRowCount(n) self.table.setColumnCount(len(self.headerLabels)) self.table.setHorizontalHeaderLabels(self.headerLabels) self.table.horizontalHeader().setDefaultSectionSize(100) self.table.horizontalHeader().setMinimumSectionSize(125) self.table.verticalHeader().setVisible(False) self.table.verticalHeader().setDefaultSectionSize(25) self.table.verticalHeader().setMinimumSectionSize(25) for row in range(n): for col in range(len(self.headerLabels)): item = QTableWidgetItem(''.format(self.headerLabels[col], row)) self.table.setItem(row, col, item) self.table.resizeColumnsToContents() self.table.resizeRowsToContents()
class GraphicsView(QGraphicsView): # +++ clicked = pyqtSignal(str, str) def mousePressEvent(self, event): super(GraphicsView, self).mousePressEvent(event) self.setCursor(Qt.DragMoveCursor) # Получить положение мыши на экране pos = event.globalPos() image = QApplication.primaryScreen().grabWindow( int(QApplication.desktop().winId()), pos.x() - 23, pos.y() - 23, 47, 47).toImage() color = image.pixelColor(23, 23) if color.isValid(): self.clicked.emit('View', color.name()) def mouseReleaseEvent(self, event): super(GraphicsView, self).mouseReleaseEvent(event) self.setCursor(Qt.ArrowCursor)
class MainssWindow(QMainWindow, Ui_MainsWindow): def __init__(self): super().__init__() self.setupUi(self) self.layout = QtWidgets.QHBoxLayout(self.wid) self.layout.addWidget(self.wid) self.scene = GraphicsScene() self.scene.setSceneRect(0, 0, 1400, 900) width = Settings.NUM_BLOCKS_X * Settings.WIDTH height = Settings.NUM_BLOCKS_Y * Settings.HEIGHT self.scene.setSceneRect(0, 0, width, height) self.scene.setItemIndexMethod(QtWidgets.QGraphicsScene.NoIndex) self.setMouseTracking(True) self.graphicsView.setScene(self.scene) self.scene.clicked.connect(self.point1) self.graphicsView.clicked.connect(self.point1) # +++ self.table()
class QS(QtWidgets.QGraphicsScene): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) width = Settings.NUM_BLOCKS_X * Settings.WIDTH height = Settings.NUM_BLOCKS_Y * Settings.HEIGHT self.setSceneRect(0, 0, width, height) self.setItemIndexMethod(QtWidgets.QGraphicsScene.NoIndex) for x in range(0,Settings.NUM_BLOCKS_X+1): xc = x * Settings.WIDTH self.addLine(xc,0,xc,height) for y in range(0,Settings.NUM_BLOCKS_Y+1): yc = y * Settings.HEIGHT self.addLine(0,yc,width,yc)
class GraphicsScene(QGraphicsScene): clicked = pyqtSignal(str, QPointF) def mousePressEvent(self, event): super(GraphicsScene, self).mousePressEvent(event) sp = event.scenePos() self.clicked.emit('Scene', sp)
def point1(self, text, value): if text == 'Scene': self.lab.setText(f'x1 = {value.x()}, y1 = {value.y()}. Интенсивность: ') if text == 'View': self.lab.setText(f'{self.lab.text()} {value}')
Отредактировано maroonlok (Фев. 24, 2023 19:13:49)
Офлайн