Qt4——精彩实例分析8

.h

#ifndef PROGRESS_H
#define PROGRESS_H
#include <QDialog>
#include <QLabel>
#include <QFont>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QProgressBar>
#include <QGridLayout>
class Progressmy:public QDialog
{
    Q_OBJECT
public:
    Progressmy();
public:
    QLabel *numLabel;
    QLineEdit *numLineEdit;
    QLabel *typeLabel;
    QComboBox *typeComboBox;

    QProgressBar *progressBar;
    QPushButton *startButton;
private slots:
    void slotStart();

};

#endif // PROGRESS_H

.c

#include "progress.h"
#include <QProgressDialog>
#include <QThread>
Progressmy::Progressmy()
{
    setWindowTitle("进度条的使用");
    QFont font("ZYsong18030",12);
    setFont(font);//设置字体

    numLabel = new QLabel("文件数目");
    numLineEdit = new QLineEdit;//单行输入框
    numLineEdit->setText("10");

    typeLabel = new QLabel("显示类型");
    /*  QComboBox提供了一种以占用最小屏幕空间的方式向用户显示选项列表的方法。
     *  组合框是显示当前项的选择小部件,可以弹出可选项的列表。
     *  组合框可以是可编辑的,允许用户修改列表中的每个项目。
     */
    typeComboBox = new QComboBox;
    typeComboBox->addItem("使用进度条");
    typeComboBox->addItem("使用进度对话框");

    progressBar = new QProgressBar;

    startButton = new QPushButton("开始");

    QGridLayout *layout =new QGridLayout(this);
    layout->addWidget(numLabel,0,0);
    layout->addWidget(numLineEdit,0,1);
    layout->addWidget(typeLabel,1,0);
    layout->addWidget(typeComboBox,1,1);
    layout->addWidget(progressBar,2,0,1,2);//行,列,行跨度,列跨度,就是说这个进度条长用的多
    layout->addWidget(startButton,3,1);
    layout->setMargin(15);//距离边缘15
    layout->setSpacing(10);//控件之间间距10

    connect(startButton,SIGNAL(clicked()),this,SLOT(slotStart()));
}

void Progressmy::slotStart()
{
   int num = numLineEdit->text().toInt();//把文本转换为整形
   if(typeComboBox->currentIndex() == 0)    //使用进度条
   {
       progressBar->setRange(0,num);//设置范围
       for(int i = 0;i < num; i++)
       {
           progressBar->setValue(i);//模拟文件复制
           QThread::msleep(100);
       }
   }
   else if(typeComboBox->currentIndex() == 1)//进度对话框使用
   {
       QProgressDialog *progressDialog = new QProgressDialog(this);
       QFont font("ZYsong18030",12);
       setFont(font);
       progressDialog->setFont(font);
       //模态方式进行显示,显示进度的同时其他窗口不响应输入信号
       progressDialog->setWindowModality(Qt::WindowModal);
       progressDialog->setMinimumDuration(5);//进队对话框出现需等待的时间
       progressDialog->setWindowTitle("请等待");
       progressDialog->setLabelText("正在拷贝");
       progressDialog->setCancelButtonText("取消");
       progressDialog->setRange(0,num);//设置范围
       for(int i = 0;i < num;i++)
       {
           progressDialog->setValue(i);//设置进度条的值
           QThread::msleep(1000);
           if(progressDialog->wasCanceled())//对话框是否取消,是则返回1,return
               return;

       }
   }

}

main.c

#include "mainwindow.h"
#include <QApplication>
#include "progress.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Progressmy b;
    b.show();
    return a.exec();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值