I solve it. I added the following code.
QImage convertedImg = unique_img->convertToFormat(QImage::Format_RGBA8888);
The image format has been converted. And now it works fine.
std::unique_ptr<QImage> unique_img(new QImage(reader.read()));
if (unique_img->isNull()) {
throw std::runtime_error("Cannot create image from file");
}
QImage convertedImg = unique_img->convertToFormat(QImage::Format_RGBA8888);
int width = convertedImg.size().width();
int height = convertedImg.size().height();
uint8_t* output;
output = nullptr;
size_t output_size = WebPEncodeRGBA(reinterpret_cast<uint8_t*>(convertedImg.bits()), width, height, width * 4, 80, &output);