How to overlay QTextEdit contents into background Pixmap?

332 views Asked by At

I am using QT for my IDE.

The intent is to move the QTextEdit text contents into a background Pixmap and keep the transparency such that only the Text shows up as overlap on top of the background pixmap - i.e. other than the text, whatever is in the background will show thru even within the QTextEdit box. My application (not shown) is to "burn in" the QTextEdit contents into the background pixmap to make Banner text etc for Photos.

My attempt was to use grab on the MainWindow widget as it seems the most straightforward. The basic program below that illustrates what I did is below: Curiously, what it displays on MainWindow's screen is what I want. Screenshot is: enter image description here

But the actual in foo.png is

enter image description here

Appreciate any pointers on this or other ways.

Thanks

Sean


#include <QPainter>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>

QTextEdit *text;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    /*
     * Setup a QTextEdit widget
     */
    text = new QTextEdit(this);
    text->viewport()->setAutoFillBackground(false);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    /*
     * On a MousePress, grab the MainWindow's Screen area
     * and save it to a .png file
     * This attempts to overlay the QTextEdit contents on the background
     * by doing essentially a screen capture
     */
    QPixmap pix = this->grab(this->rect());
    pix.save("C:/Users/temp/foo.png");
}

void MainWindow::paintEvent(QPaintEvent *event)
{

    QPainter painter(this);

    //Read in the Background image (could be anything)
    QPixmap pix("C:/Users/temp/photo.jpg");

    /*
     * Move it into somewhere inside main screen so it shows
     * as overlay in MainWindow's view
     */
    text->move(QPoint(40, 40));
    text->show();

    /*
     * Paint the background to MainWindow's view
     */
    painter.drawPixmap(0,0, this->width(), this->height(), pix);

}
0

There are 0 answers