QT 教程 之 基础Dialog

其实我本来不打算学习这一块的,最多学习个QFileDilog就够用了,但是想了一下,万一那天真的需求提到了颜色,字体上面,就尴尬了,技多不压身,了解了解吧,至少多年后自己还有博客可以翻。

QFileDialog

1、QFileDialog::getOpenFileName  以及系列的函数,都是获取文件的名称 或者 路径用的,
值得一提的就是QFileDialog::getOpenFileUrl,它返回的URL,若是采用【toString】转换成 QString,那么他会在磁盘路径的前面多出file://这应该是win文件系统导致的,具体后面学到操作系统再回头说吧,要想获取磁盘路径,就用【toLocalFile】。

2、QFileDialog::getOpenFileNames  以及系列的函数,都是获取多个文件的名称 或者 路径用的,
同样,这个也有获取路径的函数,【QFileDialog::getOpenFileUrls】不过它直接返回了QUrl的list,所以只能单独遍历,然后按照上面的方法进行转换。

3、QFileDialog::getSaveFileName 以及系列的函数,获取保存文件的名称 或者 路径用的,
同样,它也有获取路径的函数,【QFileDialog::getSaveFileUrl】这个返回的也是QUrl对象,可以按照【1】的办法来处,得到QString对象。

4、QFileDialog::getExistingDirectory() 这个函数实在没看出有什么用,我设置了路径,还是在别处打开,就很坑爹了。不知道是不是因为win10机制修改的原因,待定吧!

QColorDialog

QColorDialog::getColor 这个函数主要用来选择调色板里面的颜色以及返回用户选择的颜色(QColor对象)

QFontDialog

QFontDialog::getFont 这个函数主要用来选择的字体以及返回用户选择的字体(QFont)

***QInputDialog***

QInputDialog::getText 这个函数主要用来返回用户在窗口里面输入的文字,个人感觉用途不大

具体的测试代码请见我上传的code吧,写的有点烂,不足之处请多指教。
《QT 教程 之 基础Dialog》

注:一个GroupBox就是一个Dialog,每一个按钮就是一个功能。

=============补充===============

我错了,代码不能上传,贴在这里吧:

头文件
#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);
    }
}
    原文作者:CLEVERlBOY
    原文地址: https://blog.51cto.com/11753138/2449570
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