#ifndef TEDIT_H
#define TEDIT_H
#include <QWidget>
{
Q_OBJECT
public:
TEdit();
virtual ~TEdit();
private slots:
void makeBold();
void makeItalic();
void makeUnderline();
void setAlignLeft();
void setAlignCenter();
void setAlignRight();
private:
};
#endif /* TEDIT_H_ */
#ifndef TEDIT_H
#define TEDIT_H
#include <QWidget>
class QToolBar;
class QAction;
class QTextEdit;
class TEdit : public QWidget
{
Q_OBJECT
public:
TEdit();
virtual ~TEdit();
private slots:
void makeBold();
void makeItalic();
void makeUnderline();
void setAlignLeft();
void setAlignCenter();
void setAlignRight();
private:
QToolBar *toolBar;
QAction *textBold;
QAction *textItalic;
QAction *textUnderline;
QAction *textLeft;
QAction *textCenter;
QAction *textRight;
QTextEdit *te;
};
#endif /* TEDIT_H_ */
To copy to clipboard, switch view to plain text mode
implement
#include <QtGui>
#include "tedit.h"
TEdit::TEdit()
{
resize(800,600);
textBold
= new QAction(QIcon(":images/format_text_bold.png"),tr
("B"),
this);
connect(textBold, SIGNAL(triggered()), this, SLOT(makeBold()));
textItalic
= new QAction(QIcon(":images/format_text_italic.png"),tr
("I"),
this);
connect(textItalic, SIGNAL(triggered()), this, SLOT(makeItalic()));
textUnderline
= new QAction(QIcon(":images/format_text_underline.png"),tr
("U"),
this);
connect(textUnderline, SIGNAL(triggered()), this, SLOT(makeUnderline()));
textLeft
= new QAction(QIcon(":images/format_justify_left.png"),tr
("Align Left"),
this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignLeft()));
textCenter
= new QAction(QIcon(":images/format_justify_center.png"),tr
("Align Center"),
this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignCenter()));
textRight
= new QAction(QIcon(":images/format_justify_right.png"),tr
("Align Right"),
this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignRight()));
toolBar->addAction(textBold);
toolBar->addAction(textItalic);
toolBar->addAction(textUnderline);
toolBar->addSeparator();
toolBar->addAction(textLeft);
toolBar->addAction(textCenter);
toolBar->addAction(textRight);
ml->setMargin(1);
ml->setSpacing(0);
ml->addWidget(toolBar);
ml->addWidget(te);
setLayout(ml);
}
void TEdit::makeBold()
{
if(cursor.
charFormat().
fontWeight()==QFont::Normal) {
boldFormat.
setFontWeight(QFont::Bold);
}
else
{
boldFormat.
setFontWeight(QFont::Normal);
}
cursor.mergeCharFormat(boldFormat);
}
void TEdit::makeItalic()
{
if(cursor.charFormat().fontItalic()==false)
{
italicFormat.setFontItalic(true);
}
else
{
italicFormat.setFontItalic(false);
}
cursor.mergeCharFormat(italicFormat);
}
void TEdit::makeUnderline()
{
if(cursor.charFormat().fontUnderline()==false)
{
underlineFormat.setFontUnderline(true);
}
else
{
underlineFormat.setFontUnderline(false);
}
cursor.mergeCharFormat(underlineFormat);
}
void TEdit::setAlignLeft()
{
if(!cursor.hasSelection())
{
}
blockFmt.setAlignment(Qt::AlignLeft);
cursor.mergeBlockFormat(blockFmt);
}
void TEdit::setAlignRight()
{
if(!cursor.hasSelection())
{
}
blockFmt.setAlignment(Qt::AlignRight);
cursor.mergeBlockFormat(blockFmt);
}
void TEdit::setAlignCenter()
{
if(!cursor.hasSelection())
{
}
blockFmt.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(blockFmt);
}
TEdit::~TEdit()
{
// TODO Auto-generated destructor stub
}
#include <QtGui>
#include "tedit.h"
TEdit::TEdit()
{
resize(800,600);
te = new QTextEdit;
toolBar = new QToolBar(this);
textBold = new QAction(QIcon(":images/format_text_bold.png"),tr("B"),this);
connect(textBold, SIGNAL(triggered()), this, SLOT(makeBold()));
textItalic = new QAction(QIcon(":images/format_text_italic.png"),tr("I"),this);
connect(textItalic, SIGNAL(triggered()), this, SLOT(makeItalic()));
textUnderline = new QAction(QIcon(":images/format_text_underline.png"),tr("U"),this);
connect(textUnderline, SIGNAL(triggered()), this, SLOT(makeUnderline()));
textLeft = new QAction(QIcon(":images/format_justify_left.png"),tr("Align Left"),this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignLeft()));
textCenter = new QAction(QIcon(":images/format_justify_center.png"),tr("Align Center"),this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignCenter()));
textRight = new QAction(QIcon(":images/format_justify_right.png"),tr("Align Right"),this);
connect(textLeft, SIGNAL(triggered()), this, SLOT(setAlignRight()));
toolBar->addAction(textBold);
toolBar->addAction(textItalic);
toolBar->addAction(textUnderline);
toolBar->addSeparator();
toolBar->addAction(textLeft);
toolBar->addAction(textCenter);
toolBar->addAction(textRight);
QVBoxLayout *ml = new QVBoxLayout;
ml->setMargin(1);
ml->setSpacing(0);
ml->addWidget(toolBar);
ml->addWidget(te);
setLayout(ml);
}
void TEdit::makeBold()
{
QTextCursor cursor = te->textCursor();
QTextCharFormat boldFormat;
if(cursor.charFormat().fontWeight()==QFont::Normal)
{
boldFormat.setFontWeight(QFont::Bold);
}
else
{
boldFormat.setFontWeight(QFont::Normal);
}
cursor.mergeCharFormat(boldFormat);
}
void TEdit::makeItalic()
{
QTextCursor cursor = te->textCursor();
QTextCharFormat italicFormat;
if(cursor.charFormat().fontItalic()==false)
{
italicFormat.setFontItalic(true);
}
else
{
italicFormat.setFontItalic(false);
}
cursor.mergeCharFormat(italicFormat);
}
void TEdit::makeUnderline()
{
QTextCursor cursor = te->textCursor();
QTextCharFormat underlineFormat;
if(cursor.charFormat().fontUnderline()==false)
{
underlineFormat.setFontUnderline(true);
}
else
{
underlineFormat.setFontUnderline(false);
}
cursor.mergeCharFormat(underlineFormat);
}
void TEdit::setAlignLeft()
{
QTextCursor cursor = te->textCursor();
QTextBlockFormat blockFmt;
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::BlockUnderCursor);
}
blockFmt.setAlignment(Qt::AlignLeft);
cursor.mergeBlockFormat(blockFmt);
}
void TEdit::setAlignRight()
{
QTextCursor cursor = te->textCursor();
QTextBlockFormat blockFmt;
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::BlockUnderCursor);
}
blockFmt.setAlignment(Qt::AlignRight);
cursor.mergeBlockFormat(blockFmt);
}
void TEdit::setAlignCenter()
{
QTextCursor cursor = te->textCursor();
QTextBlockFormat blockFmt;
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::BlockUnderCursor);
}
blockFmt.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(blockFmt);
}
TEdit::~TEdit()
{
// TODO Auto-generated destructor stub
}
To copy to clipboard, switch view to plain text mode
compilation done, no errors or warnings
Bookmarks