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 3723768
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:23:40+00:00 2026-05-19T06:23:40+00:00

I have been able to display an image in a label in Qt using

  • 0

I have been able to display an image in a label in Qt using something like the following:

transformPixels(0,0,1,imheight,imwidth,1);//sets unsigned char** imageData

unsigned char* fullCharArray = new unsigned char[imheight * imwidth];
     for (int i = 0 ; i < imheight ; i++)
         for (int j = 0 ; j < imwidth ; j++)
                fullCharArray[(i*imwidth)+j] = imageData[i][j];

QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_RGB32);

ui->viewLabel->setPixmap(QPixmap::fromImage(*qi,Qt::AutoColor));

So fullCharArray is an array of unsigned chars that have been mapped from the 2D array imageData, in other words, it is imheight * imwidth bytes.

The problem is, it seems like only a portion of my image is showing in the label. The image is very large. I would like to display the full image, scaled down to fit in the label, with the aspect ratio preserved.

Also, that QImage format was the only one I could find that seemed to give me a close representation of the image I am wanting to display, is that what I should expect? I am only using one byte per pixel (unsigned char – values from 0 to 255), and it seems liek RGB32 doesnt make much sense for that data type, but none of the other ones displayed anything remotely correct

edit:
Following dan gallaghers advice, I implemented this code:

QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_RGB32);
int labelWidth = ui->viewLabel->width();
int labelHeight = ui->viewLabel->height();

QImage small = qi->scaled(labelWidth, labelHeight,Qt::KeepAspectRatio);
ui->viewLabel->setPixmap(QPixmap::fromImage(small,Qt::AutoColor));

But this causes my program to “unexpectedly finish” with code 0

  • 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-05-19T06:23:42+00:00Added an answer on May 19, 2026 at 6:23 am

    QImage has a scaled member. So you want to change your setPixmap call to something like:

    QImage small = qi->scaled(labelWidth, labelHeight, Qt::KeepAspectRatio);
    ui->viewLabel->setPixmap(QPixmap::fromImage(small, Qt::AutoColor);
    

    Note that scaled does not modify the original image qi; it returns a new QImage that is a scaled copy of the original.

    Re-Edit:

    To convert from 1-byte grayscale to 4-byte RGB grayscale:

     QImage qi = new QImage(imwidth, imheight, QImage::Format_RGB32);
     for (int i = 0; i < imheight; i++)
     {
         for (int j = 0; j < imwidth; j++)
         {
             qi->setPixel(i, j, QRgb(imageData[i][j], imageData[i][j], imageData[i][j]));
         }
     }
    

    Then scale qi and use the scaled copy as the pixmap for viewLabel.

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

Sidebar

Related Questions

Hi I have been able to extract a VARCHAR to a date using string_to_date
I have been able to zip the contents of my folder. But I would
I have been able to copy the raw data from an otherwise inaccessible USB
I have been able to create ASPX pages without the code behind, but I
I have been able to design my layout so that it has 2 columns
The best that I have been able to come up with is: strlen(preg_replace('/^([\\*]*)\s(.+)/',$1,$line)); ^^That
It seems obvious that some people have been able to figure out how to
My knowledge of xml and php is somewhat basic but have been able to
I'm having problems refreshing .Net 2.0 with IIS 6. I have been able to
I'm trying to install bcrypt-ruby on Windows Vista. So far, I have been able

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.