Qt 5 with USB video grabber - qt

My demo code is to choose a camera from the integrated camera of my laptop, and a USB video grabber (STK1160). My code is attached.
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QCamera>
#include <QCameraInfo>
#include <QCameraImageCapture>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QList <QCameraInfo> camList;
QCamera *camera;
private slots:
void onCameraChanged(int);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
camera = NULL;
connect(ui->cameraComboBox,static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this,
&MainWindow::onCameraChanged);
// find all available cameras and put them in the combo box
camList = QCameraInfo::availableCameras();
for(QList <QCameraInfo>::iterator it = camList.begin();it!=camList.end();++it) {
ui->cameraComboBox->addItem(it->description());
}
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::onCameraChanged(int idx) {
if(camera != NULL) {
camera->stop();
}
camera = new QCamera(camList.at(idx),this);
camera->setViewfinder(ui->viewfinder);
camera->setCaptureMode(QCamera::CaptureStillImage);
camera->start();
}
My problem is that when I choose the USB grabber from the combo box, I got the following error message:
libv4l2: error turning on stream: Message too long
CameraBin error: "Error starting streaming on device '/dev/video1'."
CameraBin error: "Could not negotiate format"
and the camera view is all black. Anyone has any idea? I have tested my video input on an AV screen, it works well.

I don't have a very concrete answer, but a more general one about camera & media APIs inside Qt5. In my experience some features only work on some platforms while other work on other platforms. For example I am currently struggling with having QVideoProbe work on Ubuntu even if it works fine on Android.
And development focus by Qt developers seems to be 80% on the mobile platforms recently. Also on Linux platforms the "backend" for video is gstreamer, which means that most errors originate there. My best tip is to upgrade to Qt 5.6 which relies on gstreamer1.0 instead of the ancient gstreamer0.1. Also make sure to install all the gstreamer plugins etc for your platform, as this may have a big impact on how well media works.
Also if you can reproduce the error directly in gstreamer, you might be able to find a fix for it there and this fix will then be available from Qt as well. For example if you are missing a codec or driver, using the gstreamer tools to add the support you need might solve the problem
I find the media APIs in Qt to be solid and I know that work is continuously being made to fill in the massing features in each platform's media back-end, so for each update of Qt more features should be added and bugs should be fixed.
I hope this helps, even if it does not address your question directly (which probably is because very few have the exact experience that you had).

Related

Garbage pixels in transparent background of QSystemTrayIcon

I'm trying to make a system tray icon using Qt 5.10.0. The icon would be non-rectangular (text actually). My current code works normally on a KDE Plasma panel, but on XFCE4-panel it appears to have garbage in the background, whenever icon's pixel is transparent. The garbage typically represents pieces of some of the already present icons in the system tray, but sometimes has some pieces of other windows.
Almost all other applications' icons look clean, including Qt-based apps like QBittorrent, Klipper, KTorrent, as well as GTK-based (Pidgin). The only exception is Dropbox and my code. Notable difference is that my code, as well as Dropbox, are both Qt5-based, while the above mentioned correctly-looking Qt applications are Qt4-based. Compiling my code for Qt4 indeed doesn't show the problem.
Below is the code. What am I doing wrong here?
#include <QTimer>
#include <QPixmap>
#include <QPainter>
#include <QApplication>
#include <QSystemTrayIcon>
class MyTrayIcon : public QSystemTrayIcon
{
Q_OBJECT
QTimer updateTimer;
public:
MyTrayIcon()
{
connect(&updateTimer, SIGNAL(timeout()), this, SLOT(updateIcon()));
updateTimer.start(2*1000);
updateIcon();
}
private:
Q_SLOT void updateIcon()
{
const int size=22;
QPixmap pixmap(size,size);
// Make sure there's no garbage in the pixmap
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setPen(QColor(0,96,192));
painter.drawText(pixmap.rect(), Qt::AlignCenter, "5");
setIcon(pixmap);
}
};
int main(int argc, char** argv)
{
QApplication app(argc,argv);
MyTrayIcon trayIcon;
trayIcon.show();
return app.exec();
}
#include "temps.moc"

