sending a dbus message from shell to qt app - c++

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

Related

QTserialport connection

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;
}

Qt serial communication

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).

Qt - How to find children right

I'm trying to find a widget that I added to the screen and do some manipulations with it, but the list of children is empty every time. What am I doing wrong?
Here is the code (to run it, create a new clean project and replace the text in MainWindow.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QLabel>
#include <QLayout>
#include <QList>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel* wid = new QLabel("hello");
ui->centralWidget->layout()->addWidget(wid);
QList<QLabel*> list = ui->centralWidget->findChildren<QLabel*>();
qDebug() << list.isEmpty();
}
MainWindow::~MainWindow()
{
delete ui;
}

QtWebEngine display black block when I called QWidget::winId()

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QWebEngineView>
#include <qDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
#if 1
auto btn = new QPushButton;
ui->gridLayout->addWidget(btn);
qDebug()<<btn->winId();
#endif
auto web = new QWebEngineView;
ui->gridLayout->addWidget(web);
web->load(QUrl("http://www.google.com"));
}
MainWindow::~MainWindow()
{
delete ui;
}
That's the whole code.
Windows 10 , Qt 5.5 .
When I turn on the switch, winId() would be called, then the QtWebEngine can not work rightly.
What should I do ?
Don't call winId() until the widget is visible. You could set an eventFilter that is activated on QEvent::Show, for further information see http://doc.qt.io/qt-5/qobject.html#installEventFilter

Controlling Multiple UI files in Qt framework

Question asked twice: see Handling multiple ui files in Qt
I am new to Qt framwork , i have been given this simple task:
In the MainWindow , i have a submit button , once its clicked another total different window should appear
i thought of doing this by doing one extra UI file called From.ui file and to switch from MainWindow to Form once submit is clicked , this is my code:
//main.cpp
#include "mainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.setOrientation(MainWindow::ScreenOrientationAuto);
mainWindow.showExpanded();
return app.exec();
}
//MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include <QtCore/QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: SubmitClicked()
{
Form* f= new Form(this);
f->show();
f->raise();
f->activateWindow();
}
//Form.cpp
#include "form.h"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}
Form::~Form()
{
delete ui;
}
this code compiled perfectly , but its not doing as expected , once submit is clicked , nothing is done...
can you please tell me whats wrong ?
It seems that SubmitClicked slot is not connected to clicked event of your button
Put a cout/printf at the top of your SubmitClicked method to make sure that it is called.