Im trying to use QTserialport to connect to a port via uart
I get QIODevice::read (QSerialPort): device not open this but device is connect as i tested with other applications and with Qserialportinfo I was able to detect the serial port but not connect to it and read from it which part am i doing wrong ??
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m.setBaudRate(QSerialPort::Baud115200);
m.setDataBits(QSerialPort::Data8);
m.setPortName("COM2");
m.setFlowControl(QSerialPort::NoFlowControl);
m.setParity(QSerialPort::NoParity);
m.setStopBits(QSerialPort::OneStop);
m.setReadBufferSize(1000);
qDebug()<<m.portName();
while(1){
qDebug()<<m.readAll();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QSerialPort>
#include <QMainWindow>
#include <QSerialPortInfo>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QSerialPort m;
QSerialPortInfo info;
};
#endif // MAINWINDOW_H
ps. I tried to use //./ & \.\ & ////.// & \\.\ before the port but it didnt worked
I believe you have to open the port before reading from it. Try something like this:
if (m.open(QIODevice::ReadOnly)){
while (1){
if (m.waitForReadyRead(1000)){
qDebug() << m.readAll();
}
}
}
after adding m.open() worked perfectly
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m.setBaudRate(QSerialPort::Baud115200);
m.setDataBits(QSerialPort::Data8);
m.setPortName("COM2");
m.setFlowControl(QSerialPort::NoFlowControl);
m.setParity(QSerialPort::NoParity);
m.setStopBits(QSerialPort::OneStop);
m.setReadBufferSize(1000);
m.open(QIODevice::ReadOnly); //after adding this worked perfectly
qDebug()<<m.portName();
while(1){
qDebug()<<m.readAll();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
I just tried the following code. But the slot function is not working. The connection is OK and I found out it by qDubug. The console output is as follows.
[ZDSGuard] 32 DllMain hook strProductName2 : C:\qt_example\build-
serial_test-Desktop_Qt_5_13_1_MinGW_32_bit-Debug\debug\serial_test.exe-1
ddd
ss
ccc
As you can find, aaa is not printed out. If the slot function works fine, it should be printed.
Please let me know if somebody finds out what is wrong.
Thanks in advance.
[ZDSGuard] 32 DllMain hook strProductName2 : C:\qt_example\build-
serial_test-Desktop_Qt_5_13_1_MinGW_32_bit-Debug\debug\serial_test.exe-1
ddd
ss
ccc
Serial Communication Code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QLabel>
QSerialPort *serial;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort();
serial->setPortName("COM4");
serial->setBaudRate(QSerialPort::Baud115200);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->setParity(QSerialPort::NoParity);
serial->setDataBits(QSerialPort::Data8);
serial->setStopBits(QSerialPort::OneStop);
if (serial->open(QIODevice::ReadWrite))
ui->label->setText("bb");
if (QObject::connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived())))
qDebug()<< "ddd";
//ui->label->setText("aa");
qDebug() << "ss";
}
MainWindow::~MainWindow()
{
delete ui;
serial->close();
}
void MainWindow::serialReceived()
{
QByteArray BA;
BA=serial->readAll();
ui->label->setText("aa");
//printf(BA);
qDebug()<<"aaa";//BA;
}
QSP has bug in Qt 5.13.1. Use or Qt 5.13.0, or wait for a newest versions (5.13.2 / 5.12.6).
Drop event will not happen, although `setAcceptDrops' has been called. The following code is based on a widget project created with Qt 5.12.0. After adding in dropEvent() function the cpp file becomes
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug> // added
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true); // added
}
MainWindow::~MainWindow()
{
delete ui;
}
// added; in .h it is in `protected:' section
void MainWindow::dropEvent(QDropEvent *event)
{
qDebug() << "dropEvent";
}
What am I missing? I have been struggling for a few days... Thanks in advance.
You have to overwrite the dragEnterEvent method that allow you to filter by the data type, by the source, by the type of action. In the following example, everything is accepted:
*.h
// ...
protected:
void dropEvent(QDropEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
// ...
*.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true); // added
}
// ...
void MainWindow::dropEvent(QDropEvent *event)
{
qDebug() << "dropEvent" << event;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
For more detail I recommend you read Drag and Drop.
I want to make application on Qt, which can play more then one video simultaneously. I want to make application for security camera, so I have to show all camera's stream simultaneously. I made media application with help of QMediaPlayer and QVideoWidget, which support all type of video.
I also get help from bellow site, but I don't want to use Vlc Lib.
Playing multiple video using libvlc and Qt
Please guide me path to achieve my destination.
Should I have to use Phonon?
I try some code to display two video simultaneously, but get some problem.
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.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
player[i][j] = new QMediaPlayer(this);
vw[i][j] = new QVideoWidget(this);
player[i][j]->setVideoOutput(vw[i][j]);
ui->graphicsView->setViewport(vw[i][j]);
//ui->graphicsView_2->setViewport(vw[i][j]);
slider = new QSlider(this);
bar = new QProgressBar(this);
slider1 = new QSlider(this);
slider->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(slider);
ui->statusBar->addPermanentWidget(bar);
ui->statusBar->addPermanentWidget(slider1);
connect(player[i][j],&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
connect(player[i][j],&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
connect(slider,&QSlider::sliderMoved,player[i][j],&QMediaPlayer::setPosition);
slider1->setValue(50);
connect(slider1,&QSlider::sliderMoved,player[i][j],&QMediaPlayer::setVolume);
connect(player[i][j],&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
connect(player[i][j],&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString filename = QFileDialog::getOpenFileName(this,"Open a File","","Video File (*.*)");
on_actionStop_triggered();
player[1][1]->setMedia(QUrl::fromLocalFile(filename));
on_actionPlay_triggered();
}
void MainWindow::on_actionPlay_triggered()
{
player[1][1]->play();
ui->statusBar->showMessage("Playing");
}
void MainWindow::on_actionPause_triggered()
{
player[1][1]->pause();
ui->statusBar->showMessage("Paused...");
}
void MainWindow::on_actionStop_triggered()
{
player[1][1]->stop();
ui->statusBar->showMessage("Stopped");
}
void MainWindow::on_actionMute_triggered()
{
player[1][1]->setMuted(1);
ui->statusBar->showMessage("Muted...");
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QFileDialog>
#include <QProgressBar>
#include <QSlider>
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void on_actionPlay_triggered();
void on_actionPause_triggered();
void on_actionStop_triggered();
void on_actionMute_triggered();
private:
Ui::MainWindow *ui;
QMediaPlayer* player[2][2];
QVideoWidget* vw[2][2];
QProgressBar* bar;
QSlider* slider;
QSlider* slider1;
};
#endif // MAINWINDOW_H
mainwindow.ui
Application Image:
Now I want to play same video on Graphics widget. Is it possible? If yes, then How?
Can anyone guide me, how can I add thread on ui->GraphicsView and ui->GraphicsView_2 in mainwindo.cpp file?
Thanks
Tejas Virpariya
I done, I can play two video simultaneously.
I change my mainwindow.cpp file only.Its not a perfect code, but its a solution.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer;
vw = new QVideoWidget;
player->setVideoOutput(vw);
ui->graphicsView->setViewport(vw);
//ui->graphicsView_2->setViewport(vw1);
slider = new QSlider(this);
bar = new QProgressBar(this);
slider1 = new QSlider(this);
slider->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(slider);
ui->statusBar->addPermanentWidget(bar);
ui->statusBar->addPermanentWidget(slider1);
connect(player,&QMediaPlayer::durationChanged,slider,&QSlider::setMaximum);
connect(player,&QMediaPlayer::positionChanged,slider,&QSlider::setValue);
connect(slider,&QSlider::sliderMoved,player,&QMediaPlayer::setPosition);
slider1->setValue(50);
connect(slider1,&QSlider::sliderMoved,player,&QMediaPlayer::setVolume);
connect(player,&QMediaPlayer::durationChanged,bar,&QProgressBar::setMaximum);
connect(player,&QMediaPlayer::positionChanged,bar,&QProgressBar::setValue);
player1 = new QMediaPlayer;
vw1 = new QVideoWidget;
player1->setVideoOutput(vw1);
//ui->graphicsView->setViewport(vw);
ui->graphicsView_2->setViewport(vw1);
slider2 = new QSlider(this);
bar1 = new QProgressBar(this);
slider3 = new QSlider(this);
slider2->setOrientation(Qt::Horizontal);
ui->statusBar->addPermanentWidget(slider2);
ui->statusBar->addPermanentWidget(bar1);
ui->statusBar->addPermanentWidget(slider3);
connect(player1,&QMediaPlayer::durationChanged,slider2,&QSlider::setMaximum);
connect(player1,&QMediaPlayer::positionChanged,slider2,&QSlider::setValue);
connect(slider2,&QSlider::sliderMoved,player1,&QMediaPlayer::setPosition);
slider3->setValue(50);
connect(slider3,&QSlider::sliderMoved,player1,&QMediaPlayer::setVolume);
connect(player1,&QMediaPlayer::durationChanged,bar1,&QProgressBar::setMaximum);
connect(player1,&QMediaPlayer::positionChanged,bar1,&QProgressBar::setValue);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionOpen_triggered()
{
QString filename = QFileDialog::getOpenFileName(this,"Open a File","","Video File (*.*)");
QString filename1 = "C:/Users/Public/Videos/Sample Videos/Wildlife.wmv";
on_actionStop_triggered();
player->setMedia(QUrl::fromLocalFile(filename));
player1->setMedia(QUrl::fromLocalFile(filename1));
on_actionPlay_triggered();
}
void MainWindow::on_actionPlay_triggered()
{
player->play();
player1->play();
ui->statusBar->showMessage("Playing");
}
void MainWindow::on_actionPause_triggered()
{
player->pause();
player1->pause();
ui->statusBar->showMessage("Paused...");
}
void MainWindow::on_actionStop_triggered()
{
player->stop();
player1->stop();
ui->statusBar->showMessage("Stopped");
}
void MainWindow::on_actionMute_triggered()
{
if(player->isMuted()){
player->setMuted(0);
}else{
player->setMuted(1);
}
if(player1->isMuted()){
player1->setMuted(0);
}else{
player1->setMuted(1);
}
ui->statusBar->showMessage("Muted...");
}
I want to do dbus-send from shell/console to a qt application.
This is the code for a simple QT app
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtDBus>
#include <QDBusConnection>
#include <QDebug>
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
bool ret = QDBusConnection::sessionBus().connect(
"MyHome", //service
"/", //path
"com.mydomain.mcp", //interface
"usb", //name
this, //receiver
SLOT(messageSlot(QString)));
}
void MainWindow::messageSlot(const QString &t1)
{
qDebug("%s", QString("%1").arg(t1).toUtf8().data());
}
From the terminal, I and sending this command
dbus-send --session --print-reply --reply-timeout=2000 --type=method_call / com.mydomain.mcp.usb string:'a'
I get this error: Method "usb" with signature "s" on interface "com.mydomain.mcp" doesn't exist
What am I doing wrong?
Thanks