目錄
概述
繼承圖
QColorDialog 顏色對(duì)話框
QFileDialog 文件對(duì)話框
保存文件對(duì)話框
QFontDialog 字體對(duì)話框
QInputDialog 輸入對(duì)話框
概述
- 對(duì)于對(duì)話框的功能,在GUI圖形界面開發(fā)過(guò)程,使用是非常多,那么Qt也提供了豐富的對(duì)話框類
- QDialog是所有對(duì)話框的基類
繼承圖
- QWidget?
- QDialog
- QColorDialog 顏色對(duì)話框
- QFileDialog 文件對(duì)話框
- QFontDialog?
- QInputDialog
- QMessageBox
- QProgressDialog
- QDialog
QColorDialog 顏色對(duì)話框
頭文件 #include <QColorDialog>
彈出顏色對(duì)話框
QColorDialog::getColor();
獲取顏色對(duì)話框選擇的顏色
QColor color = QColorDialog::getColor();
獲取rgb
color.red();
color.green();
color.blue();
QColorDialog::getColor();
默認(rèn)參數(shù)
第一個(gè),默認(rèn)當(dāng)前選擇的顏色是白色 Qt::while
第二個(gè),父類,nullptr
第三個(gè),標(biāo)題,QString()
第四個(gè),樣式,ColorDialogOptions()
其他樣式有
ShowAlphaChannel 多了一個(gè)透明度
NoButtons 沒有按鈕
DontUseNativeDialog
QFileDialog 文件對(duì)話框
給用戶選擇一個(gè)文件或者多個(gè)文件或者目錄
頭文件?#include <QFileDialog >
通過(guò)靜態(tài)函數(shù)彈出文件對(duì)話框,返回文件路徑
QString fileName = QFileDialog::getOpenFileName(
this,//父部件
"Open Image",//標(biāo)題
"/home/jana",//默認(rèn)路徑
"Image Files (*.png *.jpg *.bmp)")//文件過(guò)濾器
);
返回文件路徑容器
QStringList list = QFileDialog::getOpenFileNames(
this,//父部件
"Open Image",//標(biāo)題
"./",//默認(rèn)路徑
"Image Files (*.png *.jpg *.bmp)")//文件過(guò)濾器
);
for(int i = 0; i < list.size(); i++)
{
qDebug()<<list.at(i);
}
案例
打開文件顯示文件里的內(nèi)容
QString fileName = QFileDialog::getOpenFileName(
this,//父部件
"Open Image",//標(biāo)題
"/home/jana",//默認(rèn)路徑
"Image Files (*.png *.jpg *.bmp)")//文件過(guò)濾器
);
//實(shí)例化文件類對(duì)象
QFile file(fileName);
//打開文件
file.open(QIODevice::ReadOnly);
//讀取文件內(nèi)容
QByteArray content = file.readAll();
//將讀取的文件內(nèi)容顯示到編輯框中
ui->textEdit->setText(content);
//關(guān)閉文件
file.close();
保存文件對(duì)話框
功能:另存為,它將返回用戶選擇的文件名,文件不需要存在,用戶保存,給文件另存為命名
QString getSaveFileName(
QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
QFileDialog::Options options = Options()
)
案例
//1、彈出保存文件對(duì)話框,讓用戶選擇 將這些數(shù)據(jù) 保存到哪個(gè)文件中
QString fileName = QFileDialog::getSaveFileName(this,"Open Image", "./", "Files (*.cpp *.h)" );
if(fileName.isEmpty())
{
return ;
}
//2、打開文件,如果文件不村子則創(chuàng)建,存在則清空
QFile file(fileName);
bool ret = file.open(QIODevice::WriteOnly|QIODevice::Truncate);
if(ret == false)
{
return ;
}
//3、從界面上的編輯框上獲取文件的數(shù)據(jù)
QString content = ui->textEdit->toPlainText();
//4、寫入到文件中
file.write(content.toUtf8());
//5、關(guān)閉文件
file.close();
QFontDialog 字體對(duì)話框
頭文件 #include <QFontDialog>
//主要函數(shù)
QFont getFont( bool *ok,//對(duì)獲得字體的結(jié)果
const QFont &inital,//默認(rèn)字體
QWidget *parent = nullptr,//父部件
const QString &title = QString(),//標(biāo)題
QFontDialog::FontDialogOptions options = FontDialogOptions()//可選項(xiàng)
)
QFont getFont(bool *ok, QWidget *parent = nullptr)
案例
彈出字體對(duì)話框并返回選擇的字體
bool ok;
QFont font = QFontDialog::getFont(&ok , QFont("Helvetica [Cronyx]", 10),this);
if(ok){
ui->label->setFont(font);
}
QInputDialog 輸入對(duì)話框
頭文件 #include <QInputDialog >文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-810786.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-810786.html
構(gòu)造函數(shù)
QInputDialog(
QWidget *parent = nullptr,
Qt::WindowFlags flags = Qt::WindowFlags()
)
主要函數(shù)
QString getText(
QWidget *parent,//父部件
const QString &title,//標(biāo)題
const QString &label,//提示語(yǔ)
QLineEdit::EchoMode mode = QLineEdit::Normal,//內(nèi)容回寫模式
const QString &text = QString(),//輸入框的真正內(nèi)容
bool *ok = nullptr,//結(jié)果
Qt::WindowFlags flags = Qt::WindowFlags(),//
Qt::InputMethodHints inputMethodHints = Qt::ImhNone//
)
案例
獲取輸入數(shù)據(jù)設(shè)置到一個(gè)控件上
bool ok;
QString text= QInputDialog::getText(
this,
"字體對(duì)話框",
"輸入提示".
QLineEdit::Normal,
"",
&ok
);
if(ok){
ui->label->setText(text);
}
到了這里,關(guān)于qt學(xué)習(xí):QT對(duì)話框+顏色+文件+字體+輸入的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!