[Solved] Using zbar::QZBar
-
I created a Qt project, using a barcode encoder/decoder library libzbarqt. When I added private *qr to mainwindow.h and initialized it by adding qr(new zbar::QZBar) to the MainWindow constructor, it didn't work.
Running it gives:
Starting /home/lily/program/qt/build-TestQZBar-Desktop-Debug/TestQZBar...
The program has unexpectedly finished.
/home/lily/program/qt/build-TestQZBar-Desktop-Debug/TestQZBar exited with code 0Running a debugger gives a segmentation fault:
The inferior stopped because it received a signal from the Operating System.
Signal name :
SIGSEGV
Signal meaning :
Segmentation faultSomething went wrong by adding qr(new zvar::QZBar) to instantiate the pointer...
Missing something obvious...? Will appreciate any help or pointer. The project, header, cpp files, and running environment are as follows.In TestQZBar.pro, added the line below:
unix:!macx: LIBS += -lzbarqt====================mainwindow.h================== =====
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <zbar/QZBar.h>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;
zbar::QZBar *qr;
};#endif // MAINWINDOW_H@
================================================== =========================mainwindow.cpp================ =====
@#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QDebug>
#include <QWidget>MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
qr(new zbar::QZBar)
{
ui->setupUi(this);qDebug() << "Testing QZBar\n"; if(qr->isVideoEnabled()) { qDebug() << "Video is enabled.\n"; } else { qDebug() << "Video is disabled.\n"; }
}
MainWindow::~MainWindow()
{
delete ui;
}@================================================== =====
Environment:
Qt Creator 2.7.1 Based on Qt 5.0.2 (64 bit)
OS is Ubuntu 13.04
Installed libzbarqt-dev (ver.0.10+doc-8) -
Hi,
Never worked with QZBar library, but you might want to check out the QLibrary class.
Also is it a C++ API library or a C library API? And if it is a C++ library what compiler is used? When you want to use a static linked C++ library you MUST use the same compiler as the library. This to do with namemangling by different compilers. E.g. the library is compiled using MingGw32 and your project is compiled with MSVC2010 they won't understand each other.
In your case it appears that the 'new' command is unable to allocate memory. Maybe remove the qr allocation to "code" section of your constructor and place the returned pointer in a local variable. Then check with the debugger if it still reads zero (no memory allocated).
The output application tab in your debugger sometimes reveals mysteries aswell.
Where did you download the library. We can't really test your code without it ;-)
Maybe you also need to include LIBS in your pro file to include the library.
Greetz -
Thanks for your reply and good advice.
Tried QLibrary, and Qt4 instead of Qt5, but the problem remained the same.
The libzbarqt library was installed using apt-get on Ubuntu 13.04. I checked out its source, and found the sources for qt, and for gtk, perl, etc. Zbar and zbarcam written in C are imported. Presumably the library libzbarqt was created by Qt4 using the source at the qt directory.
The qt source is not too big, and maybe I'd better build my project including the source files...
There is another package available on Ubuntu, called QtQr, which is a Python application created by using PyQt4. It uses zbar, and works beautifully. It may not be too hard to translate its source into its original Qt source. It might take a good amount of time for a newbie like me, but it'd be a good practice :) BTW, is there a tool to do that automatically? -
The problem is solved without changing the above code when running qt4 with 32bit gcc compiler :)