山寨记事本

其实这个是我在联系拖放事件的时候做的一个小程序,只是可以接受文件拖放,后来我看到QTextEdit对于复制,粘贴等都有对应的函数和槽,于是就把这些东西加上去,然后这玩意就成了。 。。



这里字体的话用的是本地的字体,由于这个是把所有文件内容读到内存里操作,保存时候再由内存读回文件,所以没有做颜色和图片的功能

另外这个程序如果拖入一个100MB以上的文件会直接挂掉。。。不过由于不处理图片以及颜色,相信你没事不会找这么打一个纯文本文件 山寨记事本 - 板烧鱿鱼 - 我只是个路人甲,别在意我
~~
然后问题又来了 山寨记事本 - 板烧鱿鱼 - 我只是个路人甲,别在意我
在做的时候需要一个很普通的文本编辑功能,删除
但很神奇的是QTextEdit居然没这功能(也许有,但我真没找到。。。)我不知道为什么QTextEdit没有提供这个功能,应为右键菜单里是有这个delete功能的..
于是乎我开始各种找资料,QT在文本处理有个删除选的文本的函数,但这个函数不是QTextEditt的成员函数,他位于另一个类中,而这个类是QTextEdit的友元类。。 山寨记事本 - 板烧鱿鱼 - 我只是个路人甲,别在意我


我忍不住想喷人了。。。


后来没办法用了网友提供的办法通过移动光标的办法来达到删除的作用,(我自己写了个deleteSelect()的槽...),效果上来来说到一样,但删除的东西会进入系统的剪切板 山寨记事本 - 板烧鱿鱼 - 我只是个路人甲,别在意我
所以从外部ctrl+C然后进入程序删除,然后ctrl+v结果会出现你刚删除的内容。。


不过好歹至少实现了删除的功能,有点“副作用”也么办法了,但问题又来了。。就是我上面说的右键菜单,这菜单是英文的,我用了各种方法加载.qm文件,qlineEdit等编辑器的右键菜单都是中文的,唯独这货的右键始终是英文的。。


后 来没办法,我想只能重新实现右键菜单,考虑到这个程序是QTextEdit直接装到主程序里(QMainWindow),起初我想直接装个过滤器,拦截掉 这个右键菜单事件,但神(dan)奇(tong)的一幕再度发生。。。无论拦截QContextMenuEvent还是拦截 QMouseButtonEvent这个右键菜单还是会跳处来。。。 山寨记事本 - 板烧鱿鱼 - 我只是个路人甲,别在意我


最后我实在么办法了只能继承QTextEdit....
 
山寨记事本文件
http://pan.baidu.com/s/1hqJ3J6W
注意这里文件是我在linux(ubuntu14.04)下写的文件,在win系统下编译没问题,但打开可能会出现乱码.如果有这里问题,下面有源代码(PS:源代码里没有.pro文件的源代码)
这里pro文件写好了,编译的时候记得打开编译器的c++11选择(如果你没开的话)
~~~~~~以下是源代码~~~~~~

FindDialog.h

#ifndef FINDDIALOG_H_
#define FINDDIALOG_H_
#include<QDialog>
#include<QLineEdit>
#include<QPushButton>
#include<QRadioButton>
#include<QGroupBox>
#include<QCheckBox>
#include<QLabel>
#include<QCloseEvent>
class FindDialog:public QDialog
{
  Q_OBJECT
 private:
  QLabel* findtext_label;
  QLineEdit* text_edit;
  QCheckBox* matchCase_checkbox;
  QGroupBox* turns_groupbox;
  QRadioButton* goFornt_button;
  QRadioButton* goBack_button;
  QPushButton* find_button;
  QPushButton* cancel_button;
  bool cs;
  bool turns;
 public:
  FindDialog(QWidget* parent = 0);
 protected:
  void closeEvent(QCloseEvent*);
 private slots:
  void FindText();
 signals:
  void finding(const QString& , bool , bool);
};
#endif


FindDialog.cxx

