"QT5 Development and examples" 12. Implement a simple text editor 1

Source: Internet
Author: User

Showwidget.h

/*** Book: "QT5 Development and examples" * Function: Implement a simple text editor * file: showwidget.h* time: January 18, 2015 10:03:21* Author: cutter_point*/#ifndef showwidget_h# Define Showwidget_h#include <QWidget> #include <QLabel> #include <QTextEdit> #include <QImage> Class Showwidget:public qwidget{  q_objectpublic:  explicit Showwidget (Qwidget *parent = 0);  Qimage img;  Qlabel *imagelabel;  Qtextedit *text;signals:public Slots:}; #endif//Showwidget_h

Showwidget.cpp

/*** Book: "QT5 Development and examples" * Function: Implement a simple text editor * file: showwidget.h* time: January 18, 2015 10:03:21* Author: cutter_point*/#include "showwidget.h "#include <qhboxlayout>showwidget::showwidget (Qwidget *parent):  qwidget (parent) {  Imagelabel = new Qlabel;  Imagelabel->setscaledcontents (true);  Text = new Qtextedit;  Qhboxlayout *mainlayout = new Qhboxlayout (this);  Mainlayout->addwidget (Imagelabel);  Mainlayout->addwidget (text);}

Imageprocessor.h

/*** Book: "QT5 Development and examples" * Function: Implement a simple text editor * file: showwidget.h* time: January 18, 2015 10:41:52* Author: cutter_point*/#ifndef imageprocessor _h#define imageprocessor_h#include <QMainWindow> #include <QImage> #include <QLabel> #include < qmenu> #include <QMenuBar> #include <QAction> #include <QComboBox> #include <qspinbox># Include <QToolBar> #include <QFontComboBox> #include <QToolButton> #include <QTextCharFormat>  #include "showwidget.h" class Imageprocessor:public qmainwindow{q_objectpublic:imageprocessor (qwidget *parent = 0);  ~imageprocessor ();  Create action void createactions ();  Create menu void Createmenus ();  Creates a toolbar void createtoolbars ();    void LoadFile (QString filename);    Load file void Mergeformat (Qtextcharformat);  Text Format private://menu bar Qmenu *filemenu;  Qmenu *zoommenu;  Qmenu *rotatemenu;  Qmenu *mirrormenu;   Qimage img;     Picture QString FileName;     FileName Showwidget *showwidget;  Display Stream//File menu item qaction *openfileaction; Qaction *nEwfileaction;  Qaction *printtextaction;  Qaction *printimageaction;  Qaction *exitaction;  Edit menu item Qaction *copyaction;  Qaction *cutaction;  Qaction *pasteaction;  Qaction *aboutaction;  Qaction *zoominaction;  Qaction *zoomoutaction;  Rotate menu item qaction *rotate90action;  Qaction *rotate180action;  Qaction *rotate270action;  Mirror menu item Qaction *mirrorverticalaction;  Qaction *mirrorhorizontalaction;  Qaction *undoaction;  Qaction *redoaction;  Toolbar Qtoolbar *filetool;  Qtoolbar *zoomtool;  Qtoolbar *rotatetool;  Qtoolbar *mirrortool;   Qtoolbar *dotoolbar;protected Slots://Add function slot void Shownewfile ();    Create a new file void Showopenfile ();   Open file void Showprinttext ();    Implementation of the Print function void Showprintimage (); print a picture}; #endif//Imageprocessor_h

Imageprocessor.cpp

