第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取自定義 Qt QGraphicsView 以使用 Qt Designer 制作的表單顯示

獲取自定義 Qt QGraphicsView 以使用 Qt Designer 制作的表單顯示

繁華開滿天機(jī) 2021-11-09 19:39:17
我將一個自定義 QGraphicsView 放在一起,我可以在 Python 的主窗口中顯示它,但我正在努力將它集成到我用 Qt Designer 制作的表單中。我如何將它添加到表單中?另外,我還添加了一個 mousepress 事件。如何使用 Qt Designer 制作的表單將其集成到應(yīng)用程序中?我已經(jīng)使用 Qt Designer 構(gòu)建了一個完整的應(yīng)用程序,其他一切正常,我只需要能夠?qū)⑦@部分與其余部分添加在一起。import sysfrom PySide2 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtWidgets import QFileDialogfrom PySide2.QtUiTools import QUiLoaderfrom PySide2.QtWidgets import QApplication, QPushButton, QLineEdit, QAction, QSliderfrom PySide2.QtWidgets import QListWidget, QTabWidget, QGraphicsView, QGraphicsScenefrom PySide2.QtWidgets import QSpinBox, QWidget, QDialog, QVBoxLayoutfrom PySide2.QtGui import QPixmap, QImage, QMatrix, QPainter, QColor, QMouseEvent, QCursorfrom PySide2.QtCore import QFile, QObject, SIGNALimport cv2import numpy as npimport mathclass Display_Pixels(QGraphicsView):    def __init__(self, parent=None):        QGraphicsView.__init__(self, parent=parent)        #super().__init__()        self.initUI()        self.img = cv2.imread('roi.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)
查看完整描述

1 回答

?
30秒到達(dá)戰(zhàn)場

TA貢獻(xiàn)1828條經(jīng)驗 獲得超6個贊

我認(rèn)為您的意思是您成功將此代碼添加到主窗口,但您希望能夠?qū)⑵涮砑拥侥谠O(shè)計器中制作的小部件中,而不是嚴(yán)格意義上的主窗口。


如果是這種情況,您想要做的是在 Qt Designer 中按照您想要的方式格式化您的小部件,在您想要插入 Display_Pixels QGraphicsView 對象的位置添加一個占位符,例如框架、布局、可能是一個組等。


之后不會直接將其添加到框架中,而是將該框架使用的任何布局。所以如果你的框架中有一個網(wǎng)格布局,它會是這樣的:


    self.Disp_pixel = Display_Pixels()

    widget_name.gridlayout.addWidget(self.Disp_pixel)

很難告訴您這段代碼將在您的代碼中的何處實現(xiàn),但假設(shè)您運行了 pyuic5 并獲得了 python 輸出。您可以將它添加到該輸出 python 文件中。我建議使用類似查看器類的東西,它繼承生成的輸出類并在其中執(zhí)行添加小部件。此外,frame 將是您為圖形視圖創(chuàng)建的框架的名稱,而不是字面上的框架。


此外,我不確定您是否可以在任何類型的小部件上進(jìn)行展示,我很確定小部件必須是可顯示的,例如主窗口小部件或?qū)υ捒蛐〔考?。也就是說小部件可以放置在其他小部件中。


這是我給你的一個例子,它有點粗糙,但我認(rèn)為它會有所幫助:


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_())



查看完整回答
反對 回復(fù) 2021-11-09
  • 1 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號