使用C++ 與QT 完成山寨版金山詞霸!!!
#ifndef _CLASS_H_
#define _CLASS_H_
#include <QApplication>
#include <QtGui>
void displaynewword(QTableWidget* tableWidget2);
class Mywin2:public QWidget //編輯對話框
{
Q_OBJECT
private :
QLabel * label1;
QLabel * label2;
QLineEdit* editName2;
QTextEdit* textedit2;
QPushButton* btnresult10;
QPushButton* btnresult11;
public :
Mywin2();
private slots:
void myslot8(const QString&,const QString&);
void myslot9(); //發送修改后的單詞和解釋
signals:
void mysig2(const QString&,const QString&);
};
class Mywin1:public QWidget //生詞表對話框的類
{
Q_OBJECT
private:
QPushButton* btnresult5;
QPushButton* btnresult6;
QPushButton* btnresult7;
QPushButton* btnresult8;
QPushButton* btnresult9;
QTableWidget* tableWidget1;
Mywin2 q2;
public :
Mywin1();
private slots:
void myslot4(QTableWidget*);
void myslot3();//刷新顯示生詞表
void myslot5();//刪除選中的行
void myslot6();//保存顯示中的生詞表
void myslot7();
void myslot10(const QString&,const QString&);
signals:
void mysig(QTableWidget*);
void mysig1(const QString&,const QString&);
};
class Mywin:public QWidget
{
Q_OBJECT
private :
QLineEdit* editName1;
QPushButton* btnresult1;
QPushButton* btnresult2;
QPushButton* btnresult3;
QPushButton* btnresult4;
QTextEdit* textedit1;
Mywin1 q1;
public :
Mywin();
private slots:
void myslot(const QString &);
void myslot1();
void myslot2();
};
#endif
#include <QApplication>
#include <QtGui>
#include "class.h"
Mywin::Mywin()
{
QIcon a("zidian.bmp");
editName1 = new QLineEdit;
btnresult1 = new QPushButton("查詞(F)");
btnresult2 = new QPushButton("加入生詞本(A)");
btnresult3 = new QPushButton("生詞本(U)");
btnresult4 = new QPushButton("退出(Q)");
textedit1 = new QTextEdit;
QHBoxLayout* hlayout1 = new QHBoxLayout;
hlayout1->addWidget(editName1);
hlayout1->addWidget(btnresult1);
QHBoxLayout* hlayout2 = new QHBoxLayout;
hlayout2->addWidget(textedit1);
QHBoxLayout* hlayout3 = new QHBoxLayout;
hlayout3->addWidget(btnresult2);
hlayout3->addWidget(btnresult3);
hlayout3->addWidget(btnresult4);
QVBoxLayout* layout = new QVBoxLayout;
layout->addLayout(hlayout1);
layout->addLayout(hlayout2);
layout->addLayout(hlayout3);
this->setLayout(layout);
setWindowTitle("我的詞典");
setWindowIcon(a);
connect(btnresult4,SIGNAL(clicked()),this,SLOT(close()));
connect(editName1,SIGNAL(textChanged(const QString &)),this,SLOT(myslot(const QString &)));
connect(btnresult2,SIGNAL(clicked()),this,SLOT(myslot1()));
connect(btnresult3,SIGNAL(clicked()),this,SLOT(myslot2()));
}
Mywin1::Mywin1()
{
QIcon b("tupian2.bmp");
btnresult5 = new QPushButton("刷新(R)");
btnresult6 = new QPushButton("刪除(D)");
btnresult7 = new QPushButton("編輯(E)");
btnresult8 = new QPushButton("退出(Q)");
btnresult9 = new QPushButton("保存(S)");
tableWidget1 = new QTableWidget;
tableWidget1->setRowCount(10);
tableWidget1->setColumnCount(3);
tableWidget1->setColumnWidth(0,80);
tableWidget1->setColumnWidth(1,300);
tableWidget1->setColumnWidth(2,80);
QStringList fonts;
fonts << "單詞" << "詞語解釋" << "添加時間" ;
tableWidget1->setHorizontalHeaderLabels(fonts);
displaynewword(tableWidget1);//將生詞表里的生詞顯示在生詞框里
QHBoxLayout* hlayout5 = new QHBoxLayout;
hlayout5->addWidget(tableWidget1);
QHBoxLayout* hlayout4 = new QHBoxLayout;
hlayout4->addWidget(btnresult5);
hlayout4->addWidget(btnresult6);
hlayout4->addWidget(btnresult7);
hlayout4->addWidget(btnresult9);
hlayout4->addWidget(btnresult8);
QVBoxLayout* layout1 = new QVBoxLayout;
layout1->addLayout(hlayout5);
layout1->addLayout(hlayout4);
this->setLayout(layout1);
this->setMaximumWidth(520);
this->setMinimumWidth(520);
this->setMinimumHeight(300);
this->setMaximumHeight(300);
setWindowTitle("生詞表");
setWindowIcon(b);
connect(btnresult8,SIGNAL(clicked()),this,SLOT(close()));
connect(btnresult5,SIGNAL(clicked()),this,SLOT(myslot3()));
connect(this,SIGNAL(mysig(QTableWidget*)),this,SLOT(myslot4(QTableWidget*)));
connect(btnresult6,SIGNAL(clicked()),this,SLOT(myslot5()));
connect(btnresult9,SIGNAL(clicked()),this,SLOT(myslot6()));
connect(btnresult7,SIGNAL(clicked()),this,SLOT(myslot7()));
connect(this,SIGNAL(mysig1(const QString&,const QString&)),&q2,SLOT(myslot8(const QString&,const QString&)));
connect(&q2,SIGNAL(mysig2(const QString&,const QString&)),this,SLOT(myslot10(const QString&,const QString&)));
}
Mywin2::Mywin2()
{
QIcon c("tupian3.bmp");
editName2 = new QLineEdit;
textedit2 = new QTextEdit;
label1 = new QLabel("單詞");
label2 = new QLabel("解釋");
btnresult10 = new QPushButton("確定(S)");
btnresult11 = new QPushButton("退出(Q)");
QHBoxLayout* hlayout8 = new QHBoxLayout;
hlayout8->addWidget(label1);
QHBoxLayout* hlayout6 = new QHBoxLayout;
hlayout6->addWidget(editName2);
QHBoxLayout* hlayout9 = new QHBoxLayout;
hlayout9->addWidget(label2);
QHBoxLayout* hlayout7 = new QHBoxLayout;
hlayout7->addWidget(textedit2);
QHBoxLayout* hlayout10 = new QHBoxLayout;
hlayout10->addWidget(btnresult10);
hlayout10->addWidget(btnresult11);
QVBoxLayout* layout2 = new QVBoxLayout;
layout2->addLayout(hlayout8);
layout2->addLayout(hlayout6);
layout2->addLayout(hlayout9);
layout2->addLayout(hlayout7);
layout2->addLayout(hlayout10);
this->setLayout(layout2);
setWindowTitle("編輯");
setWindowIcon(c);
connect(btnresult11,SIGNAL(clicked()),this,SLOT(close()));
connect(btnresult10,SIGNAL(clicked()),this,SLOT(myslot9()));
}
void Mywin2::myslot9()
{
QString str1,str2;
str1 = editName2->text();
str2 = textedit2->toPlainText();
emit mysig2(str1,str2);
}
void Mywin1::myslot10(const QString& str1,const QString& str2)
{
int heng ;
QString newline;
QTableWidgetItem *newItem1 ;
heng = tableWidget1->currentRow ();
newItem1 = new QTableWidgetItem(str1);
tableWidget1->setItem(heng, 0, newItem1);
newItem1 = new QTableWidgetItem(str2);
tableWidget1->setItem(heng, 1, newItem1);
QDate newdate = QDate::currentDate ();
newline = newdate.toString ("dd-MM-yy");
newItem1 = new QTableWidgetItem(newline);
tableWidget1->setItem(heng, 2, newItem1);
}
void Mywin1::myslot7()
{
QList<QTableWidgetItem*> ql =tableWidget1->selectedItems();
if(ql.empty())
{
QMessageBox msgBox;
msgBox.setText("您還沒有選中內容!");
msgBox.exec();
}
else{
q2.show();
QTableWidgetItem *newItem;
newItem = tableWidget1->item(tableWidget1->currentRow(),0);
QString str1 = newItem->text();
newItem = tableWidget1->item(tableWidget1->currentRow(),1);
QString str2 = newItem->text();
emit mysig1(str1,str2);
}
}
void Mywin2::myslot8(const QString& str1,const QString& str2)
{
editName2->setText(str1);
textedit2->setPlainText(str2);
}
void Mywin1::myslot6()
{
int i,j;
QTableWidgetItem *newItem;
QFile unword3("newword.txt");
if(unword3.open(QFile::WriteOnly | QFile::Truncate))
{
QTextStream out(&unword3);
for(i = 0;i < tableWidget1->rowCount();i++)
for(j = 0;j<3;j++)
{
newItem = tableWidget1->item(i,j);
out<<newItem->text()<<'\n';
}
}
unword3.close();
QMessageBox msgBox;
msgBox.setText("保存成功!");
msgBox.exec();
}
void Mywin1::myslot5()
{
int line = tableWidget1->rowCount();
if(line == 0)
{
QMessageBox box(QMessageBox::Warning,"警告","生詞本已經是空的!");
box.exec();
return;
}
tableWidget1->setSelectionMode(QAbstractItemView::ExtendedSelection);
int row = tableWidget1->currentItem()->row();
tableWidget1->removeRow(row);
}
void displaynewword(QTableWidget* tableWidget2)
{
QTableWidgetItem *newItem1 ;
QFile unword3("newword.txt");
if (unword3.open(QFile::ReadWrite ))
{
int row = 1;
QTextStream in(&unword3);
QString line3;
line3 = in.readLine();
while(!line3.isNull())
{
tableWidget2->setRowCount(row);
newItem1 = new QTableWidgetItem(line3);
tableWidget2->setItem(row - 1, 0, newItem1);
line3 = in.readLine();
newItem1 = new QTableWidgetItem(line3);
tableWidget2->setItem(row - 1, 1, newItem1);
line3 = in.readLine();
newItem1 = new QTableWidgetItem(line3);
tableWidget2->setItem(row - 1, 2, newItem1);
line3 = in.readLine();
row ++;
}
}
unword3.close();
}
void Mywin1::myslot4(QTableWidget*tableWidget1) //刷新的函數
{
displaynewword(tableWidget1);//上面的顯示函數
}
void Mywin1::myslot3()//發射信號函數
{
emit mysig(tableWidget1);
}
void Mywin::myslot2()//槽函數產生生詞對話框
{
q1.show();
}
void Mywin::myslot1()
{
int flag = 1,ret;
QMessageBox msgBox,msgBox1;
msgBox1.setText("save succeeful!");
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save ?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No );
QFile unword("newword.txt");
if (unword.open(QFile::ReadWrite ))
{
QTextStream out(&unword);
QString line,line1;
line = out.readLine();
while(!line.isNull())
{
if(line == (editName1->text()))
{
ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Yes:flag = 0;
// flag = 0 是覆蓋
break;
case QMessageBox::No: flag = 2;
// flag = 2 是不覆蓋也不寫入
break;
}
break;
}
line = out.readLine();
}
unword.close();
}
if(flag == 1)
{
//textedit1->setPlainText("ok!");
QFile unword("newword.txt");
if (unword.open(QFile::WriteOnly|QFile::Append))
{
QTextStream out(&unword);
out << editName1->text() << '\n';
out << textedit1->toPlainText() << '\n';
QDate newdate = QDate::currentDate ();
QString newline;
newline = newdate.toString ("dd-MM-yy");
out << newline << endl;
} //在生詞表中沒有找到這個新的生詞保存的程序
unword.close();
msgBox1.exec();
}
if(flag == 0)
{
QString newline,newline1;
int Index = 0,start = 0,count = 0;
QFile unword("newword.txt");
if(unword.open(QFile::ReadOnly))
{
QTextStream out(&unword);
newline = out.readAll();
}
unword.close();
newline1 = editName1->text();
Index = newline.indexOf(newline1);
start = Index;
while(Index != -1)
{
Index = newline.indexOf('\n',Index +1 );
count ++;
if(count == 3)
{
break;
}
}
newline.remove(start,Index + 1);
QFile unword1("newword.txt");
if(unword1.open(QFile::WriteOnly))
{
QTextStream out(&unword1);
out << newline ;
}
unword1.close();
QFile unword2("newword.txt");
if (unword2.open(QFile::WriteOnly|QFile::Append))
{
QTextStream out(&unword2);
out << editName1->text() << '\n';
out << textedit1->toPlainText() << '\n';
QDate newdate1 = QDate::currentDate ();
QString newline2;
newline2 = newdate1.toString ("dd-MM-yy");
out << newline2 << endl;
} //生詞保存的程序
unword2.close();
msgBox1.exec();//回應覆蓋成功
}
}
void Mywin::myslot(const QString &)
{
int flag = 1;
QFile data("word1.txt");
if (data.open(QFile::ReadOnly))
{
QTextStream out(&data);
QString line,line1;
line = out.readLine();
while(!line.isNull())
{
if(line == (editName1->text()))
{
line1 = out.readLine();
textedit1->setPlainText(line1);
flag = 0;
break;
}
line = out.readLine();
}
}
if(flag == 1)
{
textedit1->setPlainText("未找到此單詞!");
}
data.close();
}
int main(int argc,char **argv)
{
QApplication app(argc,argv);
QTextCodec * codec = QTextCodec::codecForName("gbk");
QTextCodec::setCodecForCStrings(codec);
Mywin q;
q.show();
return app.exec();
}
|