A simple Notepad (example) for QT programming

Source: Internet
Author: User
Tags bool save file
     Qt is a cross-platform, native development framework for all major operating systems, including Linux, Windows, Macs, Symbian, and Maemo. This article describes a simple notepad implemented under Windows with QT programming, which is interesting to learn about QT programming. I am also just in touch with QT programming, I think QT is relatively easy to learn in other languages.   This is my QT creator implementation of the simple Notepad: QQ mini version   local network chat software see QT Programming-my QQ (LAN available)-with the source code above the interface is implemented with QT  designer There is a small icon in front of the actionnew (new), moving in the toolbar, the above effect appears.           This just realizes a part of the function, you can look at the Mainwindow.h file
<span style = "font-size: 14px;"> # ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>

namespace Ui {
    class MainWindow;
}

class MainWindow: public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow (QWidget * parent = 0);
    ~ MainWindow ();
    void changeTitle (const QString & filename);
    void openfile (const QString & filename);
    void savefile (const QString & filename);


private:
    Ui :: MainWindow * ui;
    QString curFile;
    QLineEdit * find_textLineEdit;

private slots:
    void show_findText ();
    void on_action_2_triggered (); // Find
    void on_plan_textEdit_copyAvailable (bool b);
    void on_plan_textEdit_textChanged ();
    void on_action_U_triggered (); // Undo
    void on_action_P_triggered (); // Paste
    void on_action_C_triggered (); // Copy
    void on_action_T_triggered (); // Cut
    void on_actionExit_triggered (); // Exit
    bool on_actionSave_triggered (); // Save
    bool on_actionSave_As_triggered (); // Save As
    void on_actionOpen_triggered (); // Open
    void on_actionNew_triggered (); // New
};

#endif // MAINWINDOW_H
</ span>

Main.h file
<span style= "FONT-SIZE:14PX;" > #include <QtGui/QApplication>
#include <QTextCodec>
#include "mainwindow.h"

int main ( int argc, char *argv[])
{
    qapplication A (argc, argv);

    Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("GB2312"));
    Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("GB2312"));
    QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GB2312"));


    MainWindow W;
    W.show ();

    return a.exec ();
}
</span>

Mainwindow.cpp
<span style = "font-size: 14px;"> # include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow :: MainWindow (QWidget * parent):
    QMainWindow (parent),
    ui (new Ui :: MainWindow)
{
    ui-> setupUi (this);
    this-> changeTitle ("");
    this-> setCentralWidget (ui-> plan_textEdit);
    // Set the properties of the action
    ui-> action_T-> setEnabled (false);
    ui-> action_C-> setEnabled (false);
    // Set interface icon
  this-> setWindowIcon (QIcon (": / images / editor.png"));
}

MainWindow :: ~ MainWindow ()
{
    delete ui;
}

void MainWindow :: savefile (const QString & filename)
{
    QFile file (filename);
    if (! file.open (QIODevice :: WriteOnly | QIODevice :: Text)) {
        QMessageBox :: warning (this, "Save",
                             tr ("Save, error?")
                             .arg (filename)
                            .arg (file.errorString ()));
        return;
    }
    QTextStream out (& file);
    out << ui-> plan_textEdit-> toPlainText ();
    file.close ();
}

void MainWindow :: openfile (const QString & filename)
{
    QFile file (filename);
    if (file.open (QIODevice :: ReadOnly | QIODevice :: Text)) {
        QMessageBox :: warning (this, "Open",
                             tr ("Save, error?")
                             .arg (filename)
                             .arg (file.errorString ()));
        return;
    }
    QTextStream in (& file);
    QString s = in.readAll ();
    ui-> plan_textEdit-> setPlainText (s);
}
void MainWindow :: changeTitle (const QString & filename)
{
    qDebug () << "***" << filename;
    if (filename.isEmpty ()) {
         this-> curFile = "";
         this-> setWindowTitle (tr ("Untitled.txt [*]-Notepad"));
    }
    else {
        this-> curFile = filename;
        QFileInfo fi (filename);
        QString name = fi.fileName ();
        this-> setWindowTitle (tr ("% 1 [*]-Notepad"). arg (name));
        qDebug () << "====" << name;
    }
    this-> setWindowModified (false);
    ui-> plan_textEdit-> document ()-> setModified (false);
}

