CVB++ 15.0
Cvb/QmlImageDisplay

This example requires Qt5 >= 5.9 setup for building.

You may build it with Ubuntu 18.04's default Qt5 after installing:

1// ---------------------------------------------------------------------------
4// ---------------------------------------------------------------------------
5
6#include <iostream>
7
8
9
10
11
12
13
14
15
16#include <QApplication>
17#include <QQuickView>
18#include <QQmlContext>
19#include <QIcon>
20
21
22
23
24#include <cvb/ui/image_view_item.hpp>
25
26
27
28#include <cvb/image.hpp>
29
30int main(int argc, char* argv[])
31{
32 try
33 {
34
35 QApplication app(argc, argv);
36
37 // register QML components for an image display
39
40 QQuickView view;
41 view.setResizeMode(QQuickView::SizeRootObjectToView);
42 view.setIcon(QIcon(":/qttutorial.png"));
43 auto context = view.rootContext();
44 // create a controller object to communicate with QML
45 Cvb::UI::ImageController imageController;
46 context->setContextProperty("mainImage", &imageController);
47
48 // load main QML file
49 view.setSource(QUrl::fromLocalFile("../main.qml"));
50
51
52 // load the image
53 Cvb::ImagePtr image = Cvb::Image::Load(Cvb::InstallPath() + CVB_LIT("tutorial/ClaraRGB.bmp"));
54
55 // refresh the image display through the controller object
56 imageController.Refresh(image);
57
58 view.resize(640, 480);
59 view.show();
60
61
62
63 return app.exec();
64 }
65 catch (const std::exception& error)
66 {
67 std::cout << error.what() << std::endl;
68 }
69}
static std::unique_ptr< Image > Load(const String &fileName)
Loads an image with the given file name.
Definition: detail_image.hpp:32
Controller object for the QML image view item.
Definition: image_view_item.hpp:252
void Refresh(const ImagePtr &image, AutoRefresh autoRefresh=AutoRefresh::Off)
Share the image and refresh the view.
Definition: image_view_item.hpp:294
static void Register()
Convenience method to register this type in QML.
Definition: image_view_item.hpp:667
void setResizeMode(QQuickView::ResizeMode)
QQmlContext * rootContext() const const
void setSource(const QUrl &url)
QUrl fromLocalFile(const QString &localFile)
void resize(const QSize &newSize)
void setIcon(const QIcon &icon)
void show()

UI definition using QML.

1import QtQuick 2.3
2import CvbQuick 1.0 as CvbQuick
3
4
5Rectangle
6{
7
8 CvbQuick.ImageView
9 {
10 id: imageView
11 image : mainImage
12 anchors.fill : parent
13 uploadMode : CvbQuick.UploadMode.Viewport
14
15 }
16
17 Text
18 {
19 text: "Cursor: "
20 + String(imageView.hoverPosition)
21 + "\nPixel: "
22 + String(imageView.hoverPixel)
23 + "\nZoom: "
24 + String(imageView.zoomFactor.toFixed(2))
25 anchors.horizontalCenter: parent.horizontalCenter
26 font.pointSize: 15
27 font.bold: true
28 color : "white"
29 style: Text.Outline
30 styleColor: "black"
31 }
32
33
34}