简体   繁体   English

当 qcombobox 索引更改覆盖 QUiloader 时,PySide2 在小部件上重新绘制

[英]PySide2 repaint on widget when qcombobox index changes overriding QUiloader

Using VS Code on Win10 and Python 3.6.6.在 Win10 和 Python 3.6.6 上使用 VS Code。

This link provides the same approach for creating graphics on a promoted widget which I'm using:此链接提供了在我正在使用的提升小部件上创建图形的相同方法:

https://stackoverflow.com/a/56492312/6284847 https://stackoverflow.com/a/56492312/6284847

I want to be able to create some other graphics based on what shape is selected in the qcombobox.我希望能够根据在 qcombobox 中选择的形状创建一些其他图形。 The two issues I got with this is:我遇到的两个问题是:

  1. returning the index or string value from combobox with a function/method to my Drawer class.使用函数/方法将 combobox 中的索引或字符串值返回到我的抽屉 class。 This approach, https://stackoverflow.com/a/56560455/6284847 , shows how to print the index/string value from a class, but I'm not able to return qcombobox index/string value in a function/method and use the function/method in another class.这种方法https://stackoverflow.com/a/56560455/6284847展示了如何从 class 打印索引/字符串值,但我无法在函数/方法中返回 qcombobox 索引/字符串值另一个 class 中的函数/方法。 I've read that it's not possible to return these values in different SO topics but there have to be a way to accomplish this.我已经读到不可能在不同的 SO 主题中返回这些值,但必须有一种方法来实现这一点。

  2. I don't really have an idea of how to repaint the widget other than when the application run initially.除了应用程序最初运行时,我真的不知道如何重新绘制小部件。

Any other way to restructure these things too accomplish this is much appreciated!任何其他重组这些东西的方法也可以完成这一点,非常感谢!

testUI.ui测试UI.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>996</width>
    <height>892</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QGraphicsView" name="graphicsView">
      <property name="minimumSize">
       <size>
        <width>0</width>
        <height>200</height>
       </size>
      </property>
     </widget>
    </item>
    <item>
     <widget class="Drawer" name="widget" native="true">
      <property name="minimumSize">
       <size>
        <width>0</width>
        <height>250</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
        <width>16777215</width>
        <height>300</height>
       </size>
      </property>
      <property name="styleSheet">
       <string notr="true"/>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QComboBox" name="comboBox"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>996</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>Drawer</class>
   <extends>QWidget</extends>
   <header>myDrawWidget</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

paintEventTest.py油漆事件测试.py

import sys

from PySide2 import QtWidgets 
from PySide2 import QtGui
from PySide2 import QtCore
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import (
    QApplication, QPushButton, QLineEdit, QTextEdit, QSpinBox, QMainWindow, QDesktopWidget, QTableWidget, 
    QTableWidgetItem, QToolButton, QToolTip)
from PySide2.QtCore import QFile, QObject, Qt

from  myDrawWidget import Drawer

class UiLoader(QUiLoader):
    def createWidget(self, className, parent=None, name=""):
        if className == "Drawer":
            widget = Drawer(parent)
            widget.setObjectName(name)
            return widget
        return super(UiLoader, self).createWidget(className, parent, name)

