QFileDialog

常用的打开,保存文件对话框:

#include 
#include 
#include 
#include 

DialogTest::DialogTest(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	ui.setupUi(this);
	/设置字符编码
	QTextCodec *codec = QTextCodec::codecForName("GBK");
	QTextCodec::setCodecForLocale(codec);
	QTextCodec::setCodecForCStrings(codec);
	QTextCodec::setCodecForTr(codec);      

	connect(ui.m_saveBtn,SIGNAL(clicked()),this,SLOT(saveDialog()));
	connect(ui.m_openBtn,SIGNAL(clicked()),this,SLOT(openDialog()));
}

DialogTest::~DialogTest()
{

}

void DialogTest::saveDialog()
{
	QString strBuf("write file\r\n成功");
	QString defaultFileName =tr("文件名%1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss"));
	QString fileName=QFileDialog::getSaveFileName(this,tr("保存文件"),defaultFileName,tr("文件类型(*.txt)"));
	if(fileName!=NULL)
	{
		QFile file(fileName);
		if(file.open(QFile::WriteOnly | QFile::Text))
		{
			QTextStream filestream(&file);
			int index=0;
			while(index


你可能感兴趣的:(Qt)