QT实现一个双向进度条窗口的代码
时间: 2024-03-01 17:53:22 浏览: 56
用qt编写的 进度条
5星 · 资源好评率100%
好的,以下是一个简单的QT程序,它实现了一个双向进度条窗口:
mainwindow.h文件:
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProgressBar>
#include <QTimer>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
QProgressBar *leftProgressBar;
QProgressBar *rightProgressBar;
QTimer *leftTimer;
QTimer *rightTimer;
int leftValue;
int rightValue;
private slots:
void leftTimerUpdate();
void rightTimerUpdate();
void startProgress();
void stopProgress();
};
#endif // MAINWINDOW_H
```
mainwindow.cpp文件:
```cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setFixedSize(300, 150);
leftProgressBar = new QProgressBar(this);
leftProgressBar->setGeometry(20, 40, 100, 20);
leftProgressBar->setRange(0, 100);
leftProgressBar->setValue(0);
leftTimer = new QTimer(this);
connect(leftTimer, SIGNAL(timeout()), this, SLOT(leftTimerUpdate()));
leftValue = 0;
rightProgressBar = new QProgressBar(this);
rightProgressBar->setGeometry(180, 40, 100, 20);
rightProgressBar->setRange(0, 100);
rightProgressBar->setValue(100);
rightTimer = new QTimer(this);
connect(rightTimer, SIGNAL(timeout()), this, SLOT(rightTimerUpdate()));
rightValue = 100;
QPushButton *startButton = new QPushButton("Start", this);
startButton->setGeometry(50, 90, 75, 25);
connect(startButton, SIGNAL(clicked()), this, SLOT(startProgress()));
QPushButton *stopButton = new QPushButton("Stop", this);
stopButton->setGeometry(175, 90, 75, 25);
connect(stopButton, SIGNAL(clicked()), this, SLOT(stopProgress()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::leftTimerUpdate()
{
if (leftValue == 100) {
leftValue = 0;
}
leftProgressBar->setValue(leftValue++);
}
void MainWindow::rightTimerUpdate()
{
if (rightValue == 0) {
rightValue = 100;
}
rightProgressBar->setValue(rightValue--);
}
void MainWindow::startProgress()
{
leftTimer->start(50);
rightTimer->start(50);
}
void MainWindow::stopProgress()
{
leftTimer->stop();
rightTimer->stop();
}
```
这个程序创建了两个进度条控件,分别用于显示从左到右和从右到左的进度。每个进度条控件都有一个QTimer对象,用于在定时器超时时更新控件的值。启动和停止进度条的按钮分别连接到槽函数,用于启动和停止定时器。
阅读全文