#include"FindDialog.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
FindDialog::FindDialog(QWidget* parent):QDialog(parent)
{
  findtext_label = new QLabel(tr("查找内容"));
  text_edit = new QLineEdit;

  matchCase_checkbox = new QCheckBox(tr("区分大小写"));
  cs = false;  

  turns_groupbox = new QGroupBox(tr("方向"));
  goFornt_button = new QRadioButton(tr("向前"));
  goFornt_button->setCheckable(true);
  turns = true;
  goBack_button = new QRadioButton(tr("向后"));

  find_button = new QPushButton(tr("查找"));
  connect(find_button,SIGNAL(clicked()),this,SLOT(FindText()));
  cancel_button = new QPushButton(tr("取消"));
  connect(cancel_button,SIGNAL(clicked()),this,SLOT(close()));
  //布局
  QHBoxLayout* text_layout = new QHBoxLayout;
  text_layout->addWidget(findtext_label);
  text_layout->addWidget(text_edit);
  QHBoxLayout* turns_layout = new QHBoxLayout;
  turns_layout->addWidget(goFornt_button);
  turns_layout->addWidget(goBack_button);
  turns_groupbox->setLayout(turns_layout);
  QHBoxLayout* option_layout = new QHBoxLayout;
  option_layout->addWidget(matchCase_checkbox);
  option_layout->addWidget(turns_groupbox);
  QVBoxLayout* left_layout = new QVBoxLayout;
  left_layout->addLayout(text_layout);
  left_layout->addLayout(option_layout);
  QVBoxLayout* button_layout = new QVBoxLayout;
  button_layout->addWidget(find_button);
  button_layout->addWidget(cancel_button);
  button_layout->addStretch();
  QHBoxLayout* main_layout = new QHBoxLayout;
  main_layout->addLayout(left_layout);
  main_layout->addLayout(button_layout);
  setLayout(main_layout);
  main_layout->setSizeConstraint(QLayout::SetFixedSize);//固定大小
  setWindowTitle(tr("查找"));
}
void FindDialog::closeEvent(QCloseEvent*)
{
  hide();
}
void FindDialog::FindText()
{
  if(matchCase_checkbox->isChecked())
   cs = true;
  else cs = false;
  if(goFornt_button->isChecked())
   turns = true;
  else turns = false;
 QString ts = text_edit->text();
 emit finding(ts,cs,turns);
}


myEdit.h

#ifndef MYEDIT_H_
#define MYEDIT_H_
#include<QTextEdit>
#include<QContextMenuEvent>
#include<QMenu>
#include<QAction>
class myEdit:public QTextEdit
{
  Q_OBJECT
 private:
  QAction* undo_action;
  QAction* redo_action;
  QAction* delete_action;
  QAction* copy_action;
  QAction* paste_action;
  QAction* cut_action;
  QAction* selectAll_action;
  QMenu* context_menu;  //右键菜单
 public:
  myEdit(QWidget* parent = 0);
 private slots:
  void deleteText();
 protected:
  void contextMenuEvent(QContextMenuEvent*);
};
#endif


myEdit.cxx

#include<QCursor>
#include"myEdit.h"
myEdit::myEdit(QWidget* parent):QTextEdit(parent)
{
  undo_action = new QAction(QIcon(tr(":/images/un.png")),tr("撤销"),this);
  redo_action = new QAction(QIcon(tr(":/images/re.png")),tr("恢复"),this);
  copy_action = new QAction(QIcon(tr(":/images/copy.png")),tr("复制"),this);
  paste_action = new QAction(QIcon(tr(":/images/paste.png")),tr("粘贴"),this);
  cut_action = new QAction(QIcon(tr(":/images/cut.png")),tr("剪切"),this);
  delete_action = new QAction(QIcon(tr(":/images/delete")),tr("删除"),this);
  selectAll_action = new QAction(tr("全选"),this);

  //连接信号
  connect(undo_action,SIGNAL(triggered()),this,SLOT(undo()));
  connect(redo_action,SIGNAL(triggered()),this,SLOT(redo()));
  connect(copy_action,SIGNAL(triggered()),this,SLOT(copy()));
  connect(paste_action,SIGNAL(triggered()),this,SLOT(paste()));
  connect(cut_action,SIGNAL(triggered()),this,SLOT(cut()));
  connect(delete_action,SIGNAL(triggered()),this,SLOT(deleteText()));
  connect(selectAll_action,SIGNAL(triggered()),this,SLOT(selectAll()));

  context_menu = new QMenu(this);
  context_menu->addAction(undo_action);
  context_menu->addAction(redo_action);
  context_menu->addSeparator();
  context_menu->addAction(copy_action);
  context_menu->addAction(paste_action);
  context_menu->addAction(cut_action);
  context_menu->addAction(delete_action);
  context_menu->addSeparator();
  context_menu->addAction(selectAll_action);
}
void myEdit::deleteText()
{
  moveCursor(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor);
  cut();
  setFocus();
}
void myEdit::contextMenuEvent(QContextMenuEvent* event)
{
  context_menu->exec(QCursor::pos());
  event->accept();
}


