Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Image Manager/Cvb++/QtStreamDisplayPair

This example program is located in your CVB installation under %CVB%Tutorial/Image Manager/Cvb++/QtStreamDisplayPair.

main.cpp:

// The QtStreamDisplayPair example shows two displays from a GenICam device with Qt. One display shows
// the device's output, the other display one of the planes.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
#include <iostream>
#include <QDebug>
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QIcon>
#include <cvb/device.hpp>
#include <cvb/driver/stream.hpp>
#include <cvb/driver/stream.hpp>
#include <cvb/device_factory.hpp>
#include <cvb/ui/image_view.hpp>
#include <cvb/async/single_stream_handler.hpp>
// MainWidget with two displays (original and plane 0)
class MainWidget
: public QWidget
{
public:
MainWidget()
: QWidget()
{
masterView_ = new Cvb::UI::ImageView(this);
slaveView_ = new Cvb::UI::ImageView(this);
masterView_->SetUploadMode(Cvb::UI::UploadMode::Viewport);
masterView_->CustomWaitForRepaint([&]()
{
masterView_->viewport()->repaint();
slaveView_->viewport()->repaint();
});
slaveView_->SetRefreshMode(Cvb::UI::RefreshMode::UploadOnly);
auto mainLayout = new QHBoxLayout(this);
setLayout(mainLayout);
mainLayout->addWidget(masterView_);
mainLayout->addWidget(slaveView_);
}
void Run(const Cvb::Device & device)
{
// the display of the image
slaveView_->Refresh(device.DeviceImage(), Cvb::UI::AutoRefresh::On);
// the display of the image with plane 0
masterView_->Refresh(device.DeviceImage()->Plane(0).Map());
customAcquisitonHandler_ = CustomStreamHandler::Create(device.Stream(), *this);
customAcquisitonHandler_->Run();
}
protected:
void closeEvent(QCloseEvent *event) override
{
masterView_->SetWaitForRepaintEnabled(false);
QWidget::closeEvent(event);
}
private:
class CustomStreamHandler
: public Cvb::SingleStreamHandler
{
public:
static std::unique_ptr<CustomStreamHandler> Create(const Cvb::StreamPtr& stream, MainWidget& mainWidget)
{
return std::unique_ptr<CustomStreamHandler>(new CustomStreamHandler(stream, mainWidget));
}
~CustomStreamHandler()
{
TryFinish();
}
private:
CustomStreamHandler(const Cvb::StreamPtr& stream, MainWidget& mainWidget)
: Cvb::SingleStreamHandler(stream)
, mainWidget_(mainWidget)
{
}
void HandleAsyncStream(const Cvb::StreamPtr & stream) override
{
auto waitResult = stream->WaitFor(std::chrono::milliseconds(10000));
if (!waitResult.Image)
return;
mainWidget_.masterView_->Refresh(waitResult.Image->Plane(0).Map());
}
MainWidget& mainWidget_;
};
Cvb::UI::ImageView* masterView_ = nullptr;
Cvb::UI::ImageView* slaveView_ = nullptr;
std::unique_ptr<CustomStreamHandler> customAcquisitonHandler_;
};
int main(int argc, char* argv[])
{
try
{
QApplication app(argc, argv);
auto path = Cvb::InstallPath();
path += CVB_LIT("drivers/GenICam.vin");
// use command line argument if provided
if (argc > 1)
{
std::string inputPath(argv[1]);
path = Cvb::String(inputPath.begin(), inputPath.end());
}
// expand environment variables in path
path = Cvb::ExpandPath(path);
// open a device
auto device = Cvb::DeviceFactory::Open(path, Cvb::AcquisitionStack::Vin);
// connect the device image to the UI
MainWidget mainWidget;
mainWidget.setWindowIcon(QIcon(":/qttutorial.png"));
mainWidget.Run(*device);
mainWidget.resize(880, 600);
mainWidget.show();
return app.exec();
}
catch (const std::exception& error)
{
std::cout << error.what() << std::endl;
}
}
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
virtual StreamPtr Stream() const
std::shared_ptr< T > DeviceImage() const
String ExpandPath(const String &path)
std::shared_ptr< Stream > StreamPtr
std::string String
String InstallPath()