其實我原本不打算學習這一塊的,最多學習個QFileDilog就夠用了,可是想了一下,萬一那天真的需求提到了顏色,字體上面,就尷尬了,技多不壓身,瞭解瞭解吧,至少多年後本身還有博客能夠翻。ide
QFileDialog函數
一、QFileDialog::getOpenFileName 以及系列的函數,都是獲取文件的名稱 或者 路徑用的, 值得一提的就是QFileDialog::getOpenFileUrl,它返回的URL,如果採用【toString】轉換成 QString,那麼他會在磁盤路徑的前面多出file://這應該是win文件系統致使的,具體後面學到操做系統再回頭說吧,要想獲取磁盤路徑,就用【toLocalFile】。 二、QFileDialog::getOpenFileNames 以及系列的函數,都是獲取多個文件的名稱 或者 路徑用的, 一樣,這個也有獲取路徑的函數,【QFileDialog::getOpenFileUrls】不過它直接返回了QUrl的list,因此只能單獨遍歷,而後按照上面的方法進行轉換。 三、QFileDialog::getSaveFileName 以及系列的函數,獲取保存文件的名稱 或者 路徑用的, 一樣,它也有獲取路徑的函數,【QFileDialog::getSaveFileUrl】這個返回的也是QUrl對象,能夠按照【1】的辦法來處,獲得QString對象。 四、QFileDialog::getExistingDirectory() 這個函數實在沒看出有什麼用,我設置了路徑,仍是在別處打開,就很坑爹了。不知道是否是由於win10機制修改的緣由,待定吧!
QColorDialog學習
QColorDialog::getColor 這個函數主要用來選擇調色板裏面的顏色以及返回用戶選擇的顏色(QColor對象)
QFontDialog測試
QFontDialog::getFont 這個函數主要用來選擇的字體以及返回用戶選擇的字體(QFont) ***QInputDialog***
QInputDialog::getText 這個函數主要用來返回用戶在窗口裏面輸入的文字,我的感受用途不大字體
具體的測試代碼請見我上傳的code吧,寫的有點爛,不足之處請多指教。this
注:一個GroupBox就是一個Dialog,每個按鈕就是一個功能。url
=============補充===============操作系統
我錯了,代碼不能上傳,貼在這裏吧:code
頭文件 #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QGroupBox> #include <QFileDialog> #include <QColorDialog> #include <QFontDialog> #include <QInputDialog> #include <QPushButton> #include <QLineEdit> #include <QHBoxLayout> #include <QVBoxLayout> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private: QPushButton *m_open_file; QLineEdit *m_line_edit; QHBoxLayout *m_open_file_layout; QPushButton *m_open_files; QLineEdit *m_line_edits; QHBoxLayout *m_open_files_layout; QPushButton *m_exist_dir; QLineEdit *m_exist_dir_edit; QHBoxLayout *m_exist_dir_layout; QPushButton *m_save_dir; QLineEdit *m_save_edit; QHBoxLayout *m_save_layout; QVBoxLayout *m_dialog_layout; QPushButton *m_color_btn; QPushButton *m_font_btn; QLineEdit *m_color_edit; QHBoxLayout *m_item_layout; QVBoxLayout *m_font_layout; QPushButton *m_input_btn; QLineEdit *m_input_edit; QHBoxLayout *m_input_layout; QVBoxLayout *m_main_layout; private slots: void get_open_file(); void get_open_files(); void get_exist_dir(); void save_file(); void choose_color(); void choose_font(); void get_input(); }; #endif // WIDGET_H .cpp文件 #include <QDir> #include <QFont> #include <sstream> Widget::Widget(QWidget *parent) : QWidget(parent) { m_open_file = new QPushButton; m_open_file->setText("打開文件 "); m_line_edit = new QLineEdit; m_line_edit->setEnabled(false); m_open_file_layout = new QHBoxLayout; m_open_file_layout->addWidget(m_open_file, 1, Qt::AlignLeft); m_open_file_layout->addWidget(m_line_edit, 2, Qt::AlignRight); m_open_files = new QPushButton; m_open_files->setText("打開一組文件"); m_line_edits = new QLineEdit; m_line_edits->setEnabled(false); m_open_files_layout = new QHBoxLayout; m_open_files_layout->addWidget(m_open_files, 1, Qt::AlignLeft); m_open_files_layout->addWidget(m_line_edits, 2, Qt::AlignRight); m_exist_dir = new QPushButton; m_exist_dir->setText("打開已有目錄"); m_exist_dir_edit = new QLineEdit; m_exist_dir_edit->setEnabled(false); m_exist_dir_layout = new QHBoxLayout; m_exist_dir_layout->addWidget(m_exist_dir, 1, Qt::AlignLeft); m_exist_dir_layout->addWidget(m_exist_dir_edit, 2, Qt::AlignRight); m_save_dir = new QPushButton; m_save_dir->setText("保存文件到 "); m_save_edit = new QLineEdit; m_save_edit->setEnabled(false); m_save_layout = new QHBoxLayout; m_save_layout->addWidget(m_save_dir, 1, Qt::AlignLeft); m_save_layout->addWidget(m_save_edit, 2, Qt::AlignRight); m_dialog_layout = new QVBoxLayout; m_dialog_layout->addLayout(m_open_file_layout, 1); m_dialog_layout->addLayout(m_open_files_layout, 1); m_dialog_layout->addLayout(m_exist_dir_layout, 1); m_dialog_layout->addLayout(m_save_layout, 1); QGroupBox* file_dlg_box = new QGroupBox; file_dlg_box->setLayout(m_dialog_layout); file_dlg_box->setTitle("FileDialog"); m_color_btn = new QPushButton; m_color_btn->setText("請選擇顏色 "); m_font_btn = new QPushButton; m_font_btn->setText("請選擇字體 "); m_item_layout = new QHBoxLayout; m_item_layout->addWidget(m_color_btn, 1, Qt::AlignLeft); m_item_layout->addWidget(m_font_btn, 1, Qt::AlignRight); m_color_edit = new QLineEdit; m_color_edit->setEnabled(false); m_font_layout = new QVBoxLayout; m_font_layout->addLayout(m_item_layout); m_font_layout->addWidget(m_color_edit); QGroupBox* color_dlg_box = new QGroupBox; color_dlg_box->setTitle("ColorDialog"); color_dlg_box->setLayout(m_font_layout); m_input_btn = new QPushButton; m_input_btn->setText(" 獲取輸入 "); m_input_edit = new QLineEdit; m_input_edit->setEnabled(false); m_input_layout = new QHBoxLayout; m_input_layout->addWidget(m_input_btn, 1, Qt::AlignLeft); m_input_layout->addWidget(m_input_edit, 2, Qt::AlignRight); QGroupBox* input_dlg_box = new QGroupBox; input_dlg_box->setTitle("InputDialog"); input_dlg_box->setLayout(m_input_layout); m_main_layout = new QVBoxLayout; m_main_layout->addWidget(file_dlg_box); m_main_layout->addWidget(color_dlg_box); m_main_layout->addWidget(input_dlg_box); setLayout(m_main_layout); connect(m_open_file, SIGNAL(clicked()), this, SLOT(get_open_file())); connect(m_open_files, SIGNAL(clicked()), this, SLOT(get_open_files())); connect(m_exist_dir, SIGNAL(clicked()), this, SLOT(get_exist_dir())); connect(m_save_dir, SIGNAL(clicked()), this, SLOT(save_file())); connect(m_color_btn, SIGNAL(clicked()), this, SLOT(choose_color())); connect(m_font_btn, SIGNAL(clicked()), this, SLOT(choose_font())); connect(m_input_btn, SIGNAL(clicked()), this, SLOT(get_input())); }; Widget::~Widget() { } void Widget::get_open_file() { QString current_path = QDir::currentPath(); QString dlgTitle = "打開文件"; QString filter="文本文件(*.txt);;圖片文件(*.jpg *.gif *.png);;全部文件(*.*);;"; QString strFileUrl = QFileDialog::getOpenFileUrl(this, dlgTitle, current_path, filter).toLocalFile(); m_line_edit->setText(strFileUrl); } void Widget::get_open_files() { QString current_path = QDir::currentPath(); QString dlgTitle = "打開文件"; QString filter="文本文件(*.txt);;圖片文件(*.jpg *.gif *.png);;全部文件(*.*);;"; QList<QUrl> url_list = QFileDialog::getOpenFileUrls(this, dlgTitle, current_path, filter); QString strFileUrl; for(int i = 0; i < url_list.size(); ++i) { strFileUrl += url_list[i].toLocalFile() + ";"; } m_line_edits->setText(strFileUrl); } void Widget::get_exist_dir() { QUrl url("D:\\software\\Qt5_13\\"); QString dlgTitle = "打開文件"; QString dirPath = QFileDialog::getExistingDirectoryUrl(this, dlgTitle, url, QFileDialog::ShowDirsOnly).toLocalFile(); m_exist_dir_edit->setText(dirPath); } void Widget::save_file() { QUrl url("D:\\software\\Qt5_13\\"); QString dlgTitle = "保存文件"; QString filter="文本文件(*.txt);;C++文件(.cpp);;全部文件(*.*);;"; //這裏記得,若是是分欄過濾文件類型,就要兩個; QString filePath = QFileDialog::getSaveFileUrl(this, dlgTitle, url, filter).toLocalFile(); m_save_edit->setText(filePath); } void Widget::choose_color() { QPalette palette_init = m_color_edit->palette(); QColor color_init = palette_init.color(QPalette::Text); QColor current_color = QColorDialog::getColor(color_init, this, "選擇顏色"); if(current_color.isValid()) { int red, green, blue; current_color.getRgb(&red, &green, &blue); std::ostringstream oss; oss << "R:" << red << ", G:" << green << ", B:" << blue; QString tmp(oss.str().c_str()); m_color_edit->setText(tmp); palette_init.setColor(QPalette::Text, current_color); m_color_edit->setPalette(palette_init); } } void Widget::choose_font() { bool ok = false; QFont font_init = m_color_edit->font(); QFont current_font = QFontDialog::getFont(&ok, font_init); if(ok) { m_color_edit->setFont(current_font); } } void Widget::get_input() { bool ok = false; QString dlgTitle="輸入文字對話框"; QString txtLabel="請輸入文件名"; QString defaultInput="新建文件.txt"; QString strText = QInputDialog::getText(this, dlgTitle, txtLabel, QLineEdit::Normal, defaultInput, &ok); if(ok) { m_input_edit->setText(strText); } }