Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:58:54+00:00 2026-06-01T02:58:54+00:00

I am using Qt 4.7. I want to: fill text (a character, e.g.’A’) with

  • 0

I am using Qt 4.7. I want to:

  1. fill text (a character, e.g.’A’) with an image
  2. and want that image to be scaled to the size of text (changes with change in font size)
  3. and want that the image be centered vertically and horizontally from the center of that text.

enter image description here

When this is achieved for one character, I want to do the same for many characters in a range (like: ‘a’-‘h’), or characters in a string entered at run-time.

For this currently I am employing this technique:

[ bgTextImage.h ]
QTextCursor cursor;
QTextCharFormat format;
QString imageFileName

[ bgTextImage.cpp ]
cursor = ui->textEdit->textCursor();
QPixmap pic(imageFileName);
QFontMetrics fontMetrics(format.font());
QSize fMSize = fontMetrics.size(0, "A", 0, 0);
QPixmap newPixmap = pic.scaled(fMSize, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);

if(newPixmap.width() > fMSize.width())
{
    int x = (newPixmap.width() - fMSize.width())/2;
    newPixmap = newPixmap.copy(x, newPixmap.rect().y(), fMSize.width(), fMSize.height());
}

if(newPixmap.height() > fMSize.height())
{
    int y = (newPixmap.height() - fMSize.height())/2;
    newPixmap = newPixmap.copy(newPixmap.rect().x(), y, fMSize.width(), fMSize.height());
}

format.setForeground(QBrush(newPixmap));
format.setTextOutline(QPen(borderColor, ui->spinBoxWidth->value()));

ui->textEdit->clear();
cursor.insertText(text, format);

I am getting the results, but the image is not centered properly, as it should when aligned with CSS or other techniques.
I feel that this is a very amateur approach of getting this job done, and thus looking for a better or best-practice, if any. If not then, at least please suggest if there’s any way to improve the current process.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:58:55+00:00Added an answer on June 1, 2026 at 2:58 am

    QPainter has the ability to do image composition (and you can use it outside the paint function). Seems like QPainter::CompositionMode_SourceOver with having the source image be the background, and then combining with your letter image would produce the result you want.

    I hate just referring people to documentation, but the tutorial does have a really good example (ignore the openGL stuff unless you care).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting long text using ajax in json form I want to fill
I want to dynamically render text to an image with a custom font, preferably
Given a rectangular area, I want to render some text using a specific font
Using C# and ASP.NET I want to programmatically fill in some values (4 text
I am using JSON for first time... and want to fill my datagrid with
I want to draw an arc using canvas using a gradient fill. How can
I want using GPS data (I got it from $GPRMC) in an desktop application(that
Using SPList I want to get all the webpart urls that associated with the
I'm using an HTML table for a calendar and I want to fill the
I want to put text into a field that has been autofilled. The traditional

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.