Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QImage and,or setPixmap
Forum Updated to NodeBB v4.3 + New Features

Problem with QImage and,or setPixmap

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 5.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Checksum1990
    wrote on last edited by
    #1

    Hi, im new to Qt. I have played around with qt and the different examples provided by qt. But i have one problem with showing an image. I get the top left part of an image only. You can see it here:
    0_1501237775649_Screenshot from 2017-07-28 12-29-12.png

    here you can see my code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <opencv2/opencv.hpp>
    #include <QImage>
    #include <QLabel>
    #include <QPixmap>

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

    //Read in image with OpenCV
    cv::Mat img = cv::imread("/home/sebastian/Downloads/penguins.jpg");
    
    //BGR (OpenCV) to RGB (Qt)
    cvtColor(img, img, CV_BGR2RGB);
    
    QLabel *label = new QLabel(this);
    QImage Image(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
    label->setPixmap(QPixmap::fromImage(Image));
    

    }

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

    could you help me with this problem?
    Thank you in advance

    raven-worxR J.HilkJ 2 Replies Last reply
    0
    • C Checksum1990

      Hi, im new to Qt. I have played around with qt and the different examples provided by qt. But i have one problem with showing an image. I get the top left part of an image only. You can see it here:
      0_1501237775649_Screenshot from 2017-07-28 12-29-12.png

      here you can see my code:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <opencv2/opencv.hpp>
      #include <QImage>
      #include <QLabel>
      #include <QPixmap>

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

      //Read in image with OpenCV
      cv::Mat img = cv::imread("/home/sebastian/Downloads/penguins.jpg");
      
      //BGR (OpenCV) to RGB (Qt)
      cvtColor(img, img, CV_BGR2RGB);
      
      QLabel *label = new QLabel(this);
      QImage Image(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
      label->setPixmap(QPixmap::fromImage(Image));
      

      }

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

      could you help me with this problem?
      Thank you in advance

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      I don't know OpenCV very well. But here are some thoughts.

      @Checksum1990 said in Problem with QImage and,or setPixmap:

      QImage::Format_RGB888

      might probably be QImage::Format_ARGB32_Premultiplied?

      @Checksum1990 said in Problem with QImage and,or setPixmap:

      cvtColor(img, img, CV_BGR2RGB);

      Also i think you shouldn't do the conversion in and from the same pointer and use a tmp buffer instead.

      Also check this.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • C Checksum1990

        Hi, im new to Qt. I have played around with qt and the different examples provided by qt. But i have one problem with showing an image. I get the top left part of an image only. You can see it here:
        0_1501237775649_Screenshot from 2017-07-28 12-29-12.png

        here you can see my code:

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <opencv2/opencv.hpp>
        #include <QImage>
        #include <QLabel>
        #include <QPixmap>

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

        //Read in image with OpenCV
        cv::Mat img = cv::imread("/home/sebastian/Downloads/penguins.jpg");
        
        //BGR (OpenCV) to RGB (Qt)
        cvtColor(img, img, CV_BGR2RGB);
        
        QLabel *label = new QLabel(this);
        QImage Image(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
        label->setPixmap(QPixmap::fromImage(Image));
        

        }

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

        could you help me with this problem?
        Thank you in advance

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Checksum1990
        IIRC, you'll need to adjust your label to the size of your pixmap, or your pixmap to the size of your label.

        with just setPixmap the label will draw what it can, and thats it.

        QLabel *label = new QLabel(this);
        QImage Image(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
        
        //Either
        QPixmap pMap = QPixmap::fromImage(Image).scaled(label.size(),Qt::KeepAspectRatio);
        //Or
        label->resize(Image.size);
        
        label->setPixmap(QPixmap::fromImage(Image));
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Checksum1990
          wrote on last edited by Checksum1990
          #4

          Thank you guys for your answers. After adding "label->resize(Image.size)" i got this error:

          /home/sebastian/Qt/My_Projects/OpenCV_project/mainwindow.cpp:24: error: invalid use of non-static member function
          label->resize(Image.size);

          jsulmJ 1 Reply Last reply
          0
          • C Checksum1990

            Thank you guys for your answers. After adding "label->resize(Image.size)" i got this error:

            /home/sebastian/Qt/My_Projects/OpenCV_project/mainwindow.cpp:24: error: invalid use of non-static member function
            label->resize(Image.size);

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Checksum1990 Can you show the code?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Checksum1990
              wrote on last edited by
              #6

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              #include <opencv2/opencv.hpp>
              #include <QImage>
              #include <QLabel>
              #include <QPixmap>

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

              //Read in image with OpenCV
              cv::Mat img = cv::imread("/home/sebastian/Downloads/penguins.jpg");
              
              //BGR (OpenCV) to RGB (Qt)
              cvtColor(img, img, CV_BGR2RGB);
              
              QLabel *label = new QLabel(this);
              QImage Image(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
              label->resize(Image.size);
              label->setPixmap(QPixmap::fromImage(Image));
              

              }

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

              raven-worxR 1 Reply Last reply
              0
              • C Checksum1990

                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                #include <opencv2/opencv.hpp>
                #include <QImage>
                #include <QLabel>
                #include <QPixmap>

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

                //Read in image with OpenCV
                cv::Mat img = cv::imread("/home/sebastian/Downloads/penguins.jpg");
                
                //BGR (OpenCV) to RGB (Qt)
                cvtColor(img, img, CV_BGR2RGB);
                
                QLabel *label = new QLabel(this);
                QImage Image(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
                label->resize(Image.size);
                label->setPixmap(QPixmap::fromImage(Image));
                

                }

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

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                @Checksum1990 said in Problem with QImage and,or setPixmap:

                label->resize(Image.size);

                should be label->resize(Image.size());

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  Checksum1990
                  wrote on last edited by Checksum1990
                  #8

                  It worked great!!!! Thank you
                  Now the opened window is to small for the picture how can i fix it?
                  ( i have to pull on the edges of the window)

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Checksum1990
                    wrote on last edited by Checksum1990
                    #9

                    I found out when i edit the main.ccp like this:

                    #include "mainwindow.h"
                    #include <QApplication>
                    #include <QDesktopWidget>

                    int main(int argc, char *argv[])
                    {
                    QApplication a(argc, argv);
                    MainWindow w;
                    //set fixed window size
                    QDesktopWidget dw;
                    int x=dw.width()*0.7;
                    int y=dw.height()*0.7;
                    w.setFixedSize(x,y);

                    w.show();
                    
                    return a.exec();
                    

                    }

                    ...i have the appropriate window size

                    Thanks for your help guys!!!!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved