《学习Qt之路2》笔记:Qt之文本对话框

 
 
QString str; str = "%1 %2"; str.arg("%1f", "Hello"); 
// returns "%1f Hello"

解释下tr(%1).arg(parameter)的含义。


这里学习QTextEdit  与QFileDialog 与QTextStream的相关问题。

代码如下:

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"
#include <QIcon>
#include <QAction>
#include <QMenuBar>
#include<QToolBar>
#include <QStatusBar>
#include <QMessageBox>
#include <QTextEdit>
#include <QFileDialog>
#include <QTextStream>

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	MainWindow(QWidget *parent = 0);
	~MainWindow();

private:
	Ui::MainWindowClass ui;

	void openFile();
	void saveFile();

	QAction *openAction;
	QAction *saveAction;
	QTextEdit *textEdit;
	
};

#endif // MAINWINDOW_H

//mainwindow.cpp
#include "mainwindow.h"

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

	setWindowTitle(tr("Main Window"));

	openAction = new QAction(QIcon(""),tr("&Open"),this);
	openAction->setShortcuts(QKeySequence::Open);
	openAction->setStatusTip("Open an existing file");

	saveAction = new QAction(QIcon(""),tr("&Save"),this);
	saveAction->setShortcuts(QKeySequence::Save);
	saveAction->setStatusTip("Save a new file");

	connect(openAction,&QAction::triggered,this,&MainWindow::openFile);
	connect(saveAction,&QAction::triggered,this,&MainWindow::saveFile);

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

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

	QStatusBar *statuBar = statusBar();
	statuBar->addAction(openAction);
	statuBar->addAction(saveAction);

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

MainWindow::~MainWindow()
{

}

void MainWindow::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("Cannot open file:\n%1").arg(path));
		   return;
		}
		QTextStream in(&file);
		textEdit->setText(in.readAll());
		file.close();

	}
	else
	{
		QMessageBox::warning(this,tr("Path"),tr("You did not select any file."));
	}
}


void MainWindow::saveFile()
{
	QString path = QFileDialog::getSaveFileName(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("Write File"),tr("Cannot open file:\n%1").arg(path));
			return;
		}
		QTextStream out(&file);
		out<<textEdit->toPlainText();
		file.close();
	}
	else
	{
		QMessageBox::warning(this,tr("Path"),tr("You did not select any file."));
	}
}

当textEdit内容改变时,在退出时弹出提示。

//mainwindow.h
protected:
	void closeEvent(QCloseEvent *event);
//mainwindow.cpp
textEdit = new QTextEdit(this);  
	setCentralWidget(textEdit);  
	connect(textEdit, &QTextEdit::textChanged, [=](){this->setWindowModified(true);});

void MainWindow::closeEvent(QCloseEvent *event)
{
	if (isWindowModified())
	{
		bool exit = QMessageBox::question(this,tr("Quit"),tr("Are you sure to quit this application?"),
			QMessageBox::Yes | QMessageBox::No,
			QMessageBox::No) == QMessageBox::Yes;
		if(exit)
		{
			event->accept();
		}
		else
		{
			event->ignore();
		}
	}
	else
	{
		event->accept();
	}

}


你可能感兴趣的:(《学习Qt之路2》笔记:Qt之文本对话框)