Qt中文件对话框,颜色对话框,字体对话框

1.头文件

#pragma once

#include 
#include "ui_WidgetTest.h"

#include 
#include 
#include 
#include 
#include 
#include 

class WidgetTest : public QWidget
{
    Q_OBJECT

public:
    WidgetTest(QWidget *parent = Q_NULLPTR);

private:
    Ui::WidgetTestClass ui;

    QLineEdit* m_pLineEidt;
    QPushButton* m_pPushButton1;
    QPushButton* m_pPushButton2;
    QPushButton* m_pPushButton3;
    QFrame*  m_pFrame;

private slots:
    void showFile();
    void showColor();
    void showFont();
};

2.源文件

#include "WidgetTest.h"

#include 

WidgetTest::WidgetTest(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    //文件对话框
    m_pLineEidt = new QLineEdit(this);
    m_pPushButton1 = new QPushButton(this);
    connect(m_pPushButton1, SIGNAL(clicked()), this, SLOT(showFile()));

    //颜色对话框
    m_pPushButton2 = new QPushButton(this);
    m_pFrame = new QFrame;
    m_pFrame->setFrameShape(QFrame::Panel);
    m_pFrame->setAutoFillBackground(true);  //填充背景
    connect(m_pPushButton2, SIGNAL(clicked()), this, SLOT(showColor()));

    //文字对话框
    m_pPushButton3 = new QPushButton(this);
    connect(m_pPushButton3, SIGNAL(clicked()), this, SLOT(showFont()));

    //布局
    QHBoxLayout* pLayout = new QHBoxLayout(this);
    pLayout->addWidget(m_pPushButton1);
    pLayout->addWidget(m_pLineEidt);
    pLayout->addWidget(m_pPushButton2);
    pLayout->addWidget(m_pFrame);
    pLayout->addWidget(m_pPushButton3);
}

void WidgetTest::showFile()
{
    QString str = QFileDialog::getOpenFileName(this, "open", "/", "c++ files(*.cpp)::c files(*.c)::head files(*.h)");
    m_pLineEidt->setText(str);
}

void WidgetTest::showColor()
{
    QColor color = QColorDialog::getColor(Qt::blue);
    if (color.isValid()) {
        m_pFrame->setPalette(QPalette(color));
    }
}


void WidgetTest::showFont()
{
    bool bOk;
    QFont font = QFontDialog::getFont(&bOk);
    if (bOk){
        m_pLineEidt->setFont(font);
    }
}

你可能感兴趣的:(Qt中文件对话框,颜色对话框,字体对话框)