#ifndef WIDGET_H#define WIDGET_H#include <QObject>#include <QTextEdit>#include <QString>class Counter : public QObject{ Q_OBJECTpublic: QTextEdit edit; QTextEdit fintext; Counter(); QString AddColor(QString text); QString ColorChoose();public slots: void SetString();private: QString tekst;};#endif
#include <QtGui/QApplication>#include <QWidget>#include <QPushButton>#include <QTextBrowser>#include <QBoxLayout>#include <QTextEdit>#include <QString>#include <QLabel>#include "counter.h"//////////////////////////////////////////Counter::Counter() : QObject(){}void Counter::SetString(){ tekst=edit.toPlainText(); fintext.setText(AddColor(tekst));}//////////////////////////////////////////int main(int argc, char *argv[]){ QApplication a(argc, argv); QWidget wgt; QPushButton* but = new QPushButton("Go"); Counter counter; QBoxLayout* lo = new QBoxLayout(QBoxLayout::TopToBottom); lo->addWidget(&counter.edit, 1); lo->addWidget(&counter.fintext, 1); lo->addWidget(but, 1); wgt.setLayout(lo); counter.fintext.setReadOnly(true); wgt.resize(600,200); wgt.show(); QObject::connect(but,SIGNAL(clicked()), &counter.fintext,SLOT(SetString())); return a.exec();}QString Counter::AddColor(QString text){ QString Text2; QString temp; for (int i=0;i<text.length();i++) { temp=text[i]; Text2.append("[color="); Text2.append(ColorChoose()); Text2.append("]"); Text2.append(temp); Text2.append("[/color]"); } return Text2;}QString Counter::ColorChoose(){ QString color; int range=15; int min=1; int colorID=rand() % (range+1) + min; switch (colorID) { case 1: color="Blue"; break; case 2: color="Red"; break; case 3: color="Yellow"; break; case 4: color="Purple"; break; case 5: color="Green"; break; case 6: color="Olive"; break; case 7: color="Crimson"; break; case 8: color="Magenta"; break; case 9: color="OrangeRed"; break; case 10: color="DeepPink"; break; case 11: color="DarkOrange"; break; case 12: color="Tomato"; break; case 13: color="Lime"; break; case 14: color="Aqua"; break; default: color="Black"; break; } return color;}