font = QtGui.QFont() font.setPointSize(12) self.ui.textEdit.setFont(font)用pyside2如何改
时间: 2024-03-23 08:40:34 浏览: 108
在PySide2中,您可以使用以下代码来设置文本编辑器的字体大小:
```
font = QtGui.QFont()
font.setPointSize(12)
self.ui.textEdit.setFont(font)
```
请注意,PySide2的QtGui模块与PyQt5的QtGui模块具有相同的API,因此这些代码应该可以在两个库中通用。
相关问题
import FreeCAD import FreeCADGui from PySide import QtGui, QtCore Gui.activateWorkbench("PartWorkbench") FreeCAD.newDocument() class SphereDialog(QtGui.QDialog): def __init__(self): super().__init__() self.setWindowTitle("Create Sphere") self.create_widgets() def create_widgets(self): layout = QtGui.QVBoxLayout() # Radius widget radius_label = QtGui.QLabel("Radius:") self.radius_spinbox = QtGui.QDoubleSpinBox() self.radius_spinbox.setMinimum(0.1) self.radius_spinbox.setMaximum(1000.0) self.radius_spinbox.setValue(10.0) layout.addWidget(radius_label) layout.addWidget(self.radius_spinbox) # Opacity widget opacity_label = QtGui.QLabel("Opacity:") self.opacity_slider = QtGui.QSlider(QtCore.Qt.Horizontal) self.opacity_slider.setMinimum(0) self.opacity_slider.setMaximum(100) self.opacity_slider.setValue(50) layout.addWidget(opacity_label) layout.addWidget(self.opacity_slider) # Create button create_button = QtGui.QPushButton("Create") create_button.clicked.connect(self.create_sphere) layout.addWidget(create_button) self.setLayout(layout) def create_sphere(self): # Get sphere parameters radius = self.radius_spinbox.value() opacity = float(self.opacity_slider.value() / 100) # Create sphere sphere = FreeCAD.ActiveDocument.addObject("Part::Sphere", "Sphere") sphere.Radius = radius sphere.ViewObject.Transparency = int(round(opacity * 100)) # Show in 3D view sphere.ViewObject.Visibility = True FreeCADGui.SendMsgToActiveView("ViewFit") FreeCADGui.Selection.clearSelection() FreeCADGui.Selection.addSelection(sphere) FreeCADGui.SendMsgToActiveView("ViewFit") # Close dialog self.close() dialog = SphereDialog() dialog.show()代码中执行没有球体出现,请修改代码
根据您提供的代码,我发现在 `create_sphere` 函数中,创建球体后没有将其添加到文档中。修改代码如下:
```python
import FreeCAD
import FreeCADGui
from PySide import QtGui, QtCore
Gui.activateWorkbench("PartWorkbench")
FreeCAD.newDocument()
class SphereDialog(QtGui.QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Create Sphere")
self.create_widgets()
def create_widgets(self):
layout = QtGui.QVBoxLayout()
# Radius widget
radius_label = QtGui.QLabel("Radius:")
self.radius_spinbox = QtGui.QDoubleSpinBox()
self.radius_spinbox.setMinimum(0.1)
self.radius_spinbox.setMaximum(1000.0)
self.radius_spinbox.setValue(10.0)
layout.addWidget(radius_label)
layout.addWidget(self.radius_spinbox)
# Opacity widget
opacity_label = QtGui.QLabel("Opacity:")
self.opacity_slider = QtGui.QSlider(QtCore.Qt.Horizontal)
self.opacity_slider.setMinimum(0)
self.opacity_slider.setMaximum(100)
self.opacity_slider.setValue(50)
layout.addWidget(opacity_label)
layout.addWidget(self.opacity_slider)
# Create button
create_button = QtGui.QPushButton("Create")
create_button.clicked.connect(self.create_sphere)
layout.addWidget(create_button)
self.setLayout(layout)
def create_sphere(self):
# Get sphere parameters
radius = self.radius_spinbox.value()
opacity = float(self.opacity_slider.value() / 100)
# Create sphere
sphere = FreeCAD.ActiveDocument.addObject("Part::Sphere", "Sphere")
sphere.Radius = radius
sphere.ViewObject.Transparency = int(round(opacity * 100))
FreeCAD.ActiveDocument.recompute() # 添加这行代码,重新计算文档
# Show in 3D view
sphere.ViewObject.Visibility = True
FreeCADGui.SendMsgToActiveView("ViewFit")
FreeCADGui.Selection.clearSelection()
FreeCADGui.Selection.addSelection(sphere)
FreeCADGui.SendMsgToActiveView("ViewFit")
# Close dialog
self.close()
dialog = SphereDialog()
dialog.show()
```
在 `create_sphere` 函数中,我添加了 `FreeCAD.ActiveDocument.recompute()`,这会重新计算文档,使得添加的球体能够在 3D 视图中显示。
from PyQt5 import QtCore, QtGui, QtWidgets from show1 import Ui_Form1 from show2 import Ui_Form2 from show3 import Ui_Form3 class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(90, 60, 191, 51)) font = QtGui.QFont() font.setPointSize(9) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Form) self.pushButton_2.setGeometry(QtCore.QRect(90, 110, 191, 51)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_3 = QtWidgets.QPushButton(Form) self.pushButton_3.setGeometry(QtCore.QRect(90, 160, 191, 51)) self.pushButton_3.setObjectName("pushButton_3") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "无人机群显示")) self.pushButton_2.setText(_translate("Form", "无人机群数据分析展示")) self.pushButton_3.setText(_translate("Form", "无人机群飞行轨迹展示")) class MainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.ui = Ui_Form() self.ui.setupUi(self) self.ui.pushButton.clicked.connect(self.showForm1) self.ui.pushButton_2.clicked.connect(self.showForm2) self.ui.pushButton_3.clicked.connect(self.showForm3) def showForm1(self): self.form1 = QtWidgets.QWidget() self.ui1 = Ui_Form1() self.ui1.setupUi(self.form1) self.form1.show() def showForm2(self): self.form2 = QtWidgets.QWidget() self.ui2 = Ui_Form2() self.ui2.setupUi(self.form2) self.form2.show() def showForm3(self): self.form3 = QtWidgets.QWidget() self.ui3 = Ui
_Form3()
self.ui3.setupUi(self.form3)
self.form3.show()
这段代码是一个基于PyQt5的GUI程序,使用了三个不同的UI文件(show1.py、show2.py、show3.py)来展示不同的功能界面。在UI类中,setupUi()方法用于设置UI的各个元素,retranslateUi()方法用于设置UI的翻译文本。在MainWindow类中,通过连接按钮的clicked信号,来分别展示不同的UI界面。showForm1()、showForm2()、showForm3()方法分别用于展示不同的UI界面,其中每个界面都有对应的UI类来设置UI元素。
阅读全文