首页 > 其他分享 >QT常用控件之QTimer,QDialog,QLabel,QLineEdit,QProgressBar,QComboBox,QPushButton,QGridLayout

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

时间:2023-10-10 10:07:49浏览次数:32  
标签:控件 progressDialog ProgressBarWidget QT mainLayout tr QPushButton new include


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_OBJECT
private:
    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()
{

}



QT常用控件之QTimer,QDialog,QLabel,QLineEdit,QProgressBar,QComboBox,QPushButton,QGridLayout_进度条


标签:控件,progressDialog,ProgressBarWidget,QT,mainLayout,tr,QPushButton,new,include
From: https://blog.51cto.com/u_6526235/7787905

相关文章

  • 模型视图简介、QListWidget、QTreeWidget、QTableWidget、QStringListModel、QFileSys
    一、模型视图简介   有时,我们的系统需要显示大量数据,比如从数据库中读取数据,以自己的方式显示在自己的应用程序的界面中。早期的Qt要实现这个功能,需要定义一个组件,在这个组件中保存一个数据对象,比如一个列表。我们对这个列表进行查找、插入等的操作,或者把修改的地方写回,然后......
  • Qto_SpaceBaseQuantities
    Qto_SpaceBaseQuantities空间基准量:定义所有引用的空间时通用的基准量。  NameTypeDescriptionHeightQ_LENGTHHöheGesamteHöhedesRaums,vonRohbodenzurRohdecke,wirdnurangegebenwennderRaumeinekonstanteHöhehat.HeightTotalheight......
  • Qt学习随笔-2、Qt中的对象树、坐标系、信号和槽
       1 对象树  1.1 当创建的对象在堆区的时候,如果指点的父亲是QObject派生下来的类或QObject子类派生下来的类,可以不用管理释放的操作,将对象会放入对象树中。  1.2 一定程度上简化了内存回收机制。       2 Qt中的坐标系  2.1 左上......
  • QT-UDP网络编程
    QT_UDP网络编程用户数据报协议(UDP,UserDatagramProtocol);轻量的,不可靠,无连接,面向数据报的传输协议与TCP,特征:UDP通信在本质上不需要区分客户端和服务端,拥有socket的一方本身具有发送和接收数据报的能力.QUdpSocket继承于父类的QAbstractSocket,没有QTcpSocket的流功......
  • QT之QWidget::paintEngine: Should no longer be called的解决办法
    这个还是以前遇到的,今天突然想起来,就决定重现一下当初的错误,以及错误的代码。报错是这个样子的:QWidget::paintEngine:ShouldnolongerbecalledQPainter::begin:Paintdevicereturnedengine==0,type:1其实这个原因很简单,来看看报错的代码:#include"mainwindow.h"......
  • PyQT5 PyQT入门教程(之三)
    REFhttp://code.py40.com/pyqt5/22.html 事件Event所有的GUI程序都是事件驱动的。事件主要由用户触发,但也可能有其他触发方式:例如网络连接、windowmanager或定时器。当我们调用QApplication的exec_()方法时会使程序进入主循环。主循环会获取并分发事件。在事件模型中,有三个参与......
  • PyQT5 PyQT入门教程(之二)
    REFhttps://www.jianshu.com/p/3832eb48f3d5 布局(Layout)管理QtDesigner中,在工具箱中最上方可以看到有4种布局。分别是垂直布局、水平布局、栅格布局和表单布局。  四种布局布局名称   布局含义垂直(Vertical)布局   布局内的控件按照从上到下的顺序纵向排列水平(Horizon......
  • PyQT5 PyQT入门教程(之一)
    REFhttps://www.jianshu.com/p/5b063c5745d0 本篇博客对应的视频讲解:https://www.bilibili.com/video/BV1NZ4y1Z7ue/ QtDesigner的介绍在PyQt中编写UI界面可以直接通过代码来实现,也可以通过QtDesigner来完成。QtDesigner的设计符合MVC的架构,其实现了视图和逻辑的分离,从而实现......
  • 界面控件DevExpress WPF地图组件,助力轻松搭建自己的地图数据服务器!
    DevExpressWPF Map控件允许用户在WPF应用程序中合并所有流行的地图服务,如Bing或OpenStreetMap。您还可以在公司网络中创建自己的地图数据服务器,Map组件完全支持矢量和笛卡尔坐标地图,以及Bing地理代码、搜索、路线和海拔服务。P.S:DevExpressWPF拥有120+个控件和库,将帮助您交付......
  • PyQt 自定义信号带参数 emit
    PyQt5自定义信号带参数importsysfromPyQt5.QtCoreimportpyqtSignal,QObjectfromPyQt5.QtWidgetsimportQMainWindow,QApplicationclassmysignal(QObject):closeApp=pyqtSignal(list)classExample(QMainWindow):def__init__(self):super().......