赞
踩
**
**
def initUI(self): layout = QVBoxLayout() self.button1 = QPushButton("加载图片") self.button1.clicked.connect(self.loadImage) layout.addWidget(self.button1) self.imageLabel = QLabel() layout.addWidget(self.imageLabel) self.button2 = QPushButton("加载文本文件") self. button2.clicked.connect(self.loadText) layout.addWidget(self.button2) self.contents = QTextEdit() layout.addWidget(self.contents) self.setLayout(layout) self.setWindowTitle("文件对话框") def loadImage(self): fname,_ = QFileDialog.getOpenFileName(self,"打开文件","."," 图像文件(*.jpg *.png)") self.imageLabel.setPixmap((QPixmap(fname))) def loadText(self): dialog = QFileDialog() dialog.setFileMode(QFileDialog.AnyFile) dialog.setFilter(QDir.Files)
class MyComboBox(QComboBox): #继承别人的控件 def __init__(self): super(MyComboBox,self).__init__() self.setAcceptDrops(True) def dragEnterEvent(self, e): print(e) if e.mimeData().hasText(): e.accept() else: e.ignore() def dropEvent(self, e): self.addItem(e.mimeData().text()) class DrapDropDemo(QWidget): def __init__(self): super(DrapDropDemo,self).__init__() formLayout = QFormLayout() formLayout.addRow(QLabel("请将文本拖动到下拉列表中")) lineEdit = QLineEdit() lineEdit.setDragEnabled(True) #QlineEdit是可拖动的 combo = MyComboBox() formLayout.addRow(lineEdit,combo) self.setLayout(formLayout) self.setWindowTitle("拖拽")
def paintEvent(self, event): qp = QPainter() qp.begin(self) qp.setPen(Qt.blue) #绘制弧 rect = QRect(0,10,100,100) #alen:1个alen等于1/16度 45*16(表示45度的弧) qp.drawArc(rect,0,90*16) #通过弧绘制圆 qp.setPen(Qt.red) qp.drawArc(120,10,100,100,0,360*16) #带弦的弧 qp.setPen(Qt.green) qp.drawChord(0,120,100,100,12,130*16 ) #绘制扇形 qp.setPen(Qt.darkGray) qp.drawPie(10,240,100,100,12,130*16) #绘制椭圆 qp.setPen(Qt.gray) qp.drawEllipse(120,120,150,100) #绘制5边形 point1 = QPoint(250,300) point2 = QPoint(320,350) point3 = QPoint(430,380) point4 = QPoint(400,500) point5 = QPoint(100,533) polygon = QPolygon([point1,point2,point3,point4,point5]) qp.drawPolygon(polygon) qp.end()
class DrawPoints(QWidget): def __init__(self): super(DrawPoints,self).__init__() self.resize(300,300) self.setWindowTitle("绘制两个周期的正弦波") def paintEvent(self,event): painter = QPainter() painter.begin(self) painter.setPen(Qt.blue) size = self.size() for i in range(1000): x = 100 * (-1+2.0*i/1000 )+size.width()/2.0 y = -50* math.sin((x-size.width()/2.0)* math.pi/50) +size.height()/2.0 painter.drawPoint(x,y) painter.end()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。