Cvb/QmlStreamDisplay

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 
17 #include <QApplication>
18 #include <QQuickView>
19 #include <QQmlContext>
20 #include <QIcon>
21 
22 
23 
24 
25 
26 
27 #include <cvb/ui/image_view_item.hpp>
28 #include <cvb/device_factory.hpp>
29 #include <cvb/image.hpp>
30 #include <cvb/async/single_stream_handler.hpp>
31 
32 int main(int argc, char* argv[])
33 {
34  try
35  {
36 
37  QApplication app(argc, argv);
38 
39  // register QML components for an image display
41 
42  QQuickView view;
43  view.setResizeMode(QQuickView::SizeRootObjectToView);
44  view.setIcon(QIcon(":/qttutorial.png"));
45  auto context = view.rootContext();
46  // create a controller object to communicate with QML
47  Cvb::UI::ImageController imageController;
48  context->setContextProperty("mainImage", &imageController);
49 
50  // load main QML file
51  view.setSource(QUrl::fromLocalFile("../main.qml"));
52 
53  // open a device
54  auto device = Cvb::DeviceFactory::Open(Cvb::InstallPath() + CVB_LIT("drivers/CVMock.vin"), Cvb::AcquisitionStack::Vin);
55 
56  // refresh the image display through the controller object
57  imageController.Refresh(device->DeviceImage(), Cvb::UI::AutoRefresh::On);
58 
59  // get the first stream of the device
60  // or use device->Stream(index) to get another stream
61  auto stream = device->Stream();
62 
63  // create an acquisition handler for that stream and run it.
64  // Actually the guard starts it, and will finish it when destructed.
65 
66  // There are two ways to run the acquisition:
67  // - Either via stream->Start() and stream->Wait() in a separate thread
68  // - or via SingleStreamHandler (Run() / Finish())
69  Cvb::StreamHandlerGuard guard(Cvb::SingleStreamHandler::Create(stream), Cvb::AutoRun::Yes);
70 
71 
72  view.resize(640, 480);
73  view.show();
74 
75 
76 
77  return app.exec();
78  }
79  catch (const std::exception& error)
80  {
81  std::cout << error.what() << std::endl;
82  }
83 }
void setSource(const QUrl &url)
void setResizeMode(QQuickView::ResizeMode)
void show()
QQmlContext * rootContext() const const
static void Register()
Convenience method to register this type in QML.
Definition: image_view_item.hpp:669
static std::unique_ptr< SingleStreamHandler > Create(const Driver::StreamPtr &stream)
Create a stream handler object.
Definition: decl_single_stream_handler.hpp:46
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:295
Handler guard object to safely run and finish a stream handler.
Definition: decl_multi_stream_handler.hpp:289
STL class.
void resize(const QSize &newSize)
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
Opens a device with the given provider with its default board and port (if applicable).
Definition: decl_device_factory.hpp:50
QUrl fromLocalFile(const QString &localFile)
void setIcon(const QIcon &icon)

UI definition using QML.

1 import QtQuick 2.3
2 import CvbQuick 1.0 as CvbQuick
3 
4 
5 Rectangle
6 {
7 
8  CvbQuick.ImageView
9  {
10  id: imageView
11  image : mainImage
12  anchors.fill : parent
13  uploadMode : CvbQuick.UploadMode.Image
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 }