Dialog, the basis of the QT tutorial

Posted by Jaehoon on Mon, 11 Nov 2019 21:19:59 +0100

In fact, I did not intend to learn this section, it is enough to learn at most QFileDilog, but think about it, if the day really needs to mention color, font, embarrassing, more skillful, understand, at least many years later I have a blog to turn over.

QFileDialog

1. QFileDialog::getOpenFileName and a series of functions are used to get the name or path of a file.
It is worth mentioning that QFileDialog::getOpenFileUrl, which returns a URL that, if converted to QString using [toString], will have more files:// in front of the disk path that should be caused by the win file system. Learn more about the operating system later and come back later, to get the disk path, use [toLocalFile].

2. QFileDialog::getOpenFileNames and a series of functions are used to get the names or paths of multiple files.
Similarly, this has a function to get paths, [QFileDialog::getOpenFileUrls], but it returns the list of QUrl s directly, so it can only be traversed individually and then converted as described above.

3. QFileDialog::getSaveFileName and a series of functions to get the name or path to save the file,
Similarly, it also has a function to get a path, [QFileDialog::getSaveFileUrl], which returns a QUrl object that can be used to get a QString object from [1].

4. QFileDialog::getExistingDirectory() is a function that really doesn't see what's useful. Whether I set the path or opened it somewhere else is a pity.I don't know if it is due to the modification of win10 mechanism, so wait!

QColorDialog

QColorDialog::getColor This function is mainly used to select the colors inside the palette and return the colors selected by the user (QColor object)

QFontDialog

QFontDialog::getFont This function is mainly used to select fonts and return user-selected fonts (QFont)

***QInputDialog***

QInputDialog::getText This function is mainly used to return the user's input text in the window, personal feeling is not very useful

Specific test code please see the code I uploaded, write a bit rotten, inadequacies please give more advice.

Note: A GroupBox is a Dialog, and each button is a function.

===============Supplementary================

I'm wrong. The code can't be uploaded. Paste it here:

header file
#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 file
#include <QDir>
#include <QFont>
#include <sstream>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    m_open_file = new QPushButton;
    m_open_file->setText("Open File    ");

    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("Open a set of files");

    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("Open an existing directory");

    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("Save file to  ");

    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("Please select a color  ");

    m_font_btn = new QPushButton;
    m_font_btn->setText("Please select a font  ");

    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("  Get Input  ");

    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 = "Open File";
    QString filter="text file(*.txt);;Picture File(*.jpg *.gif *.png);;All Files(*.*);;";
    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 = "Open File";
    QString filter="text file(*.txt);;Picture File(*.jpg *.gif *.png);;All Files(*.*);;";
    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 = "Open File";
    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 = "Save File";
    QString filter="text file(*.txt);;C++file(.cpp);;All Files(*.*);;";  //Remember here that if you have a column filter file type, you need two;
    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, "Choose Color");
    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="Input Text Dialog";
    QString txtLabel="Please enter a file name";
    QString defaultInput="new file.txt";
    QString strText = QInputDialog::getText(this, dlgTitle, txtLabel, QLineEdit::Normal, defaultInput, &ok);
    if(ok)
    {
        m_input_edit->setText(strText);
    }
}

Topics: C++ Qt