如何用C语言实现进度条
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用C语言实现进度条相关的知识,希望对你有一定的参考价值。
我学校课程设计用,在网上找了半天没有哪位哥哥能给做出来帮忙传下,只能用C做哦,C++都不可以
最好是原代码,我对C不是很明白,麻烦了
有高手没啊?
第一种,用printf模拟输出百分比,在控制台输出,实现进度监控。
代码:
#include<stdio.h>#define N 20
void mBar(float fBar)
int i,j,k,m;
for ( i=0;i<N+6;i++ ) printf("\\b");
k=N*fBar; m=fBar*100; printf("[");
for ( i=0;i<k;i++ ) printf("=");
for ( j=0;j<N-k;j++ ) printf(" ");
printf("]%3d%%",m);
void delay(int n) int i,j,k; for ( i=0;i<n;i++ ) for ( j=0;j<0x07fff;j++ ) k++;
void main()
float x;
int i;
for ( i=0,x=0;i<100;i++,x+=0.01 ) mBar(x); delay(1024);
mBar(1);
第二种,使用窗口组件。这个要看程序工作环境,纯粹的c语言是无法实现的。可以使VC++。Visual C++中的MFC类提供了标准的进度指示器控件(CProgressCtrl)。
m_progress->GetPos(); //获取进度条的当前位置
m_progress->GetRange(int min,int max); //获取进度条控件的范围的下限和上限
m_progress->OffsetPos(int nPos); //用指定的增量推进进度条控件的当前位置,重绘进度条反映新位置
m_progress->SetBkColor(COLORREF clrNew); //设定进度条的背景颜色
m_progress->SerPos(int nPos); //设定进度条控件的当前位置,重绘进度条反映新位置
m_progress->SetRange(int min,int max); //设定进度条控件的范围的下限和上限
m_progress->SetRange32(int min,int max); //设定进度条控件的范围的下限和上限
m_progress->SetStep(int nStep); //指定进度条控件的步进增量
m_progress->StepIt(); //通过步进增量,推进进度条控件的当前位置,重绘进度条反映新位置
应用:
CProgressCtrl *m_progress; //头文件中声明
在OnInitDialog初始化
m_progress = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS1);
m_progress->SetRange(0,1000);
m_progress->SetPos(0);
UINT m_timer =(UINT) SetTimer(1,200,NULL);
采用在定时器的消息处理函数WM_TIMER内添加不断更新进度条界面的方法
OnTimer (nIDEvent)
pos = pos + 50;
if(pos>500)
pos = 0;
m_Progress->SetPos(pos);
或者采用在某种循环体内添加不断更新进展条界面的方法
相关消息
PBM_GETPOS 得到当前位置
PBM_SETPOS 设置当前位置并重画,指向这个位置
PBM_GETRANG 得到进度条范围
PBM_SETRANG 设置进度条范围
PBM_SETRANG32 设置进度条范围
PBM_SETSTEP 设置步进长度
PBM_STEPIT 执行步进
详细内容得看MSDN.
c 语言的话基本就是WIN32 SDK了,翻MSDN吧本回答被提问者采纳
我如何用 pafy 为进度条制作线程
【中文标题】我如何用 pafy 为进度条制作线程【英文标题】:how i can make thread for progress bar with pafy 【发布时间】:2019-01-04 14:55:11 【问题描述】:我正在尝试解决我的程序中的问题,这个问题是当我开始下载视频时,程序没有响应,我也看不到进度条移动,所以我尝试使用线程模块,但我无法解决问题所以我该如何解决问题
通过此代码,我可以下载视频并将数据发送到另一个函数,以检索我用来将其连接到进度条的信息
def video(self):
video_url = self.lineEdit_4.text()
video_save = self.lineEdit_3.text()
pafy_video = pafy.new(video_url)
type_video = pafy_video.videostreams
quality = self.comboBox.currentIndex()
start_download = type_video[quality].download(filepath=video_save,callback=self.video_progressbar)
此代码是从视频功能接收信息以连接进度条
def video_progressbar(self,total, recvd, ratio, rate, eta):
self.progressBar_2.setValue(ratio * 100)
我用;python3.5 pyqt5 pafy
【问题讨论】:
【参考方案1】:移动到另一个线程的一种方法是创建一个位于另一个线程中的QObject
,并在一个槽中执行该任务。并且该槽必须通过QMetaObject::invokeMethod
或信号调用。
with QThread and QMetaObject::invokeMethod:
import pafy
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
self.le_url = QtWidgets.QLineEdit("https://www.youtube.com/watch?v=bMt47wvK6u0")
path = QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.DownloadLocation)
self.le_output = QtWidgets.QLineEdit(path)
self.btn_quality = QtWidgets.QPushButton("Get qualities")
self.combo_quality = QtWidgets.QComboBox()
self.btn_download = QtWidgets.QPushButton("Download")
self.progressbar = QtWidgets.QProgressBar(maximum=100)
self.downloader = DownLoader()
thread = QtCore.QThread(self)
thread.start()
self.downloader.moveToThread(thread)
self.btn_quality.clicked.connect(self.on_clicked_quality)
self.btn_download.clicked.connect(self.download)
self.btn_download.setDisabled(True)
self.downloader.progressChanged.connect(self.progressbar.setValue)
self.downloader.qualitiesChanged.connect(self.update_qualityes)
self.downloader.finished.connect(self.on_finished)
form_lay = QtWidgets.QFormLayout(central_widget)
form_lay.addRow("Url: ", self.le_url)
form_lay.addRow(self.btn_quality)
form_lay.addRow("qualities: ", self.combo_quality)
form_lay.addRow("Output: ", self.le_output)
form_lay.addRow(self.btn_download)
form_lay.addRow(self.progressbar)
@QtCore.pyqtSlot()
def on_finished(self):
self.update_disables(False)
@QtCore.pyqtSlot()
def on_clicked_quality(self):
video_url = self.le_url.text()
QtCore.QMetaObject.invokeMethod(self.downloader, "get_qualities",
QtCore.Qt.QueuedConnection,
QtCore.Q_ARG(str, video_url))
@QtCore.pyqtSlot(list)
def update_qualityes(self, types_of_video):
for t in types_of_video:
self.combo_quality.addItem(str(t), t)
self.btn_download.setDisabled(False)
@QtCore.pyqtSlot()
def download(self):
video_save = self.le_output.text()
d = self.combo_quality.currentData()
QtCore.QMetaObject.invokeMethod(self.downloader, "start_download",
QtCore.Qt.QueuedConnection,
QtCore.Q_ARG(object, d),
QtCore.Q_ARG(str, video_save))
self.update_disables(True)
def update_disables(self, state):
self.combo_quality.setDisabled(state)
self.btn_quality.setDisabled(state)
self.le_output.setDisabled(state)
self.le_url.setDisabled(state)
self.btn_download.setDisabled(not state)
class DownLoader(QtCore.QObject):
progressChanged = QtCore.pyqtSignal(int)
qualitiesChanged = QtCore.pyqtSignal(list)
finished = QtCore.pyqtSignal()
@QtCore.pyqtSlot(str)
def get_qualities(self, video_url):
pafy_video = pafy.new(video_url)
types_of_video = pafy_video.allstreams # videostreams
self.qualitiesChanged.emit(types_of_video)
@QtCore.pyqtSlot(object, str)
def start_download(self, d, filepath):
d.download(filepath=filepath, callback=self.callback)
def callback(self, total, recvd, ratio, rate, eta):
val = int(ratio * 100)
self.progressChanged.emit(val)
if val == 100:
self.finished.emit()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(320, 480)
w.show()
sys.exit(app.exec_())
with threading.Thread
:
import pafy
import threading
from PyQt5 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
qualitiesChanged = QtCore.pyqtSignal(list)
progressChanged = QtCore.pyqtSignal(int)
finished = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
self.le_url = QtWidgets.QLineEdit("https://www.youtube.com/watch?v=bMt47wvK6u0")
path = QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.DownloadLocation)
self.le_output = QtWidgets.QLineEdit(path)
self.btn_quality = QtWidgets.QPushButton("Get qualities")
self.combo_quality = QtWidgets.QComboBox()
self.btn_download = QtWidgets.QPushButton("Download")
self.progressbar = QtWidgets.QProgressBar(maximum=100)
self.btn_quality.clicked.connect(self.on_clicked_quality)
self.btn_download.clicked.connect(self.download)
self.btn_download.setDisabled(True)
self.progressChanged.connect(self.progressbar.setValue)
self.qualitiesChanged.connect(self.update_qualityes)
self.finished.connect(self.on_finished)
form_lay = QtWidgets.QFormLayout(central_widget)
form_lay.addRow("Url: ", self.le_url)
form_lay.addRow(self.btn_quality)
form_lay.addRow("qualities: ", self.combo_quality)
form_lay.addRow("Output: ", self.le_output)
form_lay.addRow(self.btn_download)
form_lay.addRow(self.progressbar)
@QtCore.pyqtSlot()
def on_finished(self):
self.update_disables(False)
@QtCore.pyqtSlot()
def on_clicked_quality(self):
video_url = self.le_url.text()
threading.Thread(target=self.get_qualities, args=(video_url,)).start()
def get_qualities(self, video_url):
pafy_video = pafy.new(video_url)
types_of_video = pafy_video.allstreams # videostreams
self.qualitiesChanged.emit(types_of_video)
@QtCore.pyqtSlot(list)
def update_qualityes(self, types_of_video):
for t in types_of_video:
self.combo_quality.addItem(str(t), t)
self.btn_download.setDisabled(False)
@QtCore.pyqtSlot()
def download(self):
video_save = self.le_output.text()
d = self.combo_quality.currentData()
threading.Thread(target=d.download, kwargs='filepath': video_save, 'callback': self.callback, daemon=True).start()
def callback(self, total, recvd, ratio, rate, eta):
print(ratio)
val = int(ratio * 100)
self.progressChanged.emit(val)
if val == 100:
self.finished.emit()
def update_disables(self, state):
self.combo_quality.setDisabled(state)
self.btn_quality.setDisabled(state)
self.le_output.setDisabled(state)
self.le_url.setDisabled(state)
self.btn_download.setDisabled(not state)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(320, 480)
w.show()
sys.exit(app.exec_())
【讨论】:
感谢您的代码运行良好,但我的代码无法使用 qthread,我使用的是 QmainWindow 因为我理解你是50%的代码所以我尝试使用qthread,并没有成功解决无响应问题 你刚换了课 我会把我的代码交给你解决我的问题,并将 qthread 放在正确的位置 谢谢你,我再次尝试阅读你的代码,我现在明白了如何使用线程以上是关于如何用C语言实现进度条的主要内容,如果未能解决你的问题,请参考以下文章