Image not shown using Qpixmap with QT5.4 and mvs2013 x64

I am using QT (version 5.4 msvc2013_64) with of-course vs2013. The compilation is successful but when running not every thing functions. Before with vs2010 and QT (i think version 5.1 msvc_2010_opengl do not remember exactly) every thing was working but now things are wried which maybe typical with the start of newer versions but need to be resolved. I also got other issues with this but lets get this resolved first as maybe others will be resolved too else I will post another question for the other problems. Of-course If i do not see a solution to this problem with the current QT and vs I will switch to vs2012 (requires downloading and installing); Maybe then everything will work as before. Thanks in advance.
ui_ta7feezquran.h
/********************************************************************************
** Form generated from reading UI file 'ta7feezquran.ui'
**
** Created by: Qt User Interface Compiler version 5.4.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_TA7FEEZQURAN_H
#define UI_TA7FEEZQURAN_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Ta7feezQuranClass
{
public:
QWidget *centralWidget;
QLabel *a;
QMenuBar *menuBar;
QToolBar *mainToolBar;
void setupUi(QMainWindow *Ta7feezQuranClass)
{
if (Ta7feezQuranClass->objectName().isEmpty())
Ta7feezQuranClass->setObjectName(QStringLiteral("Ta7feezQuranClass"));
Ta7feezQuranClass->resize(572, 485);
centralWidget = new QWidget(Ta7feezQuranClass);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
a = new QLabel(centralWidget);
a->setObjectName(QStringLiteral("a"));
a->setGeometry(QRect(-30, -20, 601, 471));
a->setPixmap(QPixmap(QString::fromUtf8(":/Ta7feezQuran/Resources/Amasjed Alharam.jpg")));
a->setScaledContents(true);
Ta7feezQuranClass->setCentralWidget(centralWidget);
menuBar = new QMenuBar(Ta7feezQuranClass);
menuBar->setObjectName(QStringLiteral("menuBar"));
menuBar->setGeometry(QRect(0, 0, 572, 21));
menuBar->setLayoutDirection(Qt::RightToLeft);
Ta7feezQuranClass->setMenuBar(menuBar);
mainToolBar = new QToolBar(Ta7feezQuranClass);
mainToolBar->setObjectName(QStringLiteral("mainToolBar"));
Ta7feezQuranClass->addToolBar(Qt::TopToolBarArea, mainToolBar);
retranslateUi(Ta7feezQuranClass);
QMetaObject::connectSlotsByName(Ta7feezQuranClass);
} // setupUi
void retranslateUi(QMainWindow *Ta7feezQuranClass)
{
Ta7feezQuranClass->setWindowTitle(QApplication::translate("Ta7feezQuranClass", "Ta7feezQuran", 0));
a->setText(QString());
} // retranslateUi
};
namespace Ui {
class Ta7feezQuranClass: public Ui_Ta7feezQuranClass {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_TA7FEEZQURAN_H
ta7feezquran.h
#ifndef TA7FEEZQURAN_H
#define TA7FEEZQURAN_H
#include <QtWidgets/QMainWindow>
#include "ui_ta7feezquran.h"
class Ta7feezQuran : public QMainWindow
{
Q_OBJECT
public:
Ta7feezQuran(QWidget *parent = 0);
~Ta7feezQuran();
private:
Ui::Ta7feezQuranClass ui;
};
#endif // TA7FEEZQURAN_H
ta7feezquran.cpp
#include "ta7feezquran.h"
Ta7feezQuran::Ta7feezQuran(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}
Ta7feezQuran::~Ta7feezQuran()
{
}
main.cpp
#include "ta7feezquran.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Ta7feezQuran w;
w.show();
return a.exec();
}
ta7feezquran.qrc
<RCC>
<qresource prefix="/Ta7feezQuran">
<file>Resources/Amasjed Alharam.jpg</file>
</qresource>
</RCC>
Some Application features used, for example images, multimedia, print support, etc., require the use of qt plugins.
The required plugins should be copied to the executable folder (folder where .exe file is included) by copying the plugin folders (with the dlls included) and pasting them inside the executable folder. Every thing else should work as expected.
In this case imageformats folder (with qjpeg.dll included as the file used is jpg) is needed to copied.

Not enough memory?

I am dealing with NI DAQmx and they define unsigned long as uInt32. I declare uInt32 array [1048*1024] in the header file and it compiles, but when I try to run it, it seems to freeze, and when I stop it, it exits with an error:
The program has unexpectedly finished.
D:\Projects\build-BlackAndWhite12bit-Desktop_Qt_5_1_1_MinGW_32bit-Debug\debug\BlackAndWhite12bit.exe exited with code -1073741571
Well, it does not say that there is not enough memory, but since I allocate 4*1048*1024 = 4,292,608 bytes, and my version is Qt Creator 2.8.1 Based on Qt 5.1.1 (MSVC 2010, 32 bit) on Win7 x64, I expect it to not have enough memory.
I found a similar question Qt Creator - calloc fails with large memory, but the only solution that works for me, would be, probably, moving to 64bit. But how do I do that? I tried to download the application from this website with the Qt 5.2.1 for Windows 64-bit (VS 2012, 556 MB) link. But when I got it, it seems to be a 32bit version configured for 64bit. Is this the one I need? Do I need OpenGL?
Is there a way to allow more memory for my current Qt version?
Any other ways to go around my problem?
Here's the .h content:
#ifndef MAIN12BITSAMPLING_H
#define MAIN12BITSAMPLING_H
#include <QMainWindow>
#include "nivision.h"
#include "nivis.h"
#include "NIDAQmx.h"
namespace Ui {
class main12bitSampling;
}
class main12bitSampling : public QMainWindow
{
Q_OBJECT
public:
explicit main12bitSampling(QWidget *parent = 0);
~main12bitSampling();
...
uInt32 ddata[1073152];
...
signals:
private slots:
private:
Ui::main12bitSampling *ui;
};
#endif // MAIN12BITSAMPLING_H
Here's the main.c:
#include "main12bitsampling.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
main12bitSampling w;
w.show();
return a.exec();
}
And here's the main12bitsampling.cpp:
main12bitSampling::main12bitSampling(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::main12bitSampling)
{
ui->setupUi(this);
mainLoop();
}
main12bitSampling::~main12bitSampling()
{
delete ui;
}
-1073741571==0xc00000fd
and c00000fd is the error code for a stack overflow.
Keeping such large arrays statically where it can end up on the stack is problematic, allocate it on the heap and you will be much better off:
QVector<uInt32> ddata;
main12bitSampling::main12bitSampling(QWidget *parent = 0)
QMainWindow(parent), ddata(1024*1048)
{
//...
}

Why is my mainwindow closing automatically when called from a differnet class?

I can't seem to figure out what went wrong so I'm here to ask you. I have made a simple class called BOBSNetworkSessionManager defined below. It is a simple class that inherits the QOBject so that I can use signals and slots but it does not have a dialog or any kind of window associated with it. It will eventually call a log in dialog and use the credentials to connect to a tcp server that I have created. This class serves as a layer to manage the connection state of the program because it will only run properly when connected to the server and when being used within 15 minutes without break due to p.c.i. compliance. If these conditions are not true this class will lock the window and force a new login. As of right now I just try to arbitrarily open the main window as though credentials had passed and i wasbconnected to the server. The problem is when I open the mainwindow it disapears right away. I cannot seem to figure out why it is diappearing. I have included all of my files.
BOBSDCNetworkSessionManager .h header file
#ifndef BOBSDCNETWORKSESSIONMANAGER_H
#define BOBSDCNETWORKSESSIONMANAGER_H
#include <QObject>
#include <QSettings>
class BOBSDCNetworkSessionManager : public QObject
{
Q_OBJECT
public:
explicit BOBSDCNetworkSessionManager(QObject *parent = 0);
protected:
void destroyed(QObject *);
signals:
public slots:
private:
void readSettings();
void writeSettings();
QSettings networkSettings;
};
#endif // BOBSDCNETWORKSESSIONMANAGER_H
BOBSDCNetworkSessionManager Implementation .cpp file
#include "bobsdcnetworksessionmanager.h"
#include "bobsmainwindow.h"
BOBSDCNetworkSessionManager::BOBSDCNetworkSessionManager(QObject *parent) :
QObject(parent)
{
BOBSMainWindow w;
w.show();
}
Main.cpp file
#include "bobsdcnetworksessionmanager.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationName("Enterprise Management Suite");
a.setApplicationVersion("Beta Version: 0.0.0.01");
a.setOrganizationName("Enigma Web Consulting");
a.setOrganizationDomain("http://www.EnigmaWebCo.com");
BOBSDCNetworkSessionManager netMgr;
return a.exec();
}
The problem is here:
{
BOBSMainWindow w;
w.show();
}
w.show() is not a blocking call. So you're creating a window, showing it, and then it immediately is destructed when it goes out of scope. You should either declare w as a member variable or construct it on the heap:
BOBSMainWindow *w = new BOBSMainWindow(this);

Qt: Trying to do a httprequest, but does not seem to work. What is wrong?

I've been using this example to create a http request for my Qt Nokia application, but I can't seem to get it working.
At first I Tried QHttp, but it is deprecated.
Here's the structure:
mainwindow.cpp
mainwindow.h
request.cpp
request.h
Here's the code:
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
Request* request = new Request();
}
MainWindow::~MainWindow()
{
delete ui;
}
Request.h
#ifndef REQUEST_H
#define REQUEST_H
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>
class Request : QObject
{
//Q_OBJECT
public:
Request();
public slots:
void finishedSlot(QNetworkReply* reply);
void checkConnection(QNetworkReply* reply);
};
#endif // REQUEST_H
And btw... what use is the "Q_OBJECT" for?
Request.cpp
#include "request.h"
#include <QDebug>
#include <QMessageBox>
Request::Request()
{
QNetworkAccessManager* oNetworkAccessManager = new QNetworkAccessManager(this);
QObject::connect(oNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),this,SLOT(finishedSlot(QNetworkReply*)));
QObject::connect(oNetworkAccessManager, SIGNAL(networkSessionConnected()),this,SLOT(checkConnection(QNetworkReply*)));
QUrl url("http://www.redrock.no");
QNetworkReply* reply = oNetworkAccessManager->get(QNetworkRequest(url));
}
void Request::checkConnection(QNetworkReply* reply)
{
QMessageBox msgBox;
msgBox.setText("checkConnection");
msgBox.setInformativeText("The network session has started");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
}
void Request::finishedSlot(QNetworkReply* reply)
{
QMessageBox msgBox;
msgBox.setText("checkConnection");
msgBox.setInformativeText("The request is done");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
}
The message boxes is just since I don't have a usb cable for my phone.
I have set breakpoints at both the "checkConnection" slot and the "finishedSlot" slot, but nothing happens there.
Anyone got an idea of what I could be doing wrong?
Here is an explanation of the Q_OBJECT macro:
http://doc.qt.nokia.com/latest/qobject.html#Q_OBJECT
Among other things,
The Q_OBJECT macro must appear in the
private section of a class definition
that declares its own signals and
slots...
So I would first try uncommenting that and see what changes. It should at least get your signals and slots talking to one another. (This is a Qt-specific feature, not part of C++, and needs the Q_OBJECT macro.) I didn't look carefully at the rest of your code, because that is the obvious first thing to try. Actually, I am surprised that it would even compile without that.
ok, finally i found what's wrong... and as usual, it's just a minor error from me.
I uncommented the Q_OBJECT, and got some vtable error or something like that. I did get this error message earlier today when i had the Q_OBJECT there, and that is why i commenting it.
But since i'm new to Qt i hade forgot to incelude the QObject in the request.h
"#include "
And that fixed everything for me :)
Thanks for the explenation and elaboration Dave.

Resources