本文通过Python3+PyQt5实现自定义部件–分数滑块。它既能支持键盘也支持鼠标,使用物理(视口)坐标通过绘制方式显示。
#!/usr/bin/env python3 import platform from PyQt5.QtCore import (QPointF, QRectF, QSize, Qt,pyqtSignal) from PyQt5.QtWidgets import (QApplication, QDialog,QSizePolicy, QGridLayout, QLCDNumber, QLabel, QSpinBox, QWidget) from PyQt5.QtGui import QColor,QFont,QPainter,QFontMetricsF,QPalette, QPolygonF X11 = True try: from PyQt5.QtGui import qt_x11_wait_for_window_manager except ImportError: X11 = False class FractionSlider(QWidget): XMARGIN = 12.0 YMARGIN = 5.0 WSTRING = "999" valueChanged = pyqtSignal(int,int) def __init__(self, numerator=0, denominator=10, parent=None): super(FractionSlider, self).__init__(parent) self.__numerator = numerator self.__denominator = denominator self.setFocusPolicy(Qt.WheelFocus) self.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)) def decimal(self): return self.__numerator / float(self.__denominator) def fraction(self): return self.__numerator, self.__denominator def sizeHint(self): return self.minimumSizeHint() def minimumSizeHint(self): font = QFont(self.font()) font.setPointSize(font.pointSize() - 1) fm = QFontMetricsF(font) return QSize(fm.width(FractionSlider.WSTRING) * self.__denominator, (fm.height() * 4) + FractionSlider.YMARGIN) def setFraction(self, numerator, denominator=None): if denominator is not None: if 3 <= denominator <= 60: self.__denominator = denominator else: raise ValueError("denominator out of range") if 0 <= numerator <= self.__denominator: self.__numerator = numerator else: raise ValueError("numerator out of range") self.update() self.updateGeometry() def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.moveSlider(event.x()) event.accept() else: QWidget.mousePressEvent(self, event) def mouseMoveEvent(self, event): self.moveSlider(event.x()) def moveSlider(self, x): span = self.width() - (FractionSlider.XMARGIN * 2) offset = span - x + FractionSlider.XMARGIN numerator = int(round(self.__denominator * (1.0 - (offset / span)))) numerator = max(0, min(numerator, self.__denominator)) if numerator != self.__numerator: self.__numerator = numerator #self.emit(SIGNAL("valueChanged(int,int)"), # self.__numerator, self.__denominator) self.valueChanged.emit(self.__numerator, self.__denominator) self.update() def keyPressEvent(self, event): change = 0 if event.key() == Qt.Key_Home: change = -self.__denominator elif event.key() in (Qt.Key_Up, Qt.Key_Right): change = 1 elif event.key() == Qt.Key_PageUp: change = (self.__denominator // 10) + 1 elif event.key() in (Qt.Key_Down, Qt.Key_Left): change = -1 elif event.key() == Qt.Key_PageDown: change = -((self.__denominator // 10) + 1) elif event.key() == Qt.Key_End: change = self.__denominator if change: numerator = self.__numerator numerator += change numerator = max(0, min(numerator, self.__denominator)) if numerator != self.__numerator: self.__numerator = numerator #self.emit(SIGNAL("valueChanged(int,int)"), # self.__numerator, self.__denominator) self.valueChanged.emit(self.__numerator, self.__denominator) self.update() event.accept() else: QWidget.keyPressEvent(self, event) def paintEvent(self, event=None): font = QFont(self.font()) font.setPointSize(font.pointSize() - 1) fm = QFontMetricsF(font) fracWidth = fm.width(FractionSlider.WSTRING) indent = fm.boundingRect("9").width() / 2.0 if not X11: fracWidth *= 1.5 span = self.width() - (FractionSlider.XMARGIN * 2) value = self.__numerator / float(self.__denominator) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setRenderHint(QPainter.TextAntialiasing) painter.setPen(self.palette().color(QPalette.Mid)) painter.setBrush(self.palette().brush( QPalette.AlternateBase)) painter.drawRect(self.rect()) segColor = QColor(Qt.green).darker(120) segLineColor = segColor.darker() painter.setPen(segLineColor) painter.setBrush(segColor) painter.drawRect(FractionSlider.XMARGIN, FractionSlider.YMARGIN, span, fm.height()) textColor = self.palette().color(QPalette.Text) segWidth = span / self.__denominator segHeight = fm.height() * 2 nRect = fm.boundingRect(FractionSlider.WSTRING) x = FractionSlider.XMARGIN yOffset = segHeight + fm.height() for i in range(self.__denominator + 1): painter.setPen(segLineColor) painter.drawLine(x, FractionSlider.YMARGIN, x, segHeight) painter.setPen(textColor) y = segHeight rect = QRectF(nRect) rect.moveCenter(QPointF(x, y + fm.height() / 2.0)) #painter.drawText(rect, Qt.AlignCenter, #QString.number(i)) painter.drawText(rect, Qt.AlignCenter,str(i)) y = yOffset rect.moveCenter(QPointF(x, y + fm.height() / 2.0)) painter.drawText(rect, Qt.AlignCenter, str(self.__denominator)) painter.drawLine(QPointF(rect.left() + indent, y), QPointF(rect.right() - indent, y)) x += segWidth span = int(span) y = FractionSlider.YMARGIN - 0.5 triangle = [QPointF(value * span, y), QPointF((value * span) + (2 * FractionSlider.XMARGIN), y), QPointF((value * span) + FractionSlider.XMARGIN, fm.height())] painter.setPen(Qt.yellow) painter.setBrush(Qt.darkYellow) painter.drawPolygon(QPolygonF(triangle)) if __name__ == "__main__": import sys app = QApplication(sys.argv) form = QDialog() sliderLabel = QLabel("&Fraction") slider = FractionSlider(denominator=12) sliderLabel.setBuddy(slider) denominatorLabel = QLabel("&Denominator") denominatorSpinBox = QSpinBox() denominatorLabel.setBuddy(denominatorSpinBox) denominatorSpinBox.setRange(3, 60) denominatorSpinBox.setValue(slider.fraction()[1]) denominatorSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter) numeratorLabel = QLabel("Numerator") numeratorLCD = QLCDNumber() numeratorLCD.setSegmentStyle(QLCDNumber.Flat) layout = QGridLayout() layout.addWidget(sliderLabel, 0, 0) layout.addWidget(slider, 0, 1, 1, 5) layout.addWidget(numeratorLabel, 1, 0) layout.addWidget(numeratorLCD, 1, 1) layout.addWidget(denominatorLabel, 1, 2) layout.addWidget(denominatorSpinBox, 1, 3) form.setLayout(layout) def valueChanged(denominator): numerator = int(slider.decimal() * denominator) slider.setFraction(numerator, denominator) numeratorLCD.display(numerator) #form.connect(slider, SIGNAL("valueChanged(int,int)"), #numeratorLCD, SLOT("display(int)")) slider.valueChanged[int,int].connect(numeratorLCD.display) #form.connect(denominatorSpinBox, SIGNAL("valueChanged(int)"), #valueChanged) denominatorSpinBox.valueChanged[int].connect(valueChanged) form.setWindowTitle("Fraction Slider") form.show() app.exec_()
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]