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
QPaintEngine::setSystemClip: Should not be changed while engine is active
QPaintEngine::setSystemRect: Should not be changed while engine is active
To copy to clipboard, switch view to plain text mode
and the application seems not responding to commands.
Thanks for the help.
Here the code:
viewer.h
#ifndef VIEWER_H
#define VIEWER_H
{
public:
protected:
};
#endif
#ifndef VIEWER_H
#define VIEWER_H
class QPainter;
class Viewer: public QWidget
{
public:
Viewer(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
};
#endif
To copy to clipboard, switch view to plain text mode
viewer.cpp
#include <QtGui>
#include "viewer.h"
{
setStyleSheet("background-color: white");
}
{
doc.setHtml("<strong>What's wrong</strong> with <em>this code?</em>");
doc.
setPageSize(QSize(250,
300));
painter->translate(10, 10);
doc.documentLayout()->draw(painter, context);
}
#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);
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QtGui>
#include "viewer.h"
int main(int argc, char *argv[])
{
window->setWindowTitle("Painter & QTextDocument");
htmlTextEdit->setMaximumHeight(150);
htmlTextEdit->setMinimumHeight(150);
Viewer *viewer = new Viewer;
viewer->setMinimumHeight(150);
viewer->setMinimumWidth(100);
labelLayout->addWidget(htmlStringLabel);
labelLayout->addStretch();
buttonLayout->addStretch();
buttonLayout->addWidget(drawButton);
buttonLayout->addStretch();
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks