Qt对话框上

文件对话框

头文件

private slots:
    void ShowFileDlg();

private:
    QPushButton *m_fileBtn;    //打开文件对话框
    QLineEdit *m_fileLineEdit; //显示所选文件路径

    QGridLayout *m_mainLayout; //布局管理器

dialog.cpp

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("标准对话框示例:");

    m_fileBtn = new QPushButton("文件标准对话框");
    m_fileLineEdit = new QLineEdit();

    m_mainLayout = new QGridLayout(this);
    m_mainLayout->addWidget(m_fileBtn,0,0);
    m_mainLayout->addWidget(m_fileLineEdit,0,1);

    connect(m_fileBtn,&QPushButton::clicked,
            this,&Dialog::ShowFileDlg);

}



void Dialog::ShowFileDlg()
{
   QString sPath =  QFileDialog::getOpenFileName(this,
                                                 "标准对话框",
                                                 ".",
                                                 "c++ files(*.cpp);;"
                                                 "c files(*.c);;"
                                                 "Header files(*.h)");
   m_fileLineEdit->setText(sPath);


}

 

颜色对话框

头文件

private slots:
    void ShowColorDig();      //函数声明
 private:

     QPushButton *m_colorBtn;    //打开颜色对话框
     QFrame * m_colorFrame;      //显示所选颜色效果

dialog.cpp 

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("标准对话框示例:");


    m_colorBtn = new QPushButton("颜色标准对话框");
    m_colorFrame = new QFrame;

    //设置边框的风格
    m_colorFrame->setFrameStyle(QFrame::Box);
    //可以自动填充背景
    m_colorFrame->setAutoFillBackground(true);

    m_mainLayout->addWidget(m_colorBtn,1,0);
    m_mainLayout->addWidget(m_colorFrame,1,1);

    //槽函数连接
    connect((m_colorBtn,&QPushButton::clicked,
             this,&Dialog::ShowColorDig);


void Dialog::ShowColorDig()
{
    //获取颜色
    QColor color = QColorDialog::getColor(Qt::yellow);
    if(color.isValid())
        //颜色填充
        m_colorFrame->setPalette(QPalette(color));
}
}

字体对话框 

头文件

private slots:
    void ShowFontDlg();
private:
    QPushButton *m_fontBtn;       //打开字体对话框
    QLineEdit *m_fontLineEdit;    //显示所选字体的效果

dialog.cpp 

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("标准对话框示例:");
    //字体对话框
    m_fontBtn = new QPushButton("字体标准对话框");
    m_fontLineEdit = new QLineEdit("胃,你好吗?");

    //字体布局
    m_mainLayout->addWidget(m_fontBtn,2,0);
    m_mainLayout->addWidget(m_fontLineEdit,2,1);

   connect(m_fontBtn,&QPushButton::clicked,
            this,&Dialog::ShowFontDlg);

}

void Dialog::ShowFontDlg()
{
    //获取字体
    bool ok;
    QFont font = QFontDialog::getFont(&ok);
    if(ok) m_fontLineEdit->setFont(font);
}

你可能感兴趣的:(Qt项目学习,qt,开发语言,c++)