I'm running a simple program in Qt creator to connecting to a camera. When I run the program, the following issue is reported:
Graph failed to connect filters -2147467259
[0x0] Failed to connect graph (The operation completed successfully.)
Can somebody tell me what the problem is?! Any solution would be appreciated.
Thanks
I followed the link you mentioned. But i didn't understand the exact way to solve the problem.
Here is the code i used to connect to a camera:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connected = false;
camera = new QCamera();
qDebug() << "Number of cameras found:" << QCameraInfo::availableCameras().count();
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
foreach (const QCameraInfo &cameraInfo, cameras)
{
qDebug() << "Camera info:" << cameraInfo.deviceName() << cameraInfo.description() << cameraInfo.position();
ui->comboBox->addItem(cameraInfo.description());
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
if(!connected)
connectCamera();
else {
camera->stop();
viewfinder->deleteLater();
ui->pushButton->setText("Connect");
connected = false;
}
}
void MainWindow::connectCamera()
{
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
foreach (const QCameraInfo &cameraInfo, cameras)
{
qDebug() << cameraInfo.description() << ui->comboBox->currentText();
if(cameraInfo.description()==ui->comboBox->currentText()) {
camera = new QCamera(cameraInfo, this);
viewfinder = new QCameraViewfinder(this);
camera->setViewfinder(viewfinder);
ui->horizontalLayout_2->addWidget(viewfinder);
connected = true;
ui->pushButton->setText("Disconnect");
camera->start();
return;
}
}
}
Related
I am currently working with QT creator and PostgreSQL.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QDebug>
#include <QtSql>
static QSqlDatabase DB = QSqlDatabase::addDatabase("QPSQL");
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
DB.setHostName("db.fe.up.pt");
DB.setDatabaseName("****");
DB.setUserName("*****");
DB.setPassword("****");
if (DB.open())
{
qDebug() << "Connected!";
QSqlQuery query2;
if(query2.exec("SET search_path TO INFI")){
qDebug() << "Schema OK";
}
}
else
{
qDebug() << "Failed to connect.";
//ui->LogOk->setText("You not are connected to VPN!! ");
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_orders_clicked()
{
QSqlQuery query;
QSqlQueryModel *model = new QSqlQueryModel;
if(query.exec("SELECT * from INFI.orders")){
qDebug()<< "TOP";
}
else{
qDebug()<< "Failed";
}
model->setQuery(std::move(query));
}
I am trying to run this piece of code, and when I execute my app my connection with the DB are ok. But I dont know why I cant execute this query when I press the button, my qDebug always return me "failed", what I am doing wrong?? This is the tables and the schema I am working. I want to select all the items of my orders table, this table is inside INFI schema.
I am learning QT, and following a video tutorial for practice. the code is given below, in if statement i have written if arduino is not found, print, arduino is not found. But it always gives this error, there is some problem in my program but I am unable to diagnose it, help me.
this is the program
#include "dialog.h"
#include "ui_dialog.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
#include <QtWidgets>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
arduino_is_available = false;
arduino_port_name = "";
arduino = new QSerialPort;
/*qDebug() << "Number of available ports: " <<QSerialPortInfo::availablePorts().length();
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
qDebug() <<"Has vendorID: " << serialPortInfo.vendorIdentifier();
if(serialPortInfo.hasVendorIdentifier()){
qDebug() <<"Vendor ID:" <<serialPortInfo.vendorIdentifier();
}
qDebug() <<"Has product ID:" <<serialPortInfo.hasProductIdentifier();
if(serialPortInfo.hasProductIdentifier()){
qDebug() <<"Product ID:" <<serialPortInfo.productIdentifier();
}
}
*/
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
if(serialPortInfo.hasVendorIdentifier() == arduino_uno_vendor_id){
if(serialPortInfo.hasProductIdentifier() == arduino_uno_product_id){
arduino_port_name = serialPortInfo.portName();
arduino_is_available = true;
}
}
}
if(arduino_is_available){
//open and configure the serialport
arduino->setPortName(arduino_port_name);
arduino->open(QSerialPort::WriteOnly);
arduino->setBaudRate(QSerialPort::Baud9600);
arduino->setDataBits(QSerialPort::Data8);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
arduino->setFlowControl(QSerialPort::NoFlowControl);
}else{
//give error message if not available
QMessageBox::warning(this, "port error", "couldn't find the arduino!");
}
}
}
Dialog::~Dialog()
{
if(arduino->isOpen()){
arduino->close();
}
delete ui;
}
void Dialog::on_redslider_valueChanged(int value)
{
ui->redvalue->setText(QString("<span style=\" font-size:18pt; font-weight:600;\">%1</span>").arg(value));
Dialog::updateRGB(QString("r%1").arg(value));
qDebug() <<value;
}
void Dialog::on_greenslider_valueChanged(int value)
{
ui->greenvalue->setText(QString("<span style=\" font-size:18pt; font-weight:600;\">%1</span>").arg(value));
Dialog::updateRGB(QString("g%1").arg(value));
qDebug() <<value;
}
void Dialog::on_blueslider_valueChanged(int value)
{
ui->bluevalue->setText(QString("<span style=\" font-size:18pt; font-weight:600;\">%1</span>").arg(value));
Dialog::updateRGB(QString("b%1").arg(value));
qDebug() <<value;
}
void Dialog::updateRGB(QString command)
{
if(arduino->isWritable()){
arduino->write(command.toStdString().c_str());
} else{
qDebug() << "Couldn't write to serial";
}
}
You are comparing a boolean to an integer (or some other type)) in the two tests for vendor id and product id.
serialPortInfo.hasVendorIdentifier() returns bool and you do an equality test to some unknown value arduino_uno_vendor_id that certainly cannot be bool. Probably string.
Same for Product ID.
'Twill always be FALSE
I am making a simple program that will 'connect' to itself and then send data. It starts a QTcpServer then waits for any incoming connections. I have a separate function that will in turn attempt to connect to this server at the localhost and port I decided on. This works when I open Telnet in the command prompt, but now in my actual program. Here is the code that I used (Some are snippets from other sources)
MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
server = new QTcpServer(this);
//Initialize and start the server
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
if (!server->listen(QHostAddress::Any, 3665))
{
qDebug() << "Server failed to start!";
}
else
{
qDebug() << "Server started";
}
//Try to connect to the server
connectToServer("127.0.0.1", qint16(3665));
}
MainWindow::~MainWindow()
{
delete server;
delete ui;
}
void MainWindow::connectToServer(QString host, qint16 port)
{
qDebug() << "Connecting to " + host + " at port " + QString::number(port);
QTcpSocket socket;
socket.connectToHost(host, port);
if (!socket.waitForConnected(5000))
{
qDebug() << socket.errorString();
}
while (socket.bytesAvailable() < (int)sizeof(quint16))
{
if (!socket.waitForReadyRead(5000))
{
qDebug() << socket.errorString();
}
}
quint16 blockSize;
QDataStream in(&socket);
in.setVersion(QDataStream::Qt_5_5);
in >> blockSize;
while (socket.bytesAvailable() < blockSize)
{
if (!socket.waitForReadyRead(5000))
{
qDebug() << socket.errorString();
}
}
QString fortune;
in >> fortune;
qDebug() << fortune;
}
void MainWindow::newConnection()
{
qDebug() << "A connection has been found.";
QTcpSocket *socket = server->nextPendingConnection();
socket->write("hello client\r\n");
socket->flush();
socket->waitForBytesWritten(5000);
socket->close();
}
The source of your problem is, most likely, the pseudo-synchronous mess caused by waitFor methods. Get rid of them. Furthermore, you're not guaranteed anything about how many bytes you receive upon readyRead: it's perfectly normal to receive one byte at a time, in some circumstances, or really any number of bytes, including more bytes than you might expect. Your code must cope with that.
This is one example of such approach - it does what you want, asynchronously.
That is another example that shows how to leverage state machines to write asynchronous communications code using an easy to read, declarative syntax.
I have a server that can handle multiple threads. The server starts and listens, but it is having trouble echoing when an incoming connection is pending.
I am using telnet to open the socket and send data to the server. However, the server only displays that it's listening, but doesn't echo any of the data I type through telnet or signify that there is an incoming connection. I shut off Windows firewall for private networks and still...nothing.
Also tried seeing if the server error string had anything useful to say, but all it is just an empty string.
This is a complete mystery to me and if anyone had anything constructive to note, it'd be much appreciated. Code for the thread and server is below.
server.cpp
#include "myserver.h"
MyServer::MyServer(QObject *parent) :
QTcpServer(parent)
{
}
void MyServer::StartServer()
{
if(!this->listen(QHostAddress::Any,1234))
{
qDebug() << "Could not start server";
}
else
{
qDebug() << "Listening...";
}
}
void MyServer::incomingConnection(int socketDescriptor)
{
qDebug() << socketDescriptor << " Connecting...";
MyThread *thread = new MyThread(socketDescriptor,this);
connect(thread, SIGNAL(finished()),thread, SLOT(deleteLater()));
thread->start();
}
thread.cpp
#include "mythread.h"
MyThread::MyThread(int ID, QObject *parent) :
QThread(parent)
{ this->socketDescriptor = ID;
}
void MyThread::run()
{
qDebug() << socket->errorString();
//thread starts here
qDebug() << socketDescriptor << " Starting thread";
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
emit error(socket->error());
return;
}
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::DirectConnection);
connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
qDebug() << socketDescriptor << " Client Connected";
exec();
}
void MyThread::readyRead()
{
QByteArray Data = socket->readAll();
qDebug() << socketDescriptor << " Data in: " << Data;
socket->write(Data);
}
void MyThread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
Which version of Qt are you using? In Qt 5, the parameter for the function incomingConnection is of type qintptr and not int. Have a look at the following links:
incomingConnection - Qt 5
Qt 5 - Multithreaded server tutorial
I'm trying to establish bluetooth connection between laptop and Samsung Smart Touch Remote. My issue is that I can't connect socket to device(socket is always in "Host lookup state"):
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Create a discovery agent and connect to its signals
QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
this, SLOT(deviceDiscovered(const QBluetoothDeviceInfo&)));
// Start a discovery
discoveryAgent->start();
}
void MainWindow::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this);
socket->connectToService(QBluetoothAddress(device.address()),
QBluetoothUuid(QBluetoothUuid::SerialPort));
connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)),
this, SLOT(socketError(QBluetoothSocket::SocketError)));
connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(socketRead()));
connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socketStateChanged()));
}
void MainWindow::socketRead()
{
QByteArray receivedData = socket->readAll();
QMessageBox msg;
msg.setText(QString(receivedData));
msg.exec();
}
void MainWindow::socketConnected()
{
qDebug() << "Socket connected";
qDebug() << "Local: "
<< socket->localName()
<< socket->localAddress().toString()
<< socket->localPort();
qDebug() << "Peer: "
<< socket->peerName()
<< socket->peerAddress().toString()
<< socket->peerPort();
}
void MainWindow::socketDisconnected()
{
qDebug() << "Socket disconnected";
socket->deleteLater();
}
void MainWindow::socketError(QBluetoothSocket::SocketError error)
{
qDebug() << "Socket error: " << error;
}
void MainWindow::socketStateChanged()
{
int socketState = socket->state();
QMessageBox msg;
if(socketState == QAbstractSocket::UnconnectedState)
{
msg.setText("unconnected");
}
else if(socketState == QAbstractSocket::HostLookupState)
{
msg.setText("host lookup");
}
else if(socketState == QAbstractSocket::ConnectingState )
{
msg.setText("connecting");
}
else if(socketState == QAbstractSocket::ConnectedState)
{
msg.setText("connected");
}
else if(socketState == QAbstractSocket::BoundState)
{
msg.setText("bound");
}
else if(socketState == QAbstractSocket::ClosingState)
{
msg.setText("closing");
}
else if(socketState == QAbstractSocket::ListeningState)
{
msg.setText("listening");
}
msg.exec();
}
Can someone please tell me what I'm doing wrong?
Have you tried to open a COM port on your Laptop which is connected to the Laptop's bluetooth module?
You can go to Bluetooth settings -> Com Ports -> Add a new com port. Then open this Port using hypertermical or Teraterm. Now try to connect from your Android phone...
Hope this works!
I had the samme issue. The fix was to use a port 1 in argument to socket->connectToService(addr, 1).
This tells SDP to use connection as channel via SDP.