Skip to content
  • 0 Votes
    4 Posts
    3k Views
    T

    Since you seem to have some problems with QRegularExpression (Read the documentation! It's the best I have ever seen!), I will give you a small example. I happen to be using something like this in a program I am working on:

    QRegularExpression re("<img (?<junk>.*?) src=(?<path>\\S+?) (?<junk2>.*?)>"), QRegularExpression::CaseInsensitiveOption);

    The ?<name>-blocks are there to provide named captures in the matches. The first one is called junk - it is allowed to contain anything but the "src"-attribute (.*? matches any sign in a "lazy" manner). junk2 behaves equivalently; if you don't need to capture (i.e. store for later access) these groups, you could simplify the expression a bit.
    path will be storing the download-link for the image. \\S will match every non-whitespace character, +? means that there has to be at least one character like that. You will have to adapt to path including "-signs, depends on your specific case.

    Here a bit of code I copied and slightly modified from the documentation for QRegularExpression:

    QString s = "<img style=\"background-repeat: no-repeat; background-size: 100% 100%; vertical-align: -0.838ex;height: 2.843ex;\" src=http://en.wikitolearn.org/index.php?title=Special:MathShowImage&hash=2af9544640fe8b97375512027efaaccd&mode=mathml>"; QRegularExpressionMatch match = re.match(s); while (match.hasMatch()) { QString junk = match.captured("junk"); // junk == style=\"background-repeat: no-repeat; background-size: 100% 100%; vertical-align: -0.838ex;height: 2.843ex;\" QString junk2 = match.captured("junk2"); // junk2 == "" QString path = match.captured("path"); // path == http://en.wikitolearn.org/index.php?title=Special:MathShowImage&hash=2af9544640fe8b97375512027efaaccd&mode=mathml }

    Hand in your html-file instead of s, take care of some minute details (path enclosed in " or not? Maybe no space before junk2? etc.), and then you should be able to do whatever you want with your links.
    I hope that gets you started.

  • 0 Votes
    3 Posts
    3k Views
    robolivableR

    Okay, so the idea here is to utilize the Item element's grabToImage method to save everything inside the element to a file. Placing the Image inside the Item, and calling the method provides the feature I was looking for. Thanks, @p3c0!

    Reference code:

    Item { id: "itemInQuestion"; Image { id: myImage; source: "http://www.arbitrarySource.com/arbitraryImage.jpeg" } } // ... MouseArea { id: "someMouseArea"; onClicked: { itemInQuestion.grabToImage(function (result) { result.saveToFile("/arbitrary/file/system/path/myArbitraryImage.jpeg"); }); } }

    EDIT: I hadn't realized the grabToImage method is an inherited method in Image. The use of a wrapping Item is unnecessary in this case. I should also note that this method does not save the actual image, but a lower resolution copy, as quoted from the documentation: "This function will render the item to an offscreen surface and copy that surface from the GPU's memory into the CPU's memory, which can be quite costly." Is there a better way of saving the full resolution image to a file without having to request it from it's source a second time? This sort of brings me back to my original question.