簡體   English   中英

當 qcombobox 索引更改覆蓋 QUiloader 時,PySide2 在小部件上重新繪制

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

在 Win10 和 Python 3.6.6 上使用 VS Code。

此鏈接提供了在我正在使用的提升小部件上創建圖形的相同方法:

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

我希望能夠根據在 qcombobox 中選擇的形狀創建一些其他圖形。 我遇到的兩個問題是:

  1. 使用函數/方法將 combobox 中的索引或字符串值返回到我的抽屜 class。 這種方法https://stackoverflow.com/a/56560455/6284847展示了如何從 class 打印索引/字符串值,但我無法在函數/方法中返回 qcombobox 索引/字符串值另一個 class 中的函數/方法。 我已經讀到不可能在不同的 SO 主題中返回這些值,但必須有一種方法來實現這一點。

  2. 除了應用程序最初運行時,我真的不知道如何重新繪制小部件。

任何其他重組這些東西的方法也可以完成這一點,非常感謝!

測試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>

油漆事件測試.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

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

編輯1:

我試圖從我的 qcombobox 中獲取我的索引或值,但我無法實現它。 我更新了 myDrawWidget.py 並評論了我認為我必須實現代碼以達到我的目標,即在激活 qcombobox 時繪制不同的形狀顏色。

所以這個問題很容易解決。 首先,我們必須連接到 Drawer.py 中的returnComboBoxIndex ,然后將已經創建的 class 變量Drawer.index分配給返回的索引。

油漆事件測試.py

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

myDrawWidget.py

改變這個

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

對此:

    @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