免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 752 | 回复: 0
打印 上一主题 下一主题

Source code of Notepad written by QT4 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-08 20:40 |只看该作者 |倒序浏览
#include "aboutme.h"
AboutDialog::AboutDialog(QWidget *parent):QDialog(parent)
{
about=new QLabel(tr("creatory@163.com"),this);
exit=new QPushButton(tr("&OK"),this);
hlayout=new QHBoxLayout(this);
hlayout->addWidget(about);
hlayout->addWidget(exit);
connect(exit,SIGNAL(clicked()),this,SLOT(accept()));
this->setWindowTitle(tr("About Me"));
}
#include
#include
#include
#include
class AboutDialog:public QDialog
{
public:
AboutDialog(QWidget *parent=0);
private:
QLabel  *about;
QPushButton *exit;
QHBoxLayout *hlayout;
};
#include
#include "notepad.h"
int main(int argc,char **argv)
{
QApplication app(argc,argv);
Notepad *note=new Notepad;
note->show();
return app.exec();
}
#include "notepad.h"
#include
#include
#include
#include
#include
#include
#include
Notepad::Notepad(QWidget *parent):QMainWindow(parent)
{
createMenus();
textEdit=new QTextEdit(this);
setCentralWidget(textEdit);
CHANGE=0;
connect(textEdit,SIGNAL(textChanged()),this,SLOT(hasChanged()));
}
void Notepad::createMenus()
{
//new Action
newAction=new QAction(tr("&New"),this);
newAction->setShortcut(tr("Ctrl+N"));
connect(newAction,SIGNAL(triggered()),this,SLOT(newFile()));
//open Action
openAction=new QAction(tr("&Open"),this);
openAction->setShortcut(tr("Ctrl+O"));
connect(openAction,SIGNAL(triggered()),this,SLOT(openFile()));
//save Action
saveAction=new QAction(tr("&Save"),this);
saveAction->setShortcut(tr("Ctrl+S"));
connect(saveAction,SIGNAL(triggered()),this,SLOT(saveFile()));
//selectallaction
selectAction=new QAction(tr("Select &All"),this);
selectAction->setShortcut(tr("Ctrl+A"));
connect(selectAction,SIGNAL(triggered()),this,SLOT(selectAll()));
//creat about me action
aboutAction=new QAction(tr("About &Me"),this);
aboutAction->setShortcut(tr("Ctrl+M"));
connect(aboutAction,SIGNAL(triggered()),this,SLOT(aboutMe()));
//create menu
fileMenu=menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
editMenu=menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(selectAction);
helpMenu=menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
}
void Notepad::newFile()
{
if(saveornot()==0)
{
textEdit->clear();
CHANGE=0;
}
}
void Notepad::openFile()
{
//open a file and read its all content line by line then added into textEdit
if(saveornot()==0)
{
fileName=QFileDialog::getOpenFileName(this,tr("OpenFile"),tr("/home"),tr("AllFiles(*)"));
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
return;
while(!file.atEnd())
{
QByteArray line=file.readLine();
textEdit->append(line);
}
file.close();
}
}
void Notepad::saveFile()
{
if(saveornot()==1)
{
fileName=QFileDialog::getSaveFileName(this,tr("SaveFile"),tr("/home"),tr("AllFiles(*.*)"));
QFile file(fileName);
if(!file.open(QFile::WriteOnly|QFile::Text))
return;
QTextStream out(&file);
out.setCodec("UTF-8");
outdocument();
//out.flush();
}
}
int Notepad::saveornot()
{
//if file need to be saved return 1,otherwise return 0
int ret;
if(CHANGE==1)
{
ret=QMessageBox::warning(this,tr("Warning"),tr("The document has been modified.\nSave or Not?"),QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save);
if(ret==QMessageBox::Save)
return 1;
}
return 0;
}
void Notepad::hasChanged()
{
//when the user modify the content change value to explain it has been modified
CHANGE=1;
}
void Notepad::selectAll()
{
textEdit->selectAll();
}
void Notepad::aboutMe()
{
about=new AboutDialog;
about->show();
}
#include
#include
#include
#include
#include "aboutme.h"
class Notepad:public QMainWindow
{
Q_OBJECT
public:
Notepad(QWidget *parent=0);
void createMenus();
int saveornot();
private slots:
void newFile();
void openFile();
void saveFile();
void hasChanged();
void selectAll();
void aboutMe();
private:
QTextEdit *textEdit;
QMenu *fileMenu;
QAction *newAction;
QAction *openAction;
QAction *saveAction;
QMenu *helpMenu;
QAction *aboutAction;
QMenu *editMenu;
QAction *selectAction;
AboutDialog *about;
bool CHANGE;
QString fileName;
};
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/56374/showart_522545.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP