Problem with QImage and,or setPixmap



  • 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


  • Moderators

    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.



  • @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));
    


  • 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);


  • Moderators

    @Checksum1990 Can you show the 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->resize(Image.size);
    label->setPixmap(QPixmap::fromImage(Image));
    

    }

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


  • Moderators

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

    label->resize(Image.size);

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



  • 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)



  • 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!!!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.