setFixedSize(640,480);//设置窗口界面大小
//菜单栏
newaction1 = new QAction(QIcon("newfile.png"),"新建",this);//action的图标,名字,父类
newaction1->setShortcut(QKeySequence("Ctrl+N"));//设置快捷键
newaction2 = new QAction(QIcon("openfile.png"),"打开",this);
newaction2->setShortcut(QKeySequence("Ctrl+O"));
newaction3 = new QAction(QIcon("savefile.png"),"保存",this);
newaction3->setShortcut(QKeySequence("Ctrl+S"));
newaction4 = new QAction(QIcon("closefile.png"),"关闭",this);
newaction4->setShortcut(QKeySequence("Ctrl+Q"));
newaction5 = new QAction(QIcon("osave.png"),"另存为",this);
newaction5->setShortcut(QKeySequence("Ctrl+E"));
menu1 = menuBar()->addMenu("文件");//新建一个文件菜单
menu1->addAction(newaction1);
menu1->addAction(newaction2);
menu1->addAction(newaction3);
menu1->addAction(newaction4);
menu1->addAction(newaction5);
newaction6 = new QAction(QIcon("font.png"),"字体",this);
newaction7 = new QAction(QIcon("color.png"),"字体颜色",this);
menu2 = menuBar()->addMenu("设置");
menu2->addAction(newaction6);
menu2->addAction(newaction7);
//工具栏
tob1 = addToolBar("新建");//里面添加任意字符串
tob1->addAction(newaction1);
tob2 = addToolBar("打开");
tob2->addAction(newaction2);
tob3 = addToolBar("保存");
tob3->addAction(newaction5);
tob5 = addToolBar("另存为");
tob5->addAction(newaction9);
tob4 = addToolBar("关闭");
tob4->addAction(newaction6);
tob6 = addToolBar("字体");
tob6->addAction(newaction7);
tob7 = addToolBar("字体颜色");
tob7->addAction(newaction8);
//文本编辑框
tedt = new QTextEdit(this);
this->setCentralWidget(tedt);//把文本编辑框添加到中间
tedt->setEnabled(false);//文本编辑使能:关闭
//状态栏
// statusBar()->showMessage("状态");
lab = new QLabel("状态",this);
lab->setText("状态");
stb = statusBar();
stb->addWidget(lab);
connect(newaction1,SIGNAL(triggered()),this,SLOT(newfile()));
connect(newaction2,SIGNAL(triggered()),this,SLOT(openfile()));
connect(newaction3,SIGNAL(triggered()),this,SLOT(savefile()));
connect(newaction4,SIGNAL(triggered()),this,SLOT(closefile()));
connect(newaction6,SIGNAL(triggered()),this,SLOT(font()));
connect(newaction7,SIGNAL(triggered()),this,SLOT(fontcolor()));
connect(newaction5,SIGNAL(triggered()),this,SLOT(osavefile()));
void MainWindow:: newfile()
{
tedt->setEnabled(true); //文本编辑使能:开
}
void MainWindow:: openfile()
{
myfilename = QFileDialog::getOpenFileNames(this,"文件",".","Text files (*.txt);;XML files (*.xml)");//打开当前目录中的文件
QFile file(myfilename[0]);
if (!file.open(QIODevice::ReadWrite))
{
statusBar()->showMessage("打开失败");
return ;
}
statusBar()->showMessage("打开成功");
tedt->setText(file.readAll());//把文件中的内容显示在文本框
tedt->setEnabled(true); //文本编辑使能:开
file.close();
}
void MainWindow:: savefile()
{
QFile file1(myfilename[0]);
if (!file1.open(QIODevice::ReadWrite))
{
statusBar()->showMessage("保存失败");
return ;
}
if(!file1.write(tedt->toPlainText().toStdString().c_str()))
{
statusBar()->showMessage("保存失败");
return ;
}
statusBar()->showMessage("保存成功");
file1.close();
}
void MainWindow:: osavefile()
{
myfile = QFileDialog::getSaveFileName();
QFile file(myfile);
if (! file.open(QIODevice::ReadWrite)){
statusBar()->showMessage("文件打开失败");
return ;
}
if(!file.write(tedt->toPlainText().toStdString().c_str()))
{
statusBar()->showMessage("另存失败");
return ;
}
statusBar()->showMessage("另存成功");
file.close();
}
void MainWindow:: closefile()
{
tedt->setText("");
tedt->setEnabled(false); //文本编辑使能:关
}
void MainWindow:: font()
{
bool ok;
QFont myfont = QFontDialog::getFont(&ok,this);
if(ok)
{
tedt->setFont(myfont);
}
}
void MainWindow:: fontcolor()
{
QColor mycolor = QColorDialog::getColor();
tedt->setTextColor(mycolor);
}
MainWindow::~MainWindow()
{
}