QT常用控件之QTimer,QDialog,QLabel,QLineEdit,QProgressBar,QComboBox,QPushButton,QGridLayout

来源:互联网 发布:java变量定义规则 编辑:程序博客网 时间:2024/05/18 00:02

QT常用控件的组合

#ifndef PROGRESSBARWIDGET_H#define PROGRESSBARWIDGET_H#include <QWidget>#include <QTimer>#include <QDialog>#include <QLabel>#include <QLineEdit>#include <QProgressBar>   //显示进度条的控件#include <QComboBox>#include <QPushButton>#include <QGridLayout>    //网格布局class ProgressBarWidget : public QWidget{    Q_OBJECTprivate:    int tmp=0;    bool flag=true;    QLabel *FileNum;    //表示文件的数量    QLineEdit *FileNumLineEdit;    QLabel *ProgressType;    QComboBox *comboBox;    QGridLayout *mainLayout;    QPushButton *btn;    QProgressBar *qpb;    QTimer *qtm;private slots:    void on_click_button();    void time_out();public:    ProgressBarWidget(QWidget *parent = 0);    ~ProgressBarWidget();};#endif // PROGRESSBARWIDGET_H

#include "progressbarwidget.h"#include <QProgressDialog>    //这个显示进度条的慢速过程的进度框#include <QFont>ProgressBarWidget::ProgressBarWidget(QWidget *parent)    : QWidget(parent){    //完成界面的初始化    QFont font("宋体", 12);    setFont(font);    setWindowTitle(tr("Progress"));    resize(400,200);    FileNum = new QLabel;    FileNum->setText(tr("the file num"));   //文件的数目    FileNumLineEdit = new QLineEdit;    FileNumLineEdit->setText(tr("100000"));   //默认值    ProgressType = new QLabel;    ProgressType->setText(tr("the show type"));     //显示的类型    comboBox = new QComboBox;    comboBox->addItem(tr("progressBar"));   //第一种显示方式    comboBox->addItem(tr("progressDialog"));      //第二种显示方式    btn=new QPushButton(this);    btn->setText("push");    qpb=new QProgressBar(this);    qpb->setValue(0);    qpb->setFormat("%p%");    //按完成的百分比显示    mainLayout=new QGridLayout(this);    mainLayout->addWidget(FileNum,0,0);    mainLayout->addWidget(FileNumLineEdit,0,1);    mainLayout->addWidget(ProgressType, 1, 0);    mainLayout->addWidget(comboBox,1,1);    mainLayout->addWidget(qpb,2,0,1,2);  //在第二行第一列的位置插一个一行两列的进度条    mainLayout->addWidget(btn,3,1);    mainLayout->setMargin(15);    //设置间隔大小    mainLayout->setSpacing(10);    qtm=new QTimer(this);    connect(btn,&QPushButton::clicked,this,&ProgressBarWidget::on_click_button);    connect(qtm,&QTimer::timeout,this,&ProgressBarWidget::time_out);}void ProgressBarWidget::on_click_button(){    bool ok;    int num = FileNumLineEdit->text().toInt(&ok);     //把文本转换为int类型值    if(comboBox->currentIndex() == 0){       //如果选择的是第一个的话ProgressBar模式        qpb->setRange(0, num);      //设置范围,最小值和最大值        if(flag==true){//设置标志位,因此可以实现按钮控制进度条            qtm->start(10);//每十毫秒溢出一次            flag=false;        }        else{            qtm->stop();            flag=true;        }    }    else if(comboBox->currentIndex() == 1){        //如果选择的是第二个显示方式的话, ProgressDialog        QProgressDialog *progressDialog = new QProgressDialog(this);        QFont font("宋体", 12);        progressDialog->setFont(font);        //采用模拟的方式进行显示,即显示进度的同时,其他窗口将不响应输入信号        progressDialog->setWindowModality(Qt::WindowModal);        /*         * 在测试中会出现Dialog不显示,程序依旧进入假死状态。不显示的原因是QProgressDialog         * 有一个智能延迟,默认一切短于4秒的操作都不显示Dialog,因为测试时的数据量往往很小,         * 从而导致了问题,将这个值改小或者为0,就可以了。        */        progressDialog->setMinimumDuration(5000);   //设置进度条显示的等待时间,5秒        progressDialog->setWindowTitle(tr("Please Wait"));   //设置标题的显示时间        progressDialog->setLabelText(tr("Copying..."));        progressDialog->setCancelButtonText(tr("Cancel"));     //退出按钮名字        progressDialog->setRange(0,num);    //设置显示的范围        for(int i =  1; i <= num; ++i)  {            progressDialog->setValue(i);   //设置当前的值            if(progressDialog->wasCanceled())  //如果检测到按钮取消被激活,就跳出去                return;        }     }}void ProgressBarWidget::time_out(){    bool ok;    int num = FileNumLineEdit->text().toInt(&ok);     //把文本转换为int类型值    tmp++;    qpb->setValue(tmp);    if(tmp==num){        qtm->stop();    }}ProgressBarWidget::~ProgressBarWidget(){}


阅读全文
0 0
原创粉丝点击