在Win10和Python 3.6.6上使用VS Code。
该链接提供了与我正在使用的提升的小部件上创建图形相同的方法:
https://stackoverflow.com/a/56492312/6284847
我希望能够根据在qcombobox中选择的形状来创建一些其他图形。我遇到的两个问题是:
从带有功能/方法的组合框返回索引或字符串值到我的Drawer类。这种方法https://stackoverflow.com/a/56560455/6284847展示了如何从类中打印索引/字符串值,但是我无法在函数/方法中返回qcombobox索引/字符串值并使用另一个类中的函数/方法。我读过,不可能在不同的SO主题中返回这些值,但是必须有一种方法来实现。
除了当应用程序最初运行时,我真的没有一个关于如何重新绘制小部件的想法。
重组这些东西的任何其他方法也可以做到这一点,我们深表感谢!
testUI.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
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时绘制不同的形状颜色。
因此,此问题很容易解决。首先,我们必须连接到returnComboBoxIndex
Drawer.py中,然后将Drawer.index
已经完成的类变量分配给返回的索引。
paintEventTest.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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句