【问题标题】:In QT i have function xyz() and i need to return QImage and QString both?在 QT 我有函数 xyz() 并且我需要同时返回 QImage 和 QString ?
【发布时间】:2018-06-20 09:44:14
【问题描述】:

在函数 xyz() 中,我正在计算字符串值和图像数量,我需要返回所有值,如字符串和图像。那么,我需要采取什么返回类型以便它们获取所有值?

<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(); }
}

所以我需要添加返回类型。所以,它们将返回多个数据。

【问题讨论】:

  • 您可以使用QPair&lt;QString, QImage&gt;
  • 您也可以使用std::tuple、std::pair,创建一个类并返回它,将该类与std::any一起使用,有很多可能性。您提供的代码我们无法为您提供太多信息。
  • @King_nak。我通过 Qpair,当我运行这个代码时:- if (!imageData) { return QImage();然后我得到错误。最后我会返回(图像,字符串)我仍然得到错误

标签: qt qstring qimage


【解决方案1】:

您可以为此使用QPair&lt;QString, QImage&gt;,并使用qMakePair 来构建值:

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

然后调用者可以使用.first.second 分别访问字符串和图像:

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

如果您需要扩展到超过 2 个项目,我建议使用自定义结构,例如:

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;

【讨论】:

  • 我是结构体的另一件事。我需要另一个类中的字符串和图像值。现在,我取值。我试过这个 qMakePair(image, string = obj.xyz(); 我需要显示图像和字符串的值,但我无法在图像和字符串中取值
  • 最初图像取值,但当我应用 QPair 时,它什么也不做,我们得到错误(信息不可用)。
  • 我不明白你的意思。你能发布一些代码。如果这与此问题无关,请将其作为新问题发布
  • 还有一件事。当我在 QListwidget 中插入字符串和图像值时,图像被正确放置,但文本需要更大的大小。因为,这只有我的 listwidget 创建水平滚动条。是否有任何方法可以将字符串的大小固定为等于图像大小并且文本下方有额外的文本。我会在 QT 论坛问同样的问题:- forum.qt.io/topic/91845/listwidget-text-size-fixed
  • 你应该把它作为一个单独的问题发布
猜你喜欢
  • 2016-11-27
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2011-04-03
  • 2021-09-27
  • 2019-06-14
相关资源
最近更新 更多