how to create a basic text editor in qt c++
how to create a basic text editor in qt c++
how to create a basic text editor using qt:
In this tutorial we will create a basic text editor using qt c++
Required:
qt creatoryou can download qt creator from the qt web site.
for ubuntu : download it from ubuntu software center
getting started : creating the project
open qt creator.
click on create project under the develop tab:
select qt widget - > qt gui application.
now the new project is created and opened:
now lets get coding:
the mainwindow.h file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPlainTextEdit> // getting the text edit
#include <QFile>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QPlainTextEdit * editor; //defining the editor
QString url; // the url of the file
Ui::MainWindow *ui;
void savefile(QString url);
void svaefileas();
void savefile();
void openfile();
void newfile();
};
#endif // MAINWINDOW_H
we have declared the variables and functions.
now we will implement them in mainwindow.cpp file:
void MainWindow::openfile()
{
QString openfileurl = QFileDialog::getOpenFileName();
if(openfileurl.isEmpty() || openfileurl == url) return;
QFile file(openfileurl);
if(file.open(QIODevice::ReadOnly|QIODevice::Text))
{
url = openfileurl;
editor->setPlainText(QString::fromUtf8(file.readAll()));
}
}
this function i used to open a file it will get a file dialog in which user can select the file then it will check if the file url is the current file url or it is empty if any of these condition exits then it will end the function there.
then its open the file.
sets the current url variable to the new url.
then it sets the content of the file in the editor(QPlainTextEdit)
void MainWindow::savefile()
{
if(url.isEmpty()){savefileas(); return;}
QFile file(url);
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
file.write(editor->toPlainText().toUtf8());
}
}
first this function checks if the url is empty if it is empty then it class savefileas() and ends the script else it opens then file and put the editor contents in it.
void MainWindow::savefileas()
{
QString url = QFileDialog::getSaveFileName();
savefile(url);
}
this opens a QFileDialog with a save file name and then pass it to overloaded function savefile(QString)
void MainWindow::savefile(QString fileurl)
{
QFile file(fileurl);
if(file.open(QIODevice::WriteOnly|QIODevice::Text))
{
file.write(editor->toPlainText().toUtf8());
url = fileurl;
}
}
this function will be used when the document is not editing a file then it
will create a file it creates a file and write the contents of editor to that file.
now open mainwindow.ui file and go to slot triggered of each file menu.
void MainWindow::on_actionSave_file_triggered()
{
savefile();
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::information(this , tr("about") , tr("this is a small text editor made in order to tell people how to make a text editor for ubuntu and other platforms.using qt and c++"));
}
void MainWindow::on_actionOpen_triggered()
{
openfile();
}
these connects the ui to the functions you will also have to declare each in mainwindow.h file.
want to download the source files get it here:
texteditor.tar.gz
note:sorry for my english.
i know that this is not very good text ediotr any help for improvement will be greatly appreciated.



No comments