def QTimeToMsecs(time: QTime): msecs = 0 msecs += time.msec() msecs += time.second() * 1000 msecs += time.minute() * 1000 * 60 msecs += time.hour() * 1000 * 60 * 60 return msecs
class StopwatchWidget(QWidget, ClockAssets): # widget containing stopwatch and all methods associated with it def __init__(self): super().__init__() self.window_icon = "timer_assets/timer.png" self.play_icon = "timer_assets/play.png" self.pause_icon = "timer_assets/pause.png" self.reset_icon = "timer_assets/reset.png" self.stopwatch_time = QTime(0, 0, 0, 0) self.stopwatch_is_running = False self.stopwatch = QTimer() self.stopwatch.timeout.connect(self.update_stopwatch) self.init_window() def init_window(self): self.stopwatch_vbox = self.create_stopwatch_window() self.setLayout(self.stopwatch_vbox) # STOPWATCH def create_stopwatch_window(self): self.stopwatch_time_label = QLabel( self.stopwatch_time.toString("hh:mm:ss.zz")) self.stopwatch_time_label.setAlignment(Qt.AlignCenter) self.stopwatch_time_label.setFont(QFont("Arial", 20)) self.reset_stopwatch_button = QPushButton() self.reset_stopwatch_button.setIcon(QIcon(self.reset_icon)) self.reset_stopwatch_button.setFixedWidth(200) self.reset_stopwatch_button.clicked.connect(self.reset_stopwatch) self.toggle_stopwatch_button = QPushButton() self.toggle_stopwatch_button.setIcon(QIcon(self.play_icon)) self.toggle_stopwatch_button.setFixedWidth(200) self.toggle_stopwatch_button.clicked.connect(self.toggle_stopwatch) self.vbox = QVBoxLayout() self.vbox.addWidget(self.stopwatch_time_label) self.vbox.addWidget(self.reset_stopwatch_button, alignment=Qt.AlignHCenter) self.vbox.addWidget(self.toggle_stopwatch_button, alignment=Qt.AlignHCenter) self.vbox.addSpacing(20) return self.vbox def display_stopwatch_time(self): # msec displays the hundreds and tens place only msec = str(int(self.stopwatch_time.msec() / 10)) self.stopwatch_time_label.setText( self.stopwatch_time.toString(f"hh:mm:ss.{msec.ljust(2, '0')}")) def toggle_stopwatch(self): # starts stopwatch if it's not running or # stops stopwatch if it is running if self.stopwatch_is_running: self.stopwatch_is_running = False self.change_stopwatch_button_logo("play") self.stop_stopwatch() else: self.stopwatch_is_running = True self.change_stopwatch_button_logo("pause") self.start_stopwatch() # EFFECT def change_stopwatch_button_logo(self, action): # changes button logo if action == "play": self.toggle_stopwatch_button.setIcon(QIcon(self.play_icon)) elif action == "pause": self.toggle_stopwatch_button.setIcon(QIcon(self.pause_icon)) def start_stopwatch(self): # runs stopwatch self.stopwatch.start(10) def stop_stopwatch(self): # stops stopwatch self.stopwatch.stop() def update_stopwatch(self): # stopwatch's interval is every 10 ms so only the hundreds and tens digits are shown self.stopwatch_time = self.stopwatch_time.addMSecs(10) self.display_stopwatch_time() def reset_stopwatch(self): # resets stopwatch time to zero self.stopwatch_time = QTime(0, 0, 0, 0) self.display_stopwatch_time()
def qTime2float(self, qTime: QTime) -> float: return qTime.hour() * 3600 + qTime.minute() * 60 + qTime.second( ) + qTime.msec() / 1000
class DynamicWaveView(QChartView): def __init__(self, parent=None): super(DynamicWaveView, self).__init__(parent) self.time = QTime() self.x = 0 # 横坐标值 self.ymax = 0.0 # 纵坐标最大值 self.ymin = 0.0 # 纵坐标最小值 self.lines = dict() self.chart().legend().show() # self.chart().addSeries(self.line) # self.setChart(QChart()) # self.chart().setTitle("波形") # self.chart().createDefaultAxes() self.ax = QValueAxis() self.ax.setTitleText("t") self.ax.setRange(0, 10) # self.ay = self.chart.axisY() self.ay = QValueAxis() self.ay.setRange(-1, 1) self.ay.setTitleText("value") self.chart().addAxis(self.ax, Qt.AlignBottom) self.chart().addAxis(self.ay, Qt.AlignLeft) # self.chart().setAnimationOptions(QChart.SeriesAnimations) # 动画 # self.setRenderHint(QPainter.Antialiasing) # 消除混叠现象,消除走样,图形保真; def add_new_data(self, data: dict): self.x += self.time.msec() self.time.start() if self.x > 10000: self.ax.setRange(self.x - 10000, self.x) for key in data: try: value = float(data[key]) except ValueError: continue if value > self.ymax: self.ymax = value self.ay.setRange(self.ymin, self.ymax) elif value < self.ymin: self.ymin = value self.ay.setRange(self.ymin, self.ymax) if key in self.lines: if self.lines[key].__len__() >= 32: # self.lines[key].replace(0,self.x, value) self.lines[key].remove(0) # else: self.lines[key].append(self.x, value) else: # ls = QSplineSeries(self) ls = QLineSeries(self) ls.append(self.x, value) ls.setColor( QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) ls.setName(key) self.chart().addSeries(ls) self.chart().setAxisX(self.ax, ls) self.chart().setAxisY(self.ay, ls) self.lines[key] = ls def clear(self): self.ymax = 0 self.ymin = 0 self.lines.clear()