/*** Book: "QT5 Development and examples" * Function: Implement a simple text editor * file: showwidget.h* time: January 18, 2015 10:41:52* Author: cutter_point*/#include " Imageprocessor.h "#include <QApplication> #include <QDebug> #include <QFileDialog> #include < qfile> #include <qtextstream>//#include <QPrintDialog> #include <qtprintsupport/qprintdialog># Include <QtPrintSupport/QPrinter> #include <qpainter>imageprocessor::imageprocessor (qwidget *parent):  Qmainwindow (parent) {//First set the title Setwindowtitle (tr ("My word"));    Create a place to put the label and text input of the image, set this as the central part showwidget = new Showwidget (this);   Create an Object Setcentralwidget (Showwidget);  Set this as the center part//Qdebug () << "11111111111111";      Createactions ();  Create action//Qdebug () << "222222222222222222";    Createmenus ();  Create Menu//Qdebug () << "3333333333333333";   Createtoolbars ();  Tools//Qdebug () << "..."; If the image is loaded successfully if (Img.load (":/image.png")) {//images are placed in Imagelabel Showwidget->imagelabel->setpixmap (qpixmap:: FromImage (IMG));  }}//Toolbar implementation void Imageprocessor::createtoolbars () {//file toolbar Filetool = Addtoolbar ("file");  Filetool->addaction (openfileaction);  Filetool->addaction (newfileaction);  Filetool->addaction (printtextaction);  Filetool->addaction (printimageaction);  Edit Toolbar Zoomtool = Addtoolbar ("edit");  Zoomtool->addaction (copyaction);  Zoomtool->addaction (cutaction);  Zoomtool->addaction (pasteaction);//Zoomtool->addaction (aboutaction); Zoomtool->addseparator ();  Delimiter zoomtool->addaction (zoominaction);  Zoomtool->addaction (zoomoutaction);  Rotary Tool Rotatetool = Addtoolbar ("rotate");  Rotatetool->addaction (rotate90action);  Rotatetool->addaction (rotate180action);  Rotatetool->addaction (rotate270action);  Undo and Redo Dotoolbar = Addtoolbar ("Doedit");  Dotoolbar->addaction (undoaction);  Dotoolbar->addaction (redoaction); Filetool->setallowedareas (qt::toptoolbararea| Qt::lefttoolbararea);} Menu implementation void Imageprocessor::createmenus () {//File menu Filemenu = MeNubar ()->addmenu (tr ("file"));  Filemenu->addaction (openfileaction);  Filemenu->addaction (newfileaction);  Filemenu->addaction (printtextaction);  Filemenu->addaction (printimageaction); Filemenu->addseparator ();  Delimiter filemenu->addaction (exitaction);  Zoom Menu Zoommenu = MenuBar ()->addmenu (tr ("Zoom"));  Scaling zoommenu->addaction (copyaction);  Zoommenu->addaction (cutaction);  Zoommenu->addaction (pasteaction);  Zoommenu->addaction (aboutaction); Zoommenu->addseparator ();  Delimiter zoommenu->addaction (zoominaction);  Zoommenu->addaction (zoomoutaction);  Rotation menu Rotatemenu = MenuBar ()->addmenu (tr ("rotate"));  Rotatemenu->addaction (rotate90action);  Rotatemenu->addaction (rotate180action);  Rotatemenu->addaction (rotate270action);  Mirror Menu Mirrormenu = MenuBar ()->addmenu (tr ("Mirror"));  Mirrormenu->addaction (mirrorverticalaction); Mirrormenu->addaction (mirrorhorizontalaction);} Implementation of the Action void Imageprocessor::createactions () {//Open action//  Qdebug () << "11111111111111";    Openfileaction = new Qaction (Qicon (":/open.png"), tr ("open"), this);    Set the picture, name Openfileaction->setshortcut (tr ("Ctrl+o"));    Set the shortcut key Openfileaction->setstatustip (TR ("Open a File")); Setup tips//Qdebug () << "222222222222222222";//Connect (Newfileaction, SIGNAL (triggered ()), this, SLOT (shownewfile   ()));  Connect the corresponding slot connect (openfileaction, SIGNAL (triggered ()), this, slot (Showopenfile ()));    New Action newfileaction = new Qaction (Qicon (":/new.png"), tr ("new"), this);    Set the picture, name Newfileaction->setshortcut (tr ("CTRL + N"));    Set the shortcut key Newfileaction->setstatustip (TR ("New A File"));   Settings prompt connect (newfileaction, SIGNAL (triggered ()), this, SLOT (Shownewfile ()));    Connect the corresponding Slot//exit action exitaction = new Qaction (tr ("Out"), this);    Set the picture, name Exitaction->setshortcut (tr ("Ctrl+q"));    Set the shortcut key Exitaction->setstatustip (tr ("close"));  Set prompt for connect (exitaction, SIGNAL (triggered ()), this, SLOT (Close ())); Copy copyaction = new QactioN (Qicon (":/copy.png"), tr ("Out"), this);    Set the picture, name Copyaction->setshortcut (tr ("CTRL + C"));    Set the shortcut key Copyaction->setstatustip (tr ("Copy File"));  Settings prompt for connect (copyaction, SIGNAL (triggered ()), Showwidget->text, SLOT (copy ()));    Cut cutaction = new Qaction (Qicon (":/cut.png"), tr ("Cut"), this);    Set the picture, name Cutaction->setshortcut (tr ("ctrl+x"));    Set the shortcut key Cutaction->setstatustip (tr ("Cut file"));  Settings prompt connect (copyaction, SIGNAL (triggered ()), Showwidget->text, SLOT (Cut ()));    Paste pasteaction = new Qaction (Qicon (":/paste.png"), tr ("paste"), this);    Set the picture, name Pasteaction->setshortcut (tr ("Ctrl + V"));    Set the shortcut key Pasteaction->setstatustip (tr ("Paste file"));  Settings prompt for connect (copyaction, SIGNAL (triggered ()), Showwidget->text, SLOT (paste ()));  About//Qapplication *app;  Aboutaction = new Qaction (tr ("about"), this);  Connect (aboutaction, SIGNAL (triggered ()), this, SLOT (QAPPLICATION::ABOUTQT ())); Print text printtextaction = new Qaction (Qicon (":/printText.png "), tr (" Printtext "), this);  Printtextaction->setstatustip (TR ("Print a Text"));   Connect (printtextaction, SIGNAL (triggered ()), this, SLOT (Showprinttext ()));  Connect the corresponding action//print image printimageaction = new Qaction (Qicon (":/printimage.png"), tr ("Printimage"), this);  Printimageaction->setstatustip (TR ("Print a Image"));  Connect (printimageaction, SIGNAL (triggered ()), this, SLOT (Showprintimage ()));  Enlarge image zoominaction = new Qaction (Qicon (":/zoomin.png"), tr ("Get Big"), this);  Zoominaction->setstatustip (tr ("Get Big Image"));  Reduce zoomoutaction = new Qaction (Qicon (":/zoomout.png"), tr ("Get Small"), this);  Zoomoutaction->setstatustip (tr ("Get small Image"));  Implementation of the image selection action//Rotation 90°rotate90action = new Qaction (Qicon (":/rotate90.png"), tr ("Get Rotate90"), this);  Rotate90action->setstatustip (tr ("Get Rotate90 image"));  180°rotate180action = new Qaction (Qicon (":/rotate180.png"), tr ("Get rotate180"), this); Rotate180action->setstatustip (tr ("Get rotate180 image")); 270°rotate270action = new Qaction (Qicon (":/rotate270.png"), tr ("Get rotate270"), this);  Rotate270action->setstatustip (tr ("Get rotate270 image"));  Implementation of image Mirroring action//Portrait mirrorverticalaction = new Qaction (Qicon ("Mirrorvertical.png"), tr ("Mirrorvertical"), this);  Mirrorverticalaction->setstatustip (TR ("mirrorvertical the image");  Transverse mirrorhorizontalaction = new Qaction (Qicon ("Mirrorhorizontal.png"), tr ("Mirrorhorizontal"), this);  Mirrorhorizontalaction->setstatustip (TR ("Mirrorhorizontal the image");    Implement undo and redo actions//undo and redo UndoAction = new Qaction (Qicon (":/undo.png"), "undo", this);  Revoke Connect (undoaction, SIGNAL (triggered ()), Showwidget->text, SLOT (undo ()));    RedoAction = new Qaction (Qicon (":/redo.png"), "Redo", this); Redo Connect (undoaction, SIGNAL (triggered ()), Showwidget->text, SLOT (Redo ()));}  Functions that implement the print image function void Imageprocessor::showprintimage () {Qprinter printer;  Qprintdialog PrintDialog (&printer, this);   Whether to click OK if (Printdialog.exec ()) {   qpainter painter (&printer);    Qrect rect = Painter.viewport ();    Gets the perspective rectangle area of the Qpainter object qsize size = Img.size ();      The size of the picture Size.scale (Rect.size (), qt::keepaspectratio);   Painter.setviewport (Rect.x (), Rect.y (), Size.width (), Size.Height ());    Set Rectangular area painter.drawimage (0, 0, IMG);   }}//Implement print text function void Imageprocessor::showprinttext () {//QP Qprinter printer;  Print Qprintdialog PrintDialog (&printer, this);    if (printdialog.exec ())//Whether click OK {qtextdocument *doc = showwidget->text->document ();   Get the Document Object Doc->print (&printer);    Start printing}}//open file void Imageprocessor::showopenfile () {fileName = Qfiledialog::getopenfilename (this);         Get the file name if (!filename.isempty ())//file is not empty {if (Showwidget->text->document ()->isempty ())//The root node of the content of the central part is not empty   {loadFile (fileName);   Add File} else {imageprocessor *newimageprocessor = new Imageprocessor; Create a new My Word document NewImageProcessor->show ();    Show out Newimageprocessor->loadfile (FileName); Add File}}}//implements the LoadFile function, which will be learned again after processing void imageprocessor::loadfile (QString filename) {printf ("File name:%s    \ n ", Filename.data ());  Output file name QFile file (filename); if (File.Open (qiodevice::readonly|      Qiodevice::text)) {Qtextstream textstream (&file);    while (!textstream.atend ())//loop to the back {Showwidget->text->append (Textstream.readline ());        Add a file to the document printf ("Read line\n");    } printf ("end\n");  }}//new file slot void Imageprocessor::shownewfile () {//Qdebug () << "????";  Imageprocessor *newimageprocessor = new Imageprocessor;    Newimageprocessor->show (); A new document displays}imageprocessor::~imageprocessor () {}

Main.cpp

#include "imageprocessor.h" #include <QApplication> #include <qdebug>int main (int argc, char *argv[]) {  Qapplication A (argc, argv);  Imageprocessor W;  W.show ();  return a.exec ();}

Imageprocessor.pro

#-------------------------------------------------# # Project created by Qtcreator 2015-01-18t10:01:31##------------ -------------------------------------QT       + + core GUIQT + = Printsupportgreaterthan (Qt_major_version, 4): qt + = Widgetstarget = Imageprocessortemplate = appsources + = main.cpp        imageprocessor.cpp     showwidget.cppheaders  + = Imageprocessor.h     showwidget.hresources + =     IMAGES.QRC

Operation Result:








Hey, after the test, it should be normal point, and now is slowly adjusting.

But recently the LPL League is really interesting AH!!! There is wood there!!! Support OMG














"QT5 Development and examples" 12. Implement a simple text editor 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.