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);}}