void MainWindow :: on_actionNew_triggered () // New
{
    QTextDocument * document = ui-> plan_textEdit-> document ();
    if (! document-> isModified ()) {
        ui-> plan_textEdit-> setPlainText ("");
        this-> changeTitle ("");
        return;
    }
    int ret = QMessageBox :: question (this, "Save?",
                                  "disSave, Save",
                                  QMessageBox :: Save | QMessageBox :: Discard | QMessageBox :: Cancel);
    switch (ret)
    {
    case QMessageBox :: Cancel:

        break;
    case QMessageBox :: Discard:
        ui-> plan_textEdit-> setPlainText ("");
        this-> changeTitle ("");
        break;
    case QMessageBox :: Save:
          if (! on_actionSave_triggered ())
                 return;
          ui-> plan_textEdit-> setPlainText ("");
          this-> changeTitle ("");
          break;
         }

}

void MainWindow :: on_actionOpen_triggered () // Open
{
    QTextDocument * document = ui-> plan_textEdit-> document ();
    if (document-> isModified ()) {
        int ret = QMessageBox :: question (this, "Save?",
                                      "disSave, Save",
                                      QMessageBox :: Save | QMessageBox :: Discard | QMessageBox :: Cancel);
        if (ret == QMessageBox :: Cancel) {
            return;
        }
        else if (ret == QMessageBox :: Save) {

        }
    }
    // No modification Normal save Abandon save
    QString filename = QFileDialog :: getOpenFileName (this);
    if (filename.isEmpty ())
        return;
    this-> openfile (filename);
    this-> changeTitle (filename);
}

bool MainWindow :: on_actionSave_As_triggered () // Save as
{
    QString fileName = QFileDialog :: getSaveFileName (this, tr ("Save File"));
    if (fileName.isEmpty ())
        return false;
    this-> savefile (fileName);
    this-> changeTitle (fileName);
    return true;
}


bool MainWindow :: on_actionSave_triggered () // Save
{
    if (this-> curFile.isEmpty ())
     // qDebug () << "+++";
            return on_actionSave_As_triggered ();

    else
    {
        savefile (this-> curFile);
      this-> setWindowModified (false);
    }
    return true;
}

void MainWindow :: on_actionExit_triggered () // Exit
{
    this-> close ();
}

void MainWindow :: on_action_T_triggered () // cut
{
    this-> ui-> plan_textEdit-> cut ();
}

void MainWindow :: on_action_C_triggered () // Copy
{
    this-> ui-> plan_textEdit-> copy ();
}

void MainWindow :: on_action_P_triggered () // paste
{
    this-> ui-> plan_textEdit-> paste ();
}


void MainWindow :: on_action_U_triggered () // Undo
{
   this-> ui-> plan_textEdit-> undo ();
}

void MainWindow :: on_plan_textEdit_textChanged ()
{
    // qDebug () << "niaho";
    this-> setWindowModified (true);
}

void MainWindow :: on_plan_textEdit_copyAvailable (bool b)
{
    ui-> action_T-> setEnabled (b);
    ui-> action_C-> setEnabled (b);

}
// This part is implemented by coding, you can see what is different from the interface design
void MainWindow :: on_action_2_triggered () // Find
{
   QDialog * findDlg = new QDialog (this);
   findDlg-> setWindowTitle (tr ("Find!"));
   find_textLineEdit = new QLineEdit (findDlg);
   QPushButton * find_Bth = new QPushButton (tr ("Find next"), findDlg);

   QHBoxLayout * layout = new QHBoxLayout (findDlg);
   layout-> addWidget (find_textLineEdit);
   layout-> addWidget (find_Bth);

   findDlg-> show ();

    connect (find_Bth, SIGNAL (clicked ()), this, SLOT (show_findText ()));
}
void MainWindow :: show_findText ()
{
     QString findText = find_textLineEdit-> text ();
     if (! ui-> plan_textEdit-> find (findText, QTextDocument :: FindBackward))
     {
         QMessageBox :: warning (this, tr ("Find"), tr ("Cannot find% 1"). Arg (findText));
     }
}
</ span>

In fact, there is a part of the function is not realized, interested in the words can pay attention to my next article.
Related Article

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.