ReadMe.h

#ifndef README_H_
#define README_H_
#include<QMainWindow>
#include<QAction>
#include<QActionGroup>
#include<QMenu>
#include<QMenuBar>
#include<QToolBar>
#include<QDropEvent>
#include<QDragEnterEvent>
#include<QCloseEvent>
#include"FindDialog.h"
#include"myEdit.h"
class ReadMe:public QMainWindow
{
  Q_OBJECT
 private:
  QString myPath;  //保存当前编辑文件的路径
  myEdit* texts;  //中心编辑窗口
  FindDialog* findDialog;
  //文件菜单动作
  QAction* new_action;
  QAction* open_action;
  QAction* save_action;
  QAction* saveAs_action;
  QAction* close_action;
  //编辑菜单动作
  QAction* forback_action;
  QAction* reback_action;
  QAction* cut_action;
  QAction* copy_action;
  QAction* paste_action;
  QAction* delete_action;
  QAction* find_action;
  QAction* findNext_action;
  QAction* selectAll_action;
  QAction* date_action;
  //格式菜单动作
  QAction* line_action;
  QActionGroup* setAlignment_action;
  QAction* leftAlignment_action;
  QAction* centerlAlignment_action;
  QAction* rightAlignment_action;
  QAction* font_action;
  //帮助菜单动作
  QAction* see_action;
  QAction* about_action;
  //菜单 
  QMenu* file_menu;
  QMenu* edit_menu;
  QMenu* form_menu;
  QMenu* help_menu;
  QMenuBar* menus;  //菜单栏
  QToolBar* tools;  //工具栏
  void create_action();  //构建菜单
  void create_menu();
  void create_tool();  //构建工具栏
  void openFile(QString&);
 protected:
  void dropEvent(QDropEvent*);
  void dragEnterEvent(QDragEnterEvent*);  //拖放事件
  void closeEvent(QCloseEvent*);
 public:
  ReadMe(QWidget* parent = 0);  //构造函数
 private slots:
  void NewFile();
  void OpenFile();
  void SaveFile();
  void SaveAsFile();
  void deleteSelected();
  void CurrentDate();
  void setLineMode();
  void SetAlignment();
  void Finding();
  void fonts();
 public slots:
  void search(const QString& , bool , bool);
};
#endif


ReadMe.cxx

