Writing C++ code for image viewers

Let's get started by using the following steps:

  1. First, open up mainwindow.h and add the following headers:
#include <QMainWindow> 
#include <QFileDialog> 
#include <QPixmap> 
#include <QPainter>
  1. Then, add the following variable, called imageBuffer, which will serve as the pointer that points to the actual image data before rescaling. Then, add the functions as well:
private: 
   Ui::MainWindow *ui; 
   QPixmap* imageBuffer; public:   void resizeImage();   void paintEvent(QPaintEvent *event);public slots:   void on_actionOpen_triggered();
  1. Next, open up mainwindow.cpp and initialize the imageBuffer variable in the class constructor:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) ...

Get Hands-On GUI Programming with C++ and Qt5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.