• Qt标准对话框设置


    Qt标准对话框设置,设置字体、调色板、进度条等。

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
        QPalette pa = ui->textEdit->palette();
        QColor initColor = pa.color(QPalette::Text);//获取原来颜色
        QColor color = QColorDialog::getColor(initColor,this,"选择颜色");//调色板刚打开时,初始化为原颜色
        if(color.value()){
            pa.setColor(QPalette::Text,color);
            ui->textEdit->setPalette(pa);//为小部件设置新修改的颜色
        }
    }
    
    //设置的字体,需要在程序启动后输入的文本设置,如果是原本在ui文件中写死的文本,则字体设置不起作用
    void MainWindow::on_pushButtonFont_clicked()
    {
        QFont initFont = ui->textEdit->font();//获取当前字体
        bool ok = false;
        QFont font = QFontDialog::getFont(&ok,initFont);
        if(ok){
            ui->textEdit->setFont(font);
        }
    }
    
    
    //进度条
    void MainWindow::on_pushButtoProgress_clicked()
    {
        QProgressDialog progress("正在复制文件… …","取消",0,200,this);
        progress.setWindowTitle("正在复制");
        progress.setWindowModality(Qt::WindowModal);//设置模态
    
        connect(&progress,&QProgressDialog::canceled,this,[&]{ui->textEdit->append("进度条已经取消了");});//设置一个信号槽,当进度条取消按钮按下后,打印一条消息
        //模拟时间消耗
        QElapsedTimer msCounter;//模拟一个毫秒计时器
        for(int i=0; i<=200; i++){
            progress.setValue(i);
            progress.setLabelText(QString::asprintf("正在复制,第%d",i));
    
            msCounter.start();
            while (1) {
                if(msCounter.elapsed() > 30){//每次循环等待30毫秒,就退出
                    break;
                }
            }
    
    
            //如果点击了取消按钮,直接退出,不在循环了
            if(progress.wasCanceled()){
                break;
            }
        }
    }
    
    //输入字符串对话框显示
    void MainWindow::on_pushButtoInput_clicked()
    {
        QString title = "输入对话框";
        QString textLable = "请输入文件名";
        QString initText = "新建文件.txt";
        bool ok = false;
        QLineEdit::EchoMode echoMode = QLineEdit::Password;//以密码形式的输入小部件显示
        QString text = QInputDialog::getText(this,title,textLable,echoMode,initText,&ok);
        if(ok && !text.isEmpty()){
            ui->textEdit->append(text);
        }
    }
    
    
    //输入整形对话框,调整字体大小
    void MainWindow::on_pushButtoInputInt_clicked()
    {
        QString title = "输入整形对话框";
        QString txtLable = "设置文本框字体大小";
        int defaultFontSize = ui->textEdit->font().pointSize();//获取小部件默认字体大小
        int minFontSize = 6;//最小字体大小
        int maxFontSize = 80;//最大字体大小
        int stepValue = 1;//步进长度
        bool ok = false;
    
        int inputValue = QInputDialog::getInt(this,title,txtLable,defaultFontSize,minFontSize,maxFontSize,stepValue,&ok);
        if(ok){
            QString str = QString("文本框字体大小设置为: %1").arg(inputValue);
            ui->textEdit->append(str);
            QFont font = ui->textEdit->font();
            font.setPointSize(inputValue);
            ui->textEdit->setFont(font);
        }
    }
    
    
    //输入条目对话框
    void MainWindow::on_pushButtoInputItem_clicked()
    {
        QString title = "输入条目对话框";
        QString textLable = "请选择型号  ";
        QStringList items;
        items<<"甲"<<"乙"<<"丙"<<"丁";
    
        int curIndex = 0;//当前索引
        bool ok = false;
        bool editable = false;//不可编辑
    
        QString text = QInputDialog::getItem(this,title,textLable,items,curIndex,editable,&ok);
        if(ok && !text.isEmpty()){
            ui->textEdit->append(text);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
  • 相关阅读:
    【openGauss-3.0.0单节点安装】
    数据可视化之交通可视化
    Systemverilog-- OOP--对象的拷贝
    判断链表是否成环
    区块链3.0时代 基于GoMars构建的新概念TravelFi能否注入新力量?
    mybatis动态sql
    information_schema.tables说明
    odoo16前端框架分析1 boot.js
    传统算法与神经网络算法,神经网络算法有什么用
    之前翻硬币问题胡思乱想的完善
  • 原文地址:https://blog.csdn.net/weixin_44585751/article/details/136171445