Text is often displayed using a QLabel or QTextEdit widget. If you want more control over how the text is drawn to the screen or the printer, consider using QPainter in a custom widget.
#include <QtCore>
#include <QtGui>
class CustomWidget;
class MainWindow : public QWidget
{
public:
MainWindow();
private:
CustomWidget *drawRegion;
};
class CustomWidget : public QWidget
{
CS_OBJECT(CustomWidget)
public:
CustomWidget(QWidget *parent = nullptr);
protected:
void paintEvent(QPaintEvent *event) override;
};
The declaration for MainWindow includes a pointer of type CustomWidget. The reason we are creating this new class is to override the paintEvent() method. The base implementation is located in QWidget::paintEvent() and by default it does nothing.
MainWindow::MainWindow()
{
setMinimumSize(800, 600);
drawRegion = new CustomWidget;
QPushButton *close_pb = new QPushButton();
close_pb->setText("Close");
QHBoxLayout *layoutButtons = new QHBoxLayout();
layoutButtons->addStretch();
layoutButtons->addWidget(close_pb);
layoutButtons->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->setContentsMargins(40, 35, 40, 20);
layoutMain->addWidget(drawRegion);
layoutMain->addSpacing(20);
layoutMain->addLayout(layoutButtons);
connect(close_pb, &QPushButton::clicked,
this, &QWidget::close);
}
This code creates a new CustomWidget on line 5 and then adds the new widget to the GUI layout on line 18. The only other control visible on the screen is the close push button.
Drawing Text
Now let’s take a look at the constructor for CustomWidget and the implementation of the custom paintEvent() method.
CustomWidget::CustomWidget(QWidget *parent)
: QWidget(parent)
{
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
void CustomWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawText(50, 40, "Text displayed by calling"
" QPainter::drawText()");
QRect rec1(50, 75, 100, 25);
painter.drawRect(rec1);
rec1.adjust(2, 0, 0, 0);
painter.drawText(rec1, "This text will be truncated to"
" fit in the rectangle");
painter.drawText(175, 93, "(Full Text) This text will"
" be truncated to fit in the rectangle");
QRect rec2(50, 145, 550, 40);
painter.drawRect(rec2);
rec2.adjust(2, 2, -2, -2);
QFont font = painter.font();
font.setPointSize(10);
painter.setFont(font);
painter.drawText(rec2, Qt::AlignLeft, "Left Aligned");
painter.drawText(rec2, Qt::AlignCenter, "Center Aligned");
painter.drawText(rec2, Qt::AlignRight | Qt::AlignVCenter,
"Right VCenter");
painter.translate(50, 215);
QRect rec3(0, 0, 550, 200);
painter.drawRect(rec3);
QTextDocument doc;
doc.setTextWidth(rec3.width());
QString text =
"<font face='Arial' color='blue' size='5'>"
"<u>HTML Example</u> </font><br><br>"
"<font size='4'>"
"Another way to draw text is by using QTextDocument"
" and calling setHtml(). <br><br>"
"Text can be formatted by using standard html tags."
"</font>";
doc.setHtml(text);
doc.drawContents(&painter, rec3);
}
There are two lines in the constructor which set the background color of our draw region. Since this code only needs to be called one time it appears here and not in the paintEvent(). Any time the main window is refreshed the paintEvent() method is automatically invoked by the internal event system.
Our custom paintEvent() is where we put the code which will draw in our specified draw region. Line 12 is a basic example of how to display text. The first two arguments specify the x and y coordinates indicating where to place the text. The third argument is the text which will appear on the screen.
The second example starts by drawing a rectangle on the screen. On line 19 the call to drawText() uses this rectangle and the text is passed as the second argument. Since our text is too large for the given rectangle it will be truncated to fit.
On line 24 another rectangle is drawn. This is followed by three calls to drawText() which will left align, center, and right align the given text. Notice in these calls the alignment flags are the second argument and the text is the third argument.
For the last example we start by calling translate() which will adjust the origin of the coordinate system. The call to translate on line 37 will map the current screen coordinates of x=50 and y=215 so we can refer to this point as x=0 and y=0.
The text displayed in this example is drawn by creating a QTextDocument and then calling setHtml(). This approach allows the text to be formatted using standard HTML tags to modify the font, center text, underline, display a table, show a list, and several other options.
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_46”.