class MainForm(QMainWindow):
    def __init__(self, ui_file, parent=None):
        super(MainForm, self).__init__(parent)
        ui_file = QtCore.QFile(ui_file)
        ui_file.open(QtCore.QFile.ReadOnly)

        ### Load UI file from Designer ###
        loader = UiLoader()
        self.ui_window = loader.load(ui_file)
        ui_file.close()
        self.initUI()

        self.ui_window.show()



    def initUI(self):

        #region widget code
        widget = self.ui_window.widget
        widget.setStyleSheet("""
            QWidget {
                border: 1px solid lightgrey;
                border-radius: 2px;
                background-color: rgb(255, 255, 255);
                }
            """)

        #endregion

        sectionList = []
        sectionList.append("Rectangle")
        sectionList.append("Diamond")
        sectionList.append("Circle")
        sectionList.append("Box")
        sectionList.append("T-Section")
        sectionList.append("I-Section")

        ComboBox = self.ui_window.comboBox
        #comboBox = QtWidgets.QComboBox      #Just to get intellisense working. Gets commented out
        ComboBox.setCurrentIndex(0)

        for item in sectionList:
            ComboBox.addItem(item)

        #ComboEvent = comboBoxEvent(ComboBox)
        ComboBox.currentIndexChanged.connect(self.cbEvent)
        # #print(ComboBox.itemText(ComboBox.currentIndex()))

    def cbEvent(self, index):
        text = self.ui_window.comboBox.itemText(index)
        print(str(text))    


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    form = MainForm('./UI_designer/testUI.ui')
    sys.exit(app.exec_())

myDrawWidget.py myDrawWidget.py

class Drawer(QtWidgets.QWidget):

    index = None

    def paintEvent(self, e):
        '''
        the method paintEvent() is called automatically
        the QPainter class does all the low-level drawing
        coded between its methods begin() and end()
        '''
        index = Drawer.index

        ##############################################
        # IMPLEMENT CODE TO GET INDEX FROM QCOMBOBOX
        # HOW TO?
        ##############################################



        ### Applying QSS style on the widget ###
        opt = QtWidgets.QStyleOption()
        opt.init(self)

        qp = QtGui.QPainter()
        qp.begin(self)
        self.style().drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, qp, self)

        if index == None:
            self.init_drawGeometry(qp)
        elif cb_index == 0:

            self.drawGeometry(qp)
        else:
            self.drawGeometry_two(qp)

        qp.end()


    def init_drawGeometry(self, qp):
        qp = QtGui.QPainter(self)
        qp.setPen(QtGui.QPen(QtCore.Qt.red, 8, QtCore.Qt.DashLine))
        qp.drawEllipse(40, 40, 400, 400)

    def drawGeometry(self, qp):
        qp = QtGui.QPainter(self)
        qp.setPen(QtGui.QPen(QtCore.Qt.green, 8, QtCore.Qt.DashLine))
        qp.drawEllipse(40, 40, 400, 400)

    def drawGeometry_two(self, qp):
        qp = QtGui.QPainter(self)
        qp.setPen(QtGui.QPen(QtCore.Qt.blue, 8, QtCore.Qt.DashLine))
        qp.drawEllipse(40, 40, 400, 400)

    @QtCore.Slot()
    def returnComboBoxIndex(self, index):
        print(index)
        return index

EDIT 1:编辑1:

I've tried to get my index or value from my qcombobox but I'm not able to achieve it.我试图从我的 qcombobox 中获取我的索引或值,但我无法实现它。 I've updated myDrawWidget.py and commented where I think I have to implement code to reach my goal which is to draw different shape colours when qcombobox is activated.我更新了 myDrawWidget.py 并评论了我认为我必须实现代码以达到我的目标,即在激活 qcombobox 时绘制不同的形状颜色。

So it was an easy fix for this issue.所以这个问题很容易解决。 First we have to connect to the returnComboBoxIndex in Drawer.py and then assign the class variable Drawer.index , which was already made, to the returned index.首先,我们必须连接到 Drawer.py 中的returnComboBoxIndex ,然后将已经创建的 class 变量Drawer.index分配给返回的索引。

paintEventTest.py油漆事件测试.py

    cb_event = Drawer(self)
    ComboBox.activated[int].connect(cb_event.returnComboBoxIndex)

myDrawWidget.py myDrawWidget.py

Changing this改变这个

    @QtCore.Slot()
    def returnComboBoxIndex(self, index):
        print(index)
        return index

to this:对此:

    @QtCore.Slot()
    def returnComboBoxIndex(self, index):
    Drawer.index = index    
    print(index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM