jiveaxe
8th August 2007, 14:42
Hi, I have a problem with my new sample application; I was reading QTextDocument manual and as exercise I wrote an application very simple:
- a textEdit where insert html code
- a button for passing the textEdit->text() to...
- a widget that reimplement paintEvent() so it uses textDocument->setHtml() to render text.
Application compiles right (even if I not wrote the connection); but during execution console fills with these two lines:
QPaintEngine::setSystemClip: Should not be changed while engine is active
QPaintEngine::setSystemRect: Should not be changed while engine is active
and the application seems not responding to commands.
Thanks for the help.
Here the code:
viewer.h
#ifndef VIEWER_H
#define VIEWER_H
class QPainter;
class Viewer: public QWidget
{
public:
Viewer(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
};
#endif
viewer.cpp
#include <QtGui>
#include "viewer.h"
Viewer::Viewer(QWidget *parent)
: QWidget(parent)
{
setStyleSheet("background-color: white");
}
void Viewer::paintEvent(QPaintEvent * /* event */)
{
QPainter *painter = new QPainter(this);
QTextDocument doc;
doc.setHtml("<strong>What's wrong</strong> with <em>this code?</em>");
QAbstractTextDocumentLayout::PaintContext context;
doc.setPageSize(QSize(250,300));
painter->translate(10, 10);
doc.documentLayout()->draw(painter, context);
}
main.cpp
#include <QtGui>
#include "viewer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
window->setWindowTitle("Painter & QTextDocument");
QLabel *htmlStringLabel = new QLabel("HTML code:");
QTextEdit *htmlTextEdit = new QTextEdit;
htmlTextEdit->setMaximumHeight(150);
htmlTextEdit->setMinimumHeight(150);
QPushButton *drawButton = new QPushButton("&Draw");
Viewer *viewer = new Viewer;
viewer->setMinimumHeight(150);
viewer->setMinimumWidth(100);
QHBoxLayout *labelLayout = new QHBoxLayout;
labelLayout->addWidget(htmlStringLabel);
labelLayout->addStretch();
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(drawButton);
buttonLayout->addStretch();
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(labelLayout);
mainLayout->addWidget(htmlTextEdit);
mainLayout->addLayout(buttonLayout);
mainLayout->addWidget(viewer);
mainLayout->setMargin(2);
mainLayout->setSpacing(0);
window->setLayout(mainLayout);
window->resize(400,400);
window->show();
return app.exec();
}
- a textEdit where insert html code
- a button for passing the textEdit->text() to...
- a widget that reimplement paintEvent() so it uses textDocument->setHtml() to render text.
Application compiles right (even if I not wrote the connection); but during execution console fills with these two lines:
QPaintEngine::setSystemClip: Should not be changed while engine is active
QPaintEngine::setSystemRect: Should not be changed while engine is active
and the application seems not responding to commands.
Thanks for the help.
Here the code:
viewer.h
#ifndef VIEWER_H
#define VIEWER_H
class QPainter;
class Viewer: public QWidget
{
public:
Viewer(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
};
#endif
viewer.cpp
#include <QtGui>
#include "viewer.h"
Viewer::Viewer(QWidget *parent)
: QWidget(parent)
{
setStyleSheet("background-color: white");
}
void Viewer::paintEvent(QPaintEvent * /* event */)
{
QPainter *painter = new QPainter(this);
QTextDocument doc;
doc.setHtml("<strong>What's wrong</strong> with <em>this code?</em>");
QAbstractTextDocumentLayout::PaintContext context;
doc.setPageSize(QSize(250,300));
painter->translate(10, 10);
doc.documentLayout()->draw(painter, context);
}
main.cpp
#include <QtGui>
#include "viewer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget;
window->setWindowTitle("Painter & QTextDocument");
QLabel *htmlStringLabel = new QLabel("HTML code:");
QTextEdit *htmlTextEdit = new QTextEdit;
htmlTextEdit->setMaximumHeight(150);
htmlTextEdit->setMinimumHeight(150);
QPushButton *drawButton = new QPushButton("&Draw");
Viewer *viewer = new Viewer;
viewer->setMinimumHeight(150);
viewer->setMinimumWidth(100);
QHBoxLayout *labelLayout = new QHBoxLayout;
labelLayout->addWidget(htmlStringLabel);
labelLayout->addStretch();
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(drawButton);
buttonLayout->addStretch();
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(labelLayout);
mainLayout->addWidget(htmlTextEdit);
mainLayout->addLayout(buttonLayout);
mainLayout->addWidget(viewer);
mainLayout->setMargin(2);
mainLayout->setSpacing(0);
window->setLayout(mainLayout);
window->resize(400,400);
window->show();
return app.exec();
}