Qt学习---3

Qt对话框:

QDiglog(及其子类,以及所有Qt::Dialog类型的类)的对于其parent指针都有额外的解释:如果parent为NULL,则该对话框会作为一个顶层窗口,否则则作为其父组件的子窗口(此时,默认出现的位置是parent的中心)顶层窗口与非顶层窗口的区别在于,顶层窗口在任务栏会有自己的位置,而非顶层窗口则会共享其父组件的位置。

Qt支持模态对话框和非模态对话框。其中,Qt有两种级别的对话框:应用程序级别的模态和窗口级别的模态,默认是应用程序级别的模态。应用程序级别的模态是指,当这种模态的对话框出现时,用户必须首先对对话框进行交互,直到关闭对话框,然后才能访问程序其他的窗口。窗口级别的模态是指,该模态仅仅阻塞与对话框关联的窗口,但是仍然允许用户与其他窗口交互。窗口级别的模态适用于多窗口模式。

Qt使用QDialog::exec()实现程序级别的模态对话框,使用QDialog::open()实现窗口级别的模态对话框,使用QDialog::show()实现非模式对话框.setAttribute()函数设置对话框关闭时,自动销毁对话框。另外,QObject还有一个deleteLater()函数,该函数会在当前事件循环结束时自动销毁该对话框。

Qt标准对话框:(即Qt内置的一系列对话框,因为很多对话框都是通用的,比如打开文件,设置颜色,打印设置等,这些对话框在所有程序中基本相同,因此没有必要在每个程序中自己实现这么一个对话框)

QMesssageBox用于显示消息提示。我们一般会使用其提供的几个static函数。about,critical.information.question.warning.


QFileDialog:

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

QString *selectFliter = 0, QOption* options = 0)

#include "qlearn_filedialog.h"

QLearn_FileDialog::QLearn_FileDialog(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	openAction = new QAction(this);
	openAction->setShortcut(QKeySequence::Open);
	openAction->setStatusTip(tr("click here to open a file"));
	QObject::connect(openAction, &QAction::triggered, this, &QLearn_FileDialog::openFile);

	saveAction = new QAction(this);
	saveAction->setShortcut(QKeySequence::Save);
	saveAction->setStatusTip(tr("click here to save the file"));
	QObject::connect(saveAction, &QAction::triggered, this, &QLearn_FileDialog::saveFile);

	QMenu *file = menuBar()->addMenu(tr("&File"));
	file->addAction(openAction);
	file->addAction(saveAction);

	QToolBar *toolBar = new QToolBar(tr("&File"));
	toolBar->addAction(openAction);
	toolBar->addAction(saveAction);

	textEdit = new QTextEdit(this);
	setCentralWidget(textEdit);
}

QLearn_FileDialog::~QLearn_FileDialog()
{

}

void QLearn_FileDialog::openFile()
{
	QString path = QFileDialog::getOpenFileName(this, tr("open File"), ".", tr("Text Files(*.txt)"));
	if (!path.isEmpty())
	{
		QFile file(path);
		if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
			QMessageBox::warning(this, tr("Read File"), tr("Can not open file:\n%1").arg(path));
			return;
		}
		QTextStream in(&file);
		textEdit->setText(in.readAll());
		file.close();
	}else{
		QMessageBox::warning(this, tr("path"), tr("You didnt select any file"));
	}
}

void QLearn_FileDialog::saveFile()
{
	QString path = QFileDialog::getOpenFileName(this, tr("save File"), ".", tr("Text Files(*.txt)"));
	if (!path.isEmpty())
	{
		QFile file(path);
		if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
			QMessageBox::warning(this, tr("Open File"), tr("Can not open file:\n%1").arg(path));
			return;
		}
		QTextStream out(&file);
		out << textEdit->toPlainText();
		file.close();
	}else{
		QMessageBox::warning(this, tr("path"), tr("You didnt select any file"));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值