#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
#include <qdebug.h>
#include <qtextcodec.h>
#include <qsettings.h>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//透明设置
setWindowOpacity(0.6);
//读取坐标值
int X,Y;
QString stringX;
QString stringY;
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QFile file1("坐标.txt");
if(!file1.open(QIODevice::ReadOnly|QIODevice::Text))
return;
while(!file1.atEnd())
{
//读取一行文本数据
QByteArray line = file1.readAll();
//将读取到的行数据转换为Unicode
QString str =codec->toUnicode(line);
//将得到的字符设置到编辑框中显示
char* ptr;
QByteArray ba;
ba=str.toLatin1();
ptr=ba.data();
QString value="";
bool bol=true;
for (int i = 1; i < strlen(ptr); i++) {
value=ptr[i];
if(value=="Y"){
bol=false;
}
else{
if(bol)
{
stringX+=value;
}
else{
stringY+=ptr[i];
}
}
}
qDebug()<<stringX;
qDebug()<<stringY;
//转换并输出成int值
X=stringX.toInt();
Y=stringY.toInt();
// qDebug()<<str;
qDebug()<<X;
qDebug()<<Y;
}
//添加窗口值
window()->move(X,Y);//窗口位置
//读取笔记内容并显示
QFile file("翻译笔记.txt");
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
return;
while(!file.atEnd())
{
//读取一行文本数据
QByteArray line = file.readAll();
//将读取到的行数据转换为Unicode
QString str =codec->toUnicode(line);
//将得到的字符设置到编辑框中显示
ui->plainTextEdit->setPlainText(str);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOK_triggered()
{
//保存笔记
QFile file("翻译笔记.txt");
if(!file.open(QIODevice::WriteOnly|QIODevice::Text)){
QMessageBox::warning(this,QStringLiteral("警告"), file.errorString());
return;
}
QTextStream out(&file);
out.setCodec("utf-8");
QString str=ui->plainTextEdit->toPlainText();
out<<str;
file.close();
}
void MainWindow::closeEvent( QCloseEvent * event )
{
//获取本项目坐标并存储到文件夹中
QPoint pos = QCursor::pos();
QFile file("坐标.txt");
if(!file.open(QIODevice::WriteOnly|QIODevice::Text)){
QMessageBox::warning(this,QStringLiteral("警告"), file.errorString());
return;
}
QTextStream out(&file);
out.setCodec("utf-8");
int x=window()->pos().x();
int y=window()->pos().y();
QString ab="X";
ab+= QString::number(x);
ab+="Y";
ab+=QString::number(y);
qDebug()<<ab;
out<<ab;
file.close();
//关闭窗口时 再次确定是否关闭
switch( QMessageBox::information( this, tr("exit tip"), tr("Do you really want exit?"), tr("Yes"), tr("No"), 0, 1 ) )
{
case 0:
event->accept();
break;
case 1:
default:
event->ignore();
break;
}
}