0

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?

<Return-Type> MainWindow::xyz(QString m_ImgPath, int i)
{
    try
    {
        m_ImgPath = Array[i];
        QByteArray m_path = m_ImgPath.toLocal8Bit();
        char* ImagePath = m_path.data();

      obj *m_ThumpDCMReader = obj::New();
        TReader->SetFileName(ImagePath);
        TReader->Update();
        //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 the return type.So, they will return multiple data.

Amar Yadav
  • 29
  • 9
  • 1
    You can use a `QPair` – king_nak Jun 20 '18 at 09:48
  • 1
    You can also use std::tuple, std::pair, create a class and return it, use that class with std::any, there is many possibility. we cannot give you much informations with the code you provided. – Gabrielle de Grimouard Jun 20 '18 at 09:51
  • @King_nak. I go through Qpair, When i run code this one:- if (!imageData) { return QImage(); } then i am getting error.and lastly I will return (image,string )Still i am getting error – Amar Yadav Jun 20 '18 at 10:17

1 Answers1

1

You can use a QPair<QString, QImage> for that, and use qMakePair to build the values:

QPair<QString, QImage> MainWindow::xyz(QString m_ImgPath, int i) {
    try {
        // ...
        return qMakePair(string, Image);
    } catch (...) {
        return qMakePair(QString(), QImage());
    }
}

The caller can then use .first and .second to access the string and image, resp:

auto values = xyz("",0); // or QPair<QString, QImage> values = xyz("",0);
auto str = values.first;
auto img = values.second;

If you need to extend to more then 2 items, I suggest to use a custom struct, e.g.:

struct StringWithImage {
    QString string;
    QImage image;
};

// In your return:
return StringWithImage{ string, Image };

// Usage:
auto values = xyz("", 0);
auto str = values.string;
auto img = values.image;
king_nak
  • 11,313
  • 33
  • 58
  • One More Thing Where I am a struct. I need the string and image value in another class. Now, m I taking the value. I tried this one qMakePair(image, string = obj.xyz(); and I need to display the value of image and string but I am not able to take value inside the image and string – Amar Yadav Jun 20 '18 at 11:20
  • Initially image taking the value but when I apply the QPair it takes nothing and error we are getting (Information not available). – Amar Yadav Jun 20 '18 at 13:24
  • I don't understand what you mean. Can you post some code. If that is unrelated to this issue, post it as a new question – king_nak Jun 20 '18 at 13:31
  • One more thing. When I insert the string and image value inside the QListwidget image is placed properly but text take more size. For, this only my listwidget create the horizontal scrollbar. Is there any method to fix the size of string equal to the image size and extra text comes below the text. I will ask the same question in QT forum:- https://forum.qt.io/topic/91845/listwidget-text-size-fixed – Amar Yadav Jun 21 '18 at 09:49
  • You should post that as a separate question – king_nak Jun 21 '18 at 09:56
  • Sorry, but my question limit is over. If possible would you please solve my issue. – Amar Yadav Jun 21 '18 at 09:59
  • I cannot. There is too little information. I can only hint you to (`QListView::setWordWrap`)[https://doc.qt.io/qt-5/qlistview.html#wordWrap-prop]. Ask a new proper question when you can again – king_nak Jun 21 '18 at 10:08