【Qt】QListWidget 新增控制元件
阿新 • • 發佈:2022-05-26
介面
程式碼
dialog.cpp
#include "dialog.h" #include "ui_dialog.h" #include <QFileDialog> #include <QColorDialog> #include <QFontDialog> #include <QInputDialog> #include <QMessageBox> Dialog::Dialog(QWidget *parent) : QDialog(parent) , ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::on_btnClearText_clicked() { ui->plainTextEdit->clear(); } void Dialog::on_btnOpen_clicked() { QString curPath = QDir::currentPath(); //對話方塊預設開啟目錄 QString dlgTitle = "選擇對話方塊"; QString filter = "文字檔案(*.txt);;圖片檔案(*.jpg *.gif);;所有格式(*.*)"; QString fileName = QFileDialog::getOpenFileName(this, dlgTitle, curPath, filter); if(fileName.isEmpty()) return ; ui->plainTextEdit->appendPlainText(fileName); } void Dialog::on_btnOpenMulti_clicked() { QString curPath = QDir::currentPath(); QString dlgTitle = "選擇對話方塊"; QString filter = "文字檔案(*.txt);;圖片檔案(*.jpg *.gif);;所有格式(*.*)"; QStringList fileNames = QFileDialog::getOpenFileNames(this, dlgTitle, curPath, filter); if(fileNames.count() == 0) return ; foreach(auto str, fileNames){ ui->plainTextEdit->appendPlainText(str); } } void Dialog::on_btnSelDir_clicked() { QString curPath = QDir::currentPath(); QString dlgTitle = "選擇對話方塊"; //QString filter = "文字檔案(*.txt);;圖片檔案(*.jpg *.gif);;所有格式(*.*)"; QString selectedDir = QFileDialog::getExistingDirectory(this, dlgTitle, curPath, QFileDialog::ShowDirsOnly); //只顯示目錄 if(selectedDir.isEmpty()) return ; ui->plainTextEdit->appendPlainText(selectedDir); } void Dialog::on_btnSave_clicked() { QString curPath = QCoreApplication::applicationDirPath(); //對話方塊預設開啟目錄 QString dlgTitle = "選擇對話方塊"; QString filter = "文字檔案(*.txt);;圖片檔案(*.jpg *.gif);;所有格式(*.*)"; QString fileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath, filter); if(fileName.isEmpty()) return ; ui->plainTextEdit->appendPlainText("儲存檔案:" + fileName); } void Dialog::on_btnColor_clicked() { QPalette pal = ui->plainTextEdit->palette(); QColor initColor = pal.color(QPalette::Text); QColor color = QColorDialog::getColor(initColor, this, "選擇一個顏色"); if(!color.isValid()) return; pal.setColor(QPalette::Text, color); ui->plainTextEdit->setPalette(pal); } void Dialog::on_btnFont_clicked() { bool ok = false; QFont initFont = ui->plainTextEdit->font(); QFont font = QFontDialog::getFont(&ok, initFont, this, "選擇字型"); if(!ok) return; ui->plainTextEdit->setFont(font); } void Dialog::on_btnInputString_clicked() { QString dlgTitle = "輸入文字對話方塊"; QString txtLabel = "請輸入檔名"; QString defaultInput = "xxxx.txt"; QLineEdit::EchoMode echoMode = QLineEdit::Normal; bool ok = false; QString str = QInputDialog::getText(this, dlgTitle, txtLabel, echoMode, defaultInput, &ok); if(!ok) return ; ui->plainTextEdit->appendPlainText(str); } void Dialog::on_btnInputInt_clicked() { QString dlgTitle = "輸入整數對話方塊"; QString txtLabel = "請輸入整數"; int defaultVal = ui->plainTextEdit->font().pointSize(); int minValue = 0, maxValue = 100; bool ok = false; int val = QInputDialog::getInt(this, dlgTitle, txtLabel, defaultVal, minValue, maxValue, 1, &ok); //步長為1 if(!ok) return ; QFont font = ui->plainTextEdit->font(); font.setPointSize(val); ui->plainTextEdit->setFont(font); ui->plainTextEdit->appendPlainText(QString::asprintf("輸入了一個整數:%d", val)); } void Dialog::on_btnInputFloat_clicked() { QString dlgTitle = "輸入浮點數對話方塊"; QString txtLabel = "請輸入浮點數"; double defaultVal = 15.0; double minValue = 0, maxValue = 100; bool ok = false; double val = QInputDialog::getDouble(this, dlgTitle, txtLabel, defaultVal, minValue, maxValue, 2, &ok); //小數點後2位 if(!ok) return ; ui->plainTextEdit->appendPlainText(QString::asprintf("輸入了一個浮點數:%.2f", val)); } void Dialog::on_btnInputItem_clicked() { QStringList items; items << "優先" << "良好" << "及格" << "不及格"; QString dlgTitle = "輸入條目對話方塊"; QString txtLabel = "請選擇條目"; bool ok = false; QString text = QInputDialog::getItem(this, dlgTitle, txtLabel, items, 0, true, &ok); //true表示除了選擇還可以輸入 if(!ok || text.isEmpty()) return ; ui->plainTextEdit->appendPlainText(text); } void Dialog::on_btnMsgQuestion_clicked() { QString dlgTitle = "Question訊息框"; QString txtLabel = "檔案已被修改,是否儲存?"; QMessageBox::StandardButtons result; result = QMessageBox::question(this, dlgTitle, txtLabel, QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::NoButton); QString str; if(result == QMessageBox::Yes) str = "Question訊息框 yes被選中"; else if(result == QMessageBox::No) str = "Question訊息框 No被選中"; else if(result == QMessageBox::Cancel) str = "Question訊息框 Cancel被選中"; ui->plainTextEdit->appendPlainText(str); } void Dialog::on_btnMsgInformation_clicked() { QMessageBox::information(this, "Title", "TXT", QMessageBox::Ok); } void Dialog::on_btnMsgWarning_clicked() { QMessageBox::warning(this, "Title", "TXT", QMessageBox::Ok); } void Dialog::on_btnMsgCritical_clicked() { QMessageBox::critical(this, "Title", "TXT", QMessageBox::Ok); } void Dialog::on_btnMsgAbout_clicked() { QMessageBox::about(this, "Title", "TXT"); } void Dialog::on_btnMsgAboutQt_clicked() { QMessageBox::aboutQt(this, "Title"); }