#include"ReadMe.h"
#include<QUrl>
#include<QFile>
#include<QMimeData>
#include<QFileDialog>
#include<QMessageBox>
#include<QTextStream>
#include<QDateTime>
#include<QFontDialog>
ReadMe::ReadMe(QWidget* parent):QMainWindow(parent)
{
  texts = new myEdit;
  texts->setAcceptDrops(false);
  texts->setAlignment(Qt::AlignLeft);
  setAcceptDrops(true);  //这里设置主窗体接受拖放
  setCentralWidget(texts);  // 把text设为中心窗口用于编辑
  create_action();  
  create_menu();  //创建菜单
  create_tool();
  findDialog = new FindDialog(this);
  connect(findDialog,SIGNAL(finding(const QString&,bool,bool)),this,SLOT(search(const QString&,bool,bool)));
  setWindowTitle(tr("ReadMe"));
  setWindowIcon(QIcon(tr(":/images/read.png")));
}
void ReadMe::create_action()
{ 
  //文件菜单动作
  new_action = new QAction(QIcon(tr(":/images/new.png")),tr("新建"),this);
  connect(new_action,SIGNAL(triggered()),this,SLOT(NewFile()));
  
  open_action = new QAction(QIcon(tr(":/images/open.png")),tr("打开"),this);
  connect(open_action,SIGNAL(triggered()),this,SLOT(OpenFile()));

  save_action = new QAction(QIcon(tr(":/images/save.png")),tr("保存"),this);
  connect(save_action,SIGNAL(triggered()),this,SLOT(SaveFile()));
  
  saveAs_action = new QAction(QIcon(tr(":/images/saveAs.png")),tr("另存为..."),this);
  connect(saveAs_action,SIGNAL(triggered()),this,SLOT(SaveAsFile()));

  close_action = new QAction(tr("关闭"),this);
  connect(close_action,SIGNAL(triggered()),this,SLOT(close()));
  //编辑菜单动作
  forback_action = new QAction(QIcon(tr(":/images/re.png")),tr("撤销"),this);
  connect(forback_action,SIGNAL(triggered()),texts,SLOT(undo()));
  
  reback_action = new QAction(QIcon(tr(":/images/un.png")),tr("恢复"),this);
  connect(reback_action,SIGNAL(triggered()),texts,SLOT(redo()));

  cut_action = new QAction(QIcon(tr(":/images/cut.png")),tr("剪切"),this);
  connect(cut_action,SIGNAL(triggered()),texts,SLOT(cut()));
  
  copy_action = new QAction(QIcon(tr(":/images/copy.png")),tr("复制"),this);
  connect(copy_action,SIGNAL(triggered()),texts,SLOT(copy()));

  paste_action = new QAction(QIcon(tr(":/images/paste.png")),tr("粘贴"),this);
  connect(paste_action,SIGNAL(triggered()),texts,SLOT(paste()));
 
  delete_action = new QAction(QIcon(tr(":/images/delete.png")),tr("删除"),this);
  connect(delete_action,SIGNAL(triggered()),this,SLOT(deleteSelected()));

  find_action = new QAction(tr("查找"),this);
  connect(find_action,SIGNAL(triggered()),this,SLOT(Finding()));
  
  findNext_action = new QAction(tr("查找下一个"),this);
  connect(findNext_action,SIGNAL(triggered()),this,SLOT(Finding()));

  selectAll_action = new QAction(tr("全选"),this);
  connect(selectAll_action,SIGNAL(triggered()),texts,SLOT(selectAll()));

  date_action = new QAction(tr("日期/时间"),this);
  connect(date_action,SIGNAL(triggered()),this,SLOT(CurrentDate()));
  
  //格式菜单
  line_action = new QAction(tr("自动换行"),this);
  line_action->setCheckable(true);
  line_action->setChecked(true);
  connect(line_action,SIGNAL(triggered()),this,SLOT(setLineMode()));

  setAlignment_action = new QActionGroup(this);
  leftAlignment_action = new QAction(QIcon(tr(":/images/left.png")),tr("左对齐"),this);
  leftAlignment_action->setCheckable(true);
  connect(leftAlignment_action,SIGNAL(triggered()),this,SLOT(SetAlignment()));
  
  centerlAlignment_action = new QAction(QIcon(tr(":/images/center.png")),tr("中对齐"),this);
  centerlAlignment_action->setCheckable(true);
  connect(centerlAlignment_action,SIGNAL(triggered()),this,SLOT(SetAlignment()));
  
  rightAlignment_action = new QAction(QIcon(tr(":/images/right.png")),tr("右对齐"),this);
  rightAlignment_action->setCheckable(true);
  connect(rightAlignment_action,SIGNAL(triggered()),this,SLOT(SetAlignment()));

  setAlignment_action->addAction(leftAlignment_action);
  setAlignment_action->addAction(centerlAlignment_action);
  setAlignment_action->addAction(rightAlignment_action);
  leftAlignment_action->setChecked(true);  //默认为左对齐

  font_action = new QAction(tr("字体"),this);
  connect(font_action,SIGNAL(triggered()),this,SLOT(fonts()));
  //帮助菜单动作
  see_action = new QAction(tr("查看帮助"),this);

  about_action = new QAction(tr("关于记事本"),this);
}
void ReadMe::create_menu()
{
  menus = menuBar();  //创建菜单栏
  //文件菜单
  file_menu = new QMenu(tr("文件"),this);
  file_menu->addAction(new_action);
  file_menu->addAction(open_action);
  file_menu->addAction(save_action);
  file_menu->addAction(saveAs_action);
  file_menu->addSeparator();
  file_menu->addAction(close_action);
  //编辑菜单
  edit_menu = new QMenu(tr("编辑"),this);
  edit_menu->addAction(forback_action);
  edit_menu->addAction(reback_action);
  edit_menu->addSeparator();
  edit_menu->addAction(cut_action);
  edit_menu->addAction(copy_action);
  edit_menu->addAction(paste_action);
  edit_menu->addAction(delete_action);
  edit_menu->addSeparator();
  edit_menu->addAction(find_action);
  edit_menu->addAction(findNext_action);
  edit_menu->addSeparator();
  edit_menu->addAction(selectAll_action);
  edit_menu->addAction(date_action);
  //格式菜单
  form_menu = new QMenu(tr("格式"),this);
  form_menu->addAction(line_action);
  form_menu->addSeparator();
  form_menu->addAction(leftAlignment_action);
  form_menu->addAction(centerlAlignment_action);
  form_menu->addAction(rightAlignment_action);
  form_menu->addSeparator();
  form_menu->addAction(font_action);
  //帮助菜单
  help_menu = new QMenu(tr("帮助"),this);
  help_menu->addAction(see_action);
  help_menu->addAction(about_action);
  //把菜单添加到菜单栏
  menus->addMenu(file_menu);
  menus->addMenu(edit_menu);
  menus->addMenu(form_menu);
  menus->addMenu(help_menu);
}
void ReadMe::create_tool()
{
  tools = addToolBar(tr("工具"));
  tools->addAction(new_action);
  tools->addAction(open_action);
  tools->addAction(save_action);
  tools->addAction(saveAs_action);
  tools->addAction(cut_action);
  tools->addAction(paste_action);
  tools->addAction(delete_action);
  tools->addAction(forback_action);
  tools->addAction(reback_action);
  tools->addAction(leftAlignment_action);
  tools->addAction(centerlAlignment_action);
  tools->addAction(rightAlignment_action);
}
void ReadMe::dragEnterEvent(QDragEnterEvent* event)
{
  if(event->mimeData()->hasFormat(tr("text/uri-list")))
   event->acceptProposedAction();  //确保只接受txt文件
}
void ReadMe::dropEvent(QDropEvent* event)
{
  QList<QUrl> urls = event->mimeData()->urls();  //获得拖入的文件
  if(urls.isEmpty() or urls.size() > 1)  //拖入文件不能为空且只能拖入一个文件
   return;  
  QString names = urls.first().toLocalFile(); //然后判断拖入的是不是本地文件
  if(!names.isEmpty()) 
   openFile(names);
}
void ReadMe::closeEvent(QCloseEvent* event)  //关闭前要确认文件有无被更改
{
  if(texts->document()->isModified() and !((texts->toPlainText()).isEmpty()))
   {
    int buttons = QMessageBox::warning(this,tr("warning"),tr("文件尚未保存,是否保存当前文件?"),QMessageBox::Yes|QMessageBox::No);
    if(buttons == QMessageBox::Yes)
      {
       SaveAsFile();
       event->accept();
      }
     else if(buttons == QMessageBox::No)
       event->accept();
   }
   else if(!texts->document()->isModified()) 
     event->accept();
}
void ReadMe::openFile(QString& paths)
{
  QFile A(paths);
  QTextStream outs(&A);
  A.open(QIODevice::ReadWrite|QIODevice::Text);
  myPath = paths;
  texts->setText(outs.readAll());
}
void ReadMe::NewFile()
{
  if(texts->document()->isModified() and !((texts->toPlainText()).isEmpty()))
   {
   int buttons = QMessageBox::warning(this,tr("warning"),tr("当前文件已被更改,是否需要保存改动?"),QMessageBox::Yes|QMessageBox::No);
   if(buttons == QMessageBox::Yes)
    {
     SaveFile();
     texts->clear();
    }
    else if(buttons == QMessageBox::No)
     texts->clear();
   }
  texts->clear();
}
void ReadMe::OpenFile()
{
  QString paths = QFileDialog::getOpenFileName(this,tr("打开文件"),".",tr("Text(*.txt)"));
  if(!paths.isEmpty())
   openFile(paths);
}
void ReadMe::SaveFile()
{
  QFile A(myPath);
  QTextStream in(&A);
  A.open(QIODevice::WriteOnly);
  in<<(texts->toPlainText());
  texts->document()->setModified(false);
}
void ReadMe::SaveAsFile()
{
  QString paths = QFileDialog::getSaveFileName(this,tr("另存为.."),".",tr("Text(*.txt)"));
  QFile A(paths);
  QTextStream in(&A);
  A.open(QIODevice::WriteOnly);
  in<<(texts->toPlainText());
  texts->document()->setModified(false);
}
void ReadMe::deleteSelected()
{
  texts->moveCursor(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor);
  texts->cut();
  texts->setFocus();
}
void ReadMe::CurrentDate()
{
  texts->insertPlainText(QDateTime::currentDateTime().toString());
}
void ReadMe::setLineMode()
{
  if(line_action->isChecked())
   texts->setLineWrapMode(QTextEdit::WidgetWidth);
  else if(!line_action->isChecked())
    texts->setLineWrapMode(QTextEdit::NoWrap);
}
void ReadMe::SetAlignment()
{
  if(leftAlignment_action->isChecked())
    texts->setAlignment(Qt::AlignLeft);
  if(centerlAlignment_action->isChecked())
    texts->setAlignment(Qt::AlignCenter);
  if(rightAlignment_action->isChecked())
    texts->setAlignment(Qt::AlignRight);
}
void ReadMe::Finding()
{
  if(findDialog->isHidden())
    findDialog->show();
  else
    {
     findDialog->raise();
     findDialog->activateWindow();
    }
}
void ReadMe::fonts()
{
  bool gets;
  QFont myFont = QFontDialog::getFont(&gets,texts->currentFont(),this,tr("字体设置"));
  if(gets == true)
   {
    texts->setCurrentFont(myFont);
    texts->document()->setModified();
   }
}
void ReadMe::search(const QString& ts , bool cs , bool FB)
{
  bool hasWord = true;
  if(cs == false and FB == false)
     hasWord = texts->find(ts,QTextDocument::FindBackward);
  if(cs == false and FB == true)
     hasWord = texts->find(ts); 
  if(cs == true and FB == false)
     hasWord = texts->find(ts,QTextDocument::FindCaseSensitively|QTextDocument::FindBackward);
  if(cs == true and FB == true)
     hasWord = texts->find(ts,QTextDocument::FindCaseSensitively);
  if(hasWord == false)
   QMessageBox::information(this,tr("查询"),tr("无结果"),QMessageBox::Yes);
}


main.cxx

#include"ReadMe.h"
#include<QApplication>
int main(int argc , char** argv)
{
  QApplication app(argc,argv);
  ReadMe A;
  A.show();
  return app.exec();
}


pix.qrc

<RCC>
<qresource>
<file>images/center.png</file>
<file>images/left.png</file>
<file>images/right.png</file>
<file>images/open.png</file>
<file>images/new.png</file>
<file>images/save.png</file>
<file>images/saveAs.png</file>
<file>images/cut.png</file>
<file>images/paste.png</file>
<file>images/copy.png</file>
<file>images/re.png</file>
<file>images/un.png</file>
<file>images/delete.png</file>
<file>images/read.png</file>
</qresource>
</RCC>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值