1 回答

TA贡献1828条经验 获得超6个赞
我认为您的意思是您成功将此代码添加到主窗口,但您希望能够将其添加到您在设计器中制作的小部件中,而不是严格意义上的主窗口。
如果是这种情况,您想要做的是在 Qt Designer 中按照您想要的方式格式化您的小部件,在您想要插入 Display_Pixels QGraphicsView 对象的位置添加一个占位符,例如框架、布局、可能是一个组等。
之后不会直接将其添加到框架中,而是将该框架使用的任何布局。所以如果你的框架中有一个网格布局,它会是这样的:
self.Disp_pixel = Display_Pixels()
widget_name.gridlayout.addWidget(self.Disp_pixel)
很难告诉您这段代码将在您的代码中的何处实现,但假设您运行了 pyuic5 并获得了 python 输出。您可以将它添加到该输出 python 文件中。我建议使用类似查看器类的东西,它继承生成的输出类并在其中执行添加小部件。此外,frame 将是您为图形视图创建的框架的名称,而不是字面上的框架。
此外,我不确定您是否可以在任何类型的小部件上进行展示,我很确定小部件必须是可显示的,例如主窗口小部件或对话框小部件。也就是说小部件可以放置在其他小部件中。
这是我给你的一个例子,它有点粗糙,但我认为它会有所帮助:
import sys
import cv2
import numpy as np
import math
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtWidgets import QGraphicsView, QApplication
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '.\testUI.ui' and .\testMainWin.ui
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.frame = QtWidgets.QFrame(Form)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.gridLayout_2 = QtWidgets.QGridLayout(self.frame)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout.addWidget(self.frame, 0, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
class FormView(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(FormView, self).__init__(parent)
self.setupUi(self)
self.disp_pixels = Display_Pixels()
self.gridLayout_2.addWidget(self.disp_pixels)
class MainView(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainView, self).__init__(parent)
self.setupUi(self)
class Display_Pixels(QGraphicsView):
def __init__(self, parent=None):
QGraphicsView.__init__(self, parent=parent)
#super().__init__()
self.initUI()
self.img = cv2.imread('resources/images/pic3.jpg')
def initUI(self):
self.setGeometry(100, 100, 650, 650)
#self.setWindowTitle('By Pixel')
#self.setMouseTracking(True)
#self.show()
res = 40
self.grid = np.array([ [-1] * res for n in range(res)]) # list comprehension
#print(self.grid.shape)
def paintEvent(self, e):
qp = QPainter()
qp.begin(self.viewport())
self.drawRectangles(qp)
qp.end()
def drawRectangles(self, qp, w = 16):
print("Drawing")
mode = 0
x,y = 0,0 # starting position
lr = 20
hr = 35
col = QColor(0, 0, 0)
col.setNamedColor('#d4d4d4')
qp.setPen(col)
#print(self.img.shape)
for g_row, img_row in zip(self.grid, self.img):
#print(img_row.shape)
for g_col, img_col in zip(g_row, img_row):
r, g, b = (img_col[0], img_col[1], img_col[2])
#print(r,g,b)
if g_col == 1:
if mode == 0:
r = int(math.log(r+1)*lr)
g = int(math.log(g+1)*hr)
b = int(math.log(b+1)*lr)
elif mode == 1:
if r+50 <= 220: r = r+50
if g+80 <= 255: g = g+80
if b+50 <= 220: b = b+50
else:
if r+70 <= 220: r = r+70
if g+140 <= 255: g = g+140
if b+70 <= 220: b = b+70
qp.setBrush(QColor(r, g, b))
qp.drawRect(x, y, w, w)
else:
qp.setBrush(QColor(r, g, b))
qp.drawRect(x, y, w, w)
#qp.setBrush(QColor(200, 0, 0))
#qp.drawRect(x, y, w, w)
x = x + w # move right
y = y + w # move down
x = 0 # rest to left edge
def mousePressEvent(self, QMouseEvent):
w = 16.0
#print("MOUSE:")
#print('(', int(QMouseEvent.x()/w), ', ', int(QMouseEvent.y()/w), ')')
#print (QMouseEvent.pos())
x = float(QMouseEvent.x())
y = float(QMouseEvent.y())
self.grid[int(y/w)][int(x/w)] = -1 * self.grid[int(y/w)][int(x/w)]
#print(img[int(y/w), int(x/w), :])
self.repaint()
#self.update()
if __name__ == '__main__':
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
main_window = MainView()
form = FormView()
main_window.gridLayout.addWidget(form)
main_window.show()
sys.exit(app.exec_())
添加回答
举报