《ASCE1885的Qt学习记录》の标准对话框

本文拟就Qt提供的标准对话框函数:QFileDialogQColorDialogQFontDialog的使用方法作一实例进行说明。本实例是在Windows7+Qt v4.7.0+QtCreator 2.0.0环境下编译运行的,运行效果如图:

《ASCE1885的Qt学习记录》の标准对话框

单击“文件对话框”按钮,会弹出文件选择对话框,选中的文件名将显示在右边;单击“颜色对话框”按钮,会弹出颜色选择对话框,选中的颜色将显示在右边;单击“字体对话框”按钮,将弹出字体选择对话框,选中的字体将更新右边显示的字符串。

1QFileDialog类提供了四个静态函数,方便用户定制自己的文件对话框:

getOpenFileName函数用于获得用户选择的文件名,原型如下:

QString getOpenFileName ( QWidget * parent = 0, const QString & caption = QString(),

const QString & dir = QString(), const QString & filter = QString(),

QString * selectedFilter = 0, Options options = 0 )

这个函数下面例子会用到,这里着重介绍一下:dir参数指定了默认的目录,如果dir参数带有文件名,则该文件将是默认选中的文件;filter参数对文件类型进行过滤,只有与过滤器匹配的文件类型才显示,filter可以同时指定多种过滤方式供用户选择,多种过滤器之间用“;;”隔开,用户选择的过滤器通过参数selectedFilter返回。

getSavedFileName函数用于获取用户保存的文件名,原型如下:

QString getSaveFileName ( QWidget * parent = 0, const QString & caption = QString(),

const QString & dir = QString(), const QString & filter = QString(),

QString * selectedFilter = 0, Options options = 0 )

getExistingDirectory函数用于获取用户选择的已存在目录名,原型如下:

QString getExistingDirectory ( QWidget * parent = 0, const QString & caption = QString(),

const QString & dir = QString(), Options options = ShowDirsOnly )

getOpenFileNames函数用于获取用户选择的文件名列表,原型如下:

QStringList getOpenFileNames ( QWidget * parent = 0, const QString & caption = QString(),

const QString & dir = QString(), const QString & filter = QString(),

QString * selectedFilter = 0, Options options = 0 )

标准文件选择对话框如下:

《ASCE1885的Qt学习记录》の标准对话框

2QColorDialog类提供了getColor()静态函数用户返回用户选择的颜色值,原型如下:

QColor getColor ( const QColor & initial = Qt::white, QWidget * parent = 0 )

其中,initial参数指定了默认选中的颜色,默认是白色;通过QColor::isValid()函数可以判断用户选择颜色是否有效,若用户选择取消,QColor::isValid()将返回false

标准颜色选择对话框如下:

《ASCE1885的Qt学习记录》の标准对话框

3QFontDialog类提供了静态函数getFont()用于返回用户选择的字体,原型如下:

QFont getFont ( bool * ok, QWidget * parent = 0 )

当用户点击OK按钮,参数*ok返回true,函数返回用户选择的字体;否则*okfalse,函数返回默认字体。

标准字体选择对话框如下:

《ASCE1885的Qt学习记录》の标准对话框

具体代码如下:

=======================头文件standarddialog.h===================

#ifndef STANDARDDIALOGS_H

#define STANDARDDIALOGS_H

#include <QtCore>

#include <QtGui/QDialog>

class QGridLayout;

class QWidget;

class QPushButton;

class QLineEdit;

class QFrame;

class StandardDialogs : public QDialog

{

Q_OBJECT

public:

StandardDialogs(QWidget *parent=0, Qt::WindowFlags f=0);

~StandardDialogs();

public:

QGridLayout *layout;

QPushButton *filePushButton;

QPushButton *colorPushButton;

QPushButton *fontPushButton;

QLineEdit *fileLineEdit;

QLineEdit *fontLineEdit;

QFrame *colorFrame;

private slots:

void slotOpenFileDlg();

void slotOpenColorDlg();

void slotOpenFontDlg();

};

#endif // STANDARDDIALOGS_H

======================源文件standarddialog.cpp===================

#include "standarddialogs.h"

#include <QtGui/QGridLayout>

#include <QtGui/QLineEdit>

#include <QtGui/QFrame>

#include <QtGui/QFileDialog>

#include <QtGui/QColorDialog>

#include <QtGui/QFontDialog>

#include <QtGui/QColor>

#include <QtGui/QPushButton>

StandardDialogs::StandardDialogs(QWidget *parent, Qt::WindowFlags f)

: QDialog(parent, f)

{

setWindowTitle(tr("ASCE Standard Dialogs"));

layout = new QGridLayout(this);

filePushButton = new QPushButton;

filePushButton->setText(QString::fromLocal8Bit("文件对话框"));

colorPushButton = new QPushButton;

colorPushButton->setText(QString::fromLocal8Bit("颜色对话框"));

fontPushButton = new QPushButton;

fontPushButton->setText(QString::fromLocal8Bit("字体对话框"));

fileLineEdit = new QLineEdit;

colorFrame = new QFrame;

colorFrame->setFrameShape(QFrame::Box);

colorFrame->setAutoFillBackground(true);

fontLineEdit = new QLineEdit;

fontLineEdit->setText(tr("ASCE1885"));

layout->addWidget(filePushButton, 0, 0);

layout->addWidget(fileLineEdit, 0, 1);

layout->addWidget(colorPushButton, 1, 0);

layout->addWidget(colorFrame, 1, 1);

layout->addWidget(fontPushButton, 2, 0);

layout->addWidget(fontLineEdit, 2, 1);

layout->setMargin(15);

layout->setSpacing(10);

connect(filePushButton, SIGNAL(clicked()), this, SLOT(slotOpenFileDlg()));

connect(colorPushButton, SIGNAL(clicked()), this, SLOT(slotOpenColorDlg()));

connect(fontPushButton, SIGNAL(clicked()), this, SLOT(slotOpenFontDlg()));

}

StandardDialogs::~StandardDialogs()

{

}

void StandardDialogs::slotOpenFileDlg()

{

QString str = QFileDialog::getOpenFileName(

this, "Open File Dialog",

"/", "C++ files(*.cpp);;C files(*.c);;Head files(*.h)");

fileLineEdit->setText(str.toAscii());

}

void StandardDialogs::slotOpenColorDlg()

{

QColor color = QColorDialog::getColor(Qt::blue);

if(color.isValid())

{

colorFrame->setPalette(QPalette(color));

}

}

void StandardDialogs::slotOpenFontDlg()

{

bool ok;

QFont font = QFontDialog::getFont(&ok);

if(ok)

{

fontLineEdit->setFont(font);

}

}

======================工程StandardDialogs.pro文件=================

#-------------------------------------------------

#

# Project created by QtCreator 2010-08-15T19:43:02

#

#-------------------------------------------------

QT += core

TARGET = StandardDialogs

CONFIG += windows

CONFIG -= app_bundle

TEMPLATE = app

LIBS += D:/Qt/4.7.0-beta2/lib/QtGui4.lib //注意,此处必须指明用到的静态库,否则链接出错

SOURCES += main.cpp /

standarddialogs.cpp

HEADERS += /

standarddialogs.h

你可能感兴趣的:(对话框)