(C++) LibcvautomationExample5
February 24, 2017 · View on GitHub
(C++) LibcvautomationExample5



libcvautomation example 5: basic Qt GUI is a libcvautomation example of a Qt Creator project with a basic GUI to give commands.
- View a screenshot of 'CppLibcvautomationExample5' (png)
- Download the Qt Creator project 'CppLibcvautomationExample5' (zip)
Technical facts
Operating system(s) or programming environment(s)
Lubuntu 15.04 (vivid)
Qt Creator 3.1.1
- G++ 4.9.2
Libraries used:
Qt project file: ./CppLibcvautomationExample5/CppLibcvautomationExample5.pro
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TEMPLATE = app SOURCES += \ main.cpp \ qtdialog.cpp HEADERS += \ qtdialog.h FORMS += qtdialog.ui QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Werror # # # libcvautomation # # add path to #include paths, link statically to code # # INCLUDEPATH += ../../Libraries/libcvautomation/include SOURCES += \ ../../Libraries/libcvautomation/libcvautomation/libcvautomation-xtest.c \ ../../Libraries/libcvautomation/libcvautomation/libcvautomation-xlib.c \ ../../Libraries/libcvautomation/libcvautomation/libcvautomation-opencv.c # Link to X11 library # undefined reference to `XOpenDisplay' LIBS += -lX11 # Link to Xtst library # undefined reference to `XTestQueryExtension' LIBS += -lXtst # Link to OpenCV library # undefined reference to `cvFree_' unix { CONFIG += link_pkgconfig PKGCONFIG += opencv }
./CppLibcvautomationExample5/main.cpp
#include "qtdialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); QtDialog w; w.show(); return a.exec(); }
./CppLibcvautomationExample5/qtdialog.h
#ifndef QTDIALOG_H #define QTDIALOG_H #include <QDialog> namespace Ui { class QtDialog; } class QtDialog : public QDialog { Q_OBJECT public: explicit QtDialog(QWidget *parent = 0); ~QtDialog(); private slots: void on_button_clicked(); private: Ui::QtDialog *ui; //From http://www.richelbilderbeek.nl/CppSeperateString.htm static const std::vector<std::string> SeperateString( const std::string& input, const char seperator); }; #endif // QTDIALOG_H
./CppLibcvautomationExample5/qtdialog.cpp
#include "qtdialog.h" #include <string> #include <vector> #include <boost/algorithm/string/split.hpp> #include <boost/lexical_cast.hpp> #include "ui_qtdialog.h" //#include <X11/keysymdef.h> //To find keymap extern "C" { #include "libcvautomation/libcvautomation.h" } QtDialog::QtDialog(QWidget *parent) : QDialog(parent), ui(new Ui::QtDialog) { ui->setupUi(this); } QtDialog::~QtDialog() { delete ui; } void QtDialog::on_button_clicked() { #ifdef OLD_SKOOL_APPROACH_THAT_WORKS const std::string display_location = ""; Display * const display = XOpenDisplay( display_location.c_str() ); assert(display); const std::string command_str = "mousexy 100 100"; char * const command = new char[ command_str.size() + 1 ]; strcpy(command,&command_str[0]); xte_commandString(display,command,1,0,std::numeric_limits<int>().max(),1); XCloseDisplay( display ); delete[] command; #endif ui->output->clear(); const std::string text = ui->input->toPlainText().toStdString(); const std::vector<std::string> lines = SeperateString(text,'\n'); for (const std::string& line: lines) { //Open a display (whatever that may be) const std::string display_location = ""; Display * const display = XOpenDisplay( display_location.c_str() ); assert(display); //Convert the line (with the command) to a C style string char * const command = new char[ line.size() + 1 ]; strcpy(command,&line[0]); //Run the command const cvaPoint p = xte_commandString(display,command,1,0,std::numeric_limits<int>().max(),1); //Close the display (whatever that may be) XCloseDisplay( display ); //Don't forget to delete a C-style string :-( delete[] command; //Give some feedback std::stringstream s; s << "Execute command: '" << line << "': "; if (p.x == -2 && p.y == -2) { s << "OK"; } else { s << "(" << p.x << "," << p.y << ")"; } ui->output->appendPlainText(s.str().c_str()); } //Show the display again, when a mouseclick has caused it to hide this->hide(); this->show(); } const std::vector<std::string> QtDialog::SeperateString( const std::string& input, const char seperator) { std::vector<std::string> v; boost::algorithm::split(v,input, std::bind2nd(std::equal_to<char>(),seperator), boost::algorithm::token_compress_on); return v; }