Qt World Summit: Register Today!

In QT i have function xyz() and i need to return QImage and QString both?

  • In the function xyz(), I am calculating the string value and number of image and I need to return all the value like string and image. So, What I need to take the return type so they will take all value?

            m_ImgPath = Array[i];
            QByteArray m_path = m_ImgPath.toLocal8Bit();
            char* ImagePath = m_path.data();
          obj *m_ThumpDCMReader = obj::New();
            //const QString string = NULL;
            const char *str_uchar = TReader->GetMetaData()->GetAttributeValue(DC::string).GetCharData();
            string = QString::fromUtf8((char *)str_uchar);
            SPointer<ImageData> imageData = TReader->GetOutput();
            if (!imageData) { return QImage(); }
            /// \todo retrieve just the UpdateExtent
            int width = imageData->GetDimensions()[0];
            int height = imageData->GetDimensions()[1];
            QImage image(width, height, QImage::Format_RGB32);
            QRgb *rgbPtr =  reinterpret_cast<QRgb *>(image.bits()) + width * (height - 1);
            unsigned char *colorsPtr = reinterpret_cast<unsigned char *>(imageData->GetScalarPointer());
            for (int row = 0; row < height; row++)
                for (int col = 0; col < width; col++)
                    *(rgbPtr++) = QColor(colorsPtr[0], colorsPtr[1], colorsPtr[2]).rgb();
                    colorsPtr += imageData->GetNumberOfScalarComponents();
                rgbPtr -= width * 2;
            return (Image,string)
        catch (...) { return QImage(); }

    SO what I need to add the return type. So, they will return multiple data.

  • Lifetime Qt Champion


    Either create a small structure containing your data or use std::tuple.

  • @SGaistI am using QPair<QString , QImagre> and return qMakePair(string,image);
    but the problem is how to receive the qMakePair value ??

  • Lifetime Qt Champion

    Right, I forgot about QPair.

    It's just:

    QPair<QString , QImage> returnValue = xyzy();

  • I have one another class, I want to use the value inside the class and I will make QString as string and QImage as image and I want to update the value inside the string and image. How i will update the same value inside it.

  • Lifetime Qt Champion

    You need to make access to it in some form or another.

    #include <QPair>
    #include <QDebug>
    using MyPair = QPair<QString, QImage >; // to make it better to read
    class DataClass {
      MyPair ThePair; // your pair data
      DataClass() { // constructor. adds something to the pair
        ThePair.first = "first";
        ThePair.second = QImage(); // blank image
      MyPair& getPair() { // access function. returning a reference to the pair
        return ThePair;
    int main(int argc, char* argv[]) {
      QApplication a(argc, argv);
      DataClass dc;
      MyPair& pair = dc.getPair();
      qDebug() << "org:"  << pair.first;
      // change it
      pair.first = "the string";
      pair.second = QImage();
      qDebug() << "changed:" << pair.first;

    org: "first"
    changed: "the string"

Log in to reply