Qt: QObject::connect: No such slot QObject::processPendingDatagrams() [duplicate] - qt

This question already has answers here:
When should Q_OBJECT be used?
(4 answers)
QT "No such slot" Error [duplicate]
(1 answer)
Closed 6 years ago.
I have written an UDP programme with Qt, and when I connect this:
connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
the complier tells me that
no such slot
the error click here
and I want to know how to fix it, thank you!
P.S.
Here are my files:
files
Here are my codes:
enter code here
udptest.cpp:
#include "udptest.h"
#include <QObject>
#include <QUdpSocket>
#include <QtNetwork>
UDPtest::UDPtest()
{
socket = new QUdpSocket();
port = 2016;
socket->bind(port,QUdpSocket::ShareAddress
| QUdpSocket::ReuseAddressHint);
connect(socket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
}
QString UDPtest::getIP()
{
QList<QHostAddress> list = QNetworkInterface::allAddresses();
foreach (QHostAddress address, list)
{
if(address.protocol() == QAbstractSocket::IPv4Protocol)
return address.toString();
}
return 0;
}
void UDPtest::sendMessage(QString message)
{
QByteArray data;
QDataStream out(&data,QIODevice::WriteOnly);
QString localHostName = QHostInfo::localHostName();
QString address = getIP();
out <<"123"<< localHostName << address << message;
socket->writeDatagram(data,data.length(),QHostAddress::Broadcast, port);
}
void UDPtest::processPendingDatagrams()
{qDebug()<<"receive";
while(socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(socket->pendingDatagramSize());
socket->readDatagram(datagram.data(),datagram.size());
QDataStream in(&datagram,QIODevice::ReadOnly);
QString userName,localHostName,ipAddress,message;
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
in >>userName >>localHostName >>ipAddress >>message;
QString msg=time+userName+localHostName+ipAddress+message;
msger=msg;
qDebug()<<msg;
}
}
QString UDPtest:: messager()
{
return msger;
}
main.cpp:
#include"udptest.h"
#include<QDebug>
#include <QtCore/QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"123";
UDPtest test;
test.sendMessage("aha");
return a.exec();
}
udptest.h:
#ifndef UDPTEST_H
#define UDPTEST_H
#include <QObject>
#include <QUdpSocket>
#include <QtCore/QCoreApplication>
#include <QtNetwork>
class UDPtest:public QObject
{
public:
UDPtest();
QString messager();
void sendMessage(QString);
private slots:
void processPendingDatagrams();
private:
QString msger;
QUdpSocket *socket;
qint16 port;
QString getIP();
};
#endif // UDPTEST_H
QudptestConsole.pro:
QT += core
QT -= gui
QT += network
CONFIG += c++11
TARGET = QudptestConsole
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
udptest.cpp
HEADERS += \
udptest.h

You have forgotten Q_OBJECT macro in UDPtest class
class UDPtest: public QObject
{
Q_OBJECT
public:
UDPtest();
.....
}

Related

dbus-send to QDBus program example

I try to send a message via dbus-send to this small example program.
But it is not received:
dbus-send --session --type=method_call / dbustester.test.slot_foo
The return code is 0 and not message is printed to the console.
Below is the source code.
main.cpp
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtDBus/QtDBus>
#include <Example.h>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
Example *e = new Example();
e->setupDBus();
return app.exec();
}
Example.h
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtDBus/QtDBus>
class Example : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "dbustester.test")
public:
Example(QObject* parent = NULL) :
QObject(parent)
{
}
void setupDBus()
{
QDBusConnection session = QDBusConnection::sessionBus();
if (!session.isConnected())
{
qFatal("Cannot connect to the D-Bus session bus.");
return;
}
session.connect("", "/", "dbustester.test", "slot_foo", this, SLOT(slot_foo(void)));
if(!session.registerObject("/", this, QDBusConnection::ExportScriptableContents)) {
qFatal("Cannot registerObject.");
return;
}
if(!session.registerService("dbustester.test")) {
qFatal("Cannot registerObject.");
return;
}
}
public slots:
Q_SCRIPTABLE void slot_foo()
{
qDebug() << "request received";
}
};
Build:
qmake -project
echo "CONFIG += qdbus" >> *.pro
qmake
I've found the answer while writing the question, but I wrote the question anyway. Some people might find it useful.
dbus-send --session --dest=dbustester.test --type=method_call / dbustester.test.slot_foo
I forgot the --dest argument. :>

Q_PROPERTY not working properly in linux distro

i’m developing a clock application and I need to do it using Q_PROPERTY. The idea is to make all the clock control logic using C++ and deploy it in a QML GUI.
I got this working on a windows machine but when I run it on a linux distro in a development board I get sometimes undefined text (which are the exposed properties from C++).
The header file is:
#ifndef QCPPCLOCK_H
#define QCPPCLOCK_H
#include <QObject>
#include <QDebug>
#include <QString>
#include <QTime>
#ifdef __linux__
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#endif
class QcppClock : public QObject
{
Q_OBJECT
Q_PROPERTY(QString hours READ hours WRITE setHours NOTIFY hoursChanged)
Q_PROPERTY(QString minutes READ minutes WRITE setMinutes NOTIFY minutesChanged)
private:
QString actualHours;
QString actualMinutes;
QObject* rootObject;
QTime actualTime;
qint8 alarmHour;
qint8 alarmMinutes;
QString timeFormat;
bool alarmSet;
#ifdef __linux__
struct tm* ptm;
struct timeval tv;
#endif
public:
explicit QcppClock(QObject *parent = 0);
QString hours();
void setHours(QString);
QString minutes();
void setMinutes(QString);
public slots:
void qcppClock_vUpdateTextTime_slot(void);
signals:
void qcppClock_vTriggerAlarm_signal(void);
void hoursChanged(void);
void minutesChanged(void);
};
#endif // QCPPCLOCK_H
The .cpp file:
#include "qcppclock.h"
#include <QTimer>
#include <QtCore/qmath.h>
QcppClock::QcppClock(QObject *parent) :
QObject(parent)
{
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(qcppClock_vUpdateTextTime_slot()));
rootObject = parent;
actualTime = QTime::currentTime();
/* Initialize the format to 24 hours */
timeFormat = "24hours";
timer->start(1000);
#ifdef __linux__
gettimeofday (&tv, NULL);
ptm = localtime (&tv.tv_sec);
int hour = ptm->tm_hour;
//setHours(hour);
//actualHours = QString::number(hour);
int minutes = ptm->tm_min;
//setMinutes(minutes);
//actualMinutes = QString::number(minutes);
int seconds = ptm->tm_sec;
(void)actualTime.setHMS(hour , minutes, seconds);
#endif
}
QString QcppClock::hours()
{
return actualHours;
}
void QcppClock::setHours(QString newHours)
{
actualHours = newHours;
#ifdef __linux__
tv.tv_sec += 3600 * actualHours.toInt();
settimeofday(&tv, NULL);
#else
(void)actualTime.setHMS(actualHours.toInt() , actualMinutes.toInt(), actualTime.second());
#endif
emit hoursChanged();
}
QString QcppClock::minutes()
{
return actualMinutes;
}
void QcppClock::setMinutes(QString newMinutes)
{
actualMinutes = newMinutes;
#ifdef __linux__
tv.tv_sec += 60 * actualMinutes.toInt();
settimeofday(&tv, NULL);
#else
(void)actualTime.setHMS(actualHours.toInt() , actualMinutes.toInt(), actualTime.second());
#endif
emit minutesChanged();
}
void QcppClock::qcppClock_vUpdateTextTime_slot(void)
{
actualTime = actualTime.addSecs(1);
actualMinutes = QString::number(actualTime.minute());
emit minutesChanged();
actualHours = QString::number(actualTime.hour());
emit hoursChanged();
QString::number(actualTime.minute());
}
The main file which instantiates the clockctrl object is:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "qcpp_Clock/qcppclock.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QcppClock qcpp_Clock;
QQmlContext* context = engine.rootContext();
context->setContextProperty("clockCtrl", &qcpp_Clock);
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
And the QML file using the exposed properties is:
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
visible: true
width: 800
height: 480
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
property string prHours: clockCtrl.hours
property string prMinutes: clockCtrl.minutes
text: {
console.log(clockCtrl, clockCtrl.hours, clockCtrl.minutes);
return (prHours + " : " + prMinutes)
}
anchors.centerIn: parent
}
}
As you can see it’s a short and easy code, the problem is in QML when I try to make reference to the C++ exposed properties, i added a couple of consoles in order to see the result in the log and It works like a charm in windows, I never get these “undefined text” but in the development board with linux distro I get sometimes:
qml: QcppClock(0×7e8eeca8) 1 49
qml: QcppClock(0×7e8eeca8) undefined undefined
qml: QcppClock(0×7e8eeca8) 1 49
The first debug is the address of the c++ exposed object in C++, then, the Q_PROPERTY “hour” and the Q_PROPERTY “minutes”.
Thank you in advance.
Ramsés

Windows Task Manager shows process memory keeps growing

I observed that through task mgr, the memory increases in steps of 4kB and 8kB, though not necessarily in this order.
Possible duplicate: Windows Task Manager shows process memory keeps growing even though there are no memory leaks
I am not sure whether this's occurring because I did not release the QTimer object, timer2. Please advise me how to stop this memory increase, and whether my guess of why it's occurring, is correct.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <QDebug>
#include <QDateTime>
#include <QFileInfo>
#include <QString>
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#define TIMER2_VALUE 3000
#define UPDATED_IMAGE_STORAGE_PATH "E:\\QT1\\timeStampDateMod2\\TimeStampDateMod2\\updatedRefImg.JPG"
#define UPDATED_IMAGE_BACKUP_PATH "E:\\QT1\\timeStampDateMod2\\TimeStampDateMod2\\backUp\\updatedRefImg[%1].JPG"
using namespace std;
using namespace cv;
typedef struct
{
QDateTime dateTimeMod1;
QDateTime dateTimeMod2;
}tTimeMods;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QTimer *timer2;
tTimeMods findTimeModifiedStruct();
QDateTime findTimeModified();
void compareTimeMods(tTimeMods timeTypeFunction, QDateTime dateTimeMod2);
QString appendWithImageName(tTimeMods timeTypeFunction);
void shiftToRepository(QString pathString);
void updatedImgToRepository(QString pathString);
public slots:
void timerSlot2();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
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"
tTimeMods timeTypeFunction, timeTypeMain;
QDateTime dateTimeMod2;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timeTypeMain = findTimeModifiedStruct();
timer2 = new QTimer(this);
connect(timer2, SIGNAL(timeout()), this, SLOT(timerSlot2()));
timer2->start(TIMER2_VALUE);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerSlot2()
{
dateTimeMod2 = findTimeModified();
compareTimeMods(timeTypeMain, dateTimeMod2);
//delete timer2;
}
//tTimeMods findTimeModifiedStruct()
tTimeMods MainWindow::findTimeModifiedStruct()
{
QString myFileName = UPDATED_IMAGE_STORAGE_PATH;
QFileInfo info(myFileName);
/*find last date modified*/
timeTypeFunction.dateTimeMod1 = info.lastModified();
timeTypeFunction.dateTimeMod2 = info.lastModified();
qDebug()<< "dateTimeMod1: " << timeTypeFunction.dateTimeMod1.toString() << endl << "dateTimeMod2: "<< timeTypeFunction.dateTimeMod2.toString();
return(timeTypeFunction);
}
QDateTime MainWindow::findTimeModified()
{
QString myFileName = UPDATED_IMAGE_STORAGE_PATH;
QFileInfo info(myFileName);
QDateTime dateTimeMod2 = info.lastModified();
qDebug()<< "dateTimeMod2: "<< dateTimeMod2.toString();
return(dateTimeMod2);
}
void MainWindow::compareTimeMods(tTimeMods timeTypeFunction, QDateTime dateTimeMod2)
{
if(dateTimeMod2 >= timeTypeFunction.dateTimeMod1)
{
timeTypeFunction.dateTimeMod1 = dateTimeMod2;
QString pathString = appendWithImageName(timeTypeFunction);
shiftToRepository(pathString);
}
}
QString MainWindow::appendWithImageName(tTimeMods timeTypeFunction)
{
/*appending just the timeMod with the path & image name*/
QString path = QString(UPDATED_IMAGE_BACKUP_PATH).arg(timeTypeFunction.dateTimeMod1.toString());
qDebug()<< "path: " << path;
return path;
}
void MainWindow::shiftToRepository(QString pathString)
{
updatedImgToRepository(pathString);
}
void MainWindow::updatedImgToRepository(QString pathString)
{
pathString.replace(":","-");
pathString.replace(1,1,":");
qDebug()<<"pathString now: "<<pathString;
/*convert QString into char* */
QByteArray pathByteArray = pathString.toLocal8Bit();
const char *path = pathByteArray.data();
IplImage *InputImg = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
InputImg = cvLoadImage(UPDATED_IMAGE_STORAGE_PATH ,CV_LOAD_IMAGE_UNCHANGED);
/*save the image*/
cvSaveImage(path,InputImg);
cvReleaseImage(&InputImg);
}
I'm not familiar with OpenCV, but it seems that these two lines cause you memory leak:
IplImage *InputImg = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
InputImg = cvLoadImage(UPDATED_IMAGE_STORAGE_PATH ,CV_LOAD_IMAGE_UNCHANGED);
In the first line you are creating an object, but in the second you are assigning another object to the pointer without releasing the previous one, so the previous one gets leaked.
Is creating an image even needed, while you are going to load it?

error: variable 'QQmlComponent component' has initializer but incomplete type in Qt5

i am playing with Exposing Attributes of C++ Types to QML in Qt5 based on this tutor http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html. when i run it i got this error on my issues pane error: variable 'QQmlComponent component' has initializer but incomplete type not only i have this error i have also this error the signal i have created using Q_PROPERTY is not detected
C:\Users\Tekme\Documents\QtStuf\quick\QmlCpp\message.h:15: error: 'authorChanged' was not declared in this scope
emit authorChanged();
^
my code is
#ifndef MESSAGE_H
#define MESSAGE_H
#include <QObject>
class Message : public QObject
{
Q_OBJECT
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
public:
void setAuthor(const QString &a) {
if (a != m_author) {
m_author = a;
emit authorChanged();
}
}
QString author() const {
return m_author;
}
private:
QString m_author;
};
#endif
and in my main.cpp
#include "message.h"
#include <QApplication>
#include <QQmlEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QQmlEngine engine;
Message msg;
engine.rootContext()->setContextProperty("msg",&msg);
QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
component.create();
return a.exec();
}
You are not including QQmlComponent header in your main.cpp:
#include <QQmlComponent>
You are also trying to emit a signal that you haven't declared yet. You should declare it in your message.h like this:
signals:
void authorChanged();
Check this example.
I believe you need to add:
signals:
void authorChanged();
to your class like this:
class Message : public QObject
{
Q_OBJECT
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
public:
void setAuthor(const QString &a) {
if (a != m_author) {
m_author = a;
emit authorChanged();
}
}
QString author() const {
return m_author;
}
signals:
void authorChanged();
private:
QString m_author;
};

reading data from linux input on embedded board

i compiled the following code for arm9 board. whenever i press any key the event should be detected.
keypress.ccp:
#include "keypress.h"
#include <QApplication>
#include <QKeyEvent>
KeyPress::KeyPress(QWidget *parent) :
QWidget(parent)
{
myLabel = new QLabel("LABEL");
mainLayout = new QVBoxLayout;
mainLayout->addWidget(myLabel);
setLayout(mainLayout);
}
void KeyPress::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_A)
{
myLabel->setText("You pressed A");
}
}
void KeyPress::keyReleaseEvent(QKeyEvent *event)
{
if(event->key())
{
qDebug()<<"key released";
}
if(event->key() == Qt::Key_A)
{
myLabel->setText("You released A");
}
}
keypress.h:
#ifndef KEYPRESS_H
#define KEYPRESS_H
#include <QWidget>
#include <QtGui>
class KeyPress : public QWidget
{
Q_OBJECT
public:
KeyPress(QWidget *parent = 0);
protected:
void keyPressEvent(QKeyEvent *);
void keyReleaseEvent(QKeyEvent *);
private:
QLabel *myLabel;
QVBoxLayout *mainLayout;
};
#endif // KEYPRESS_H
main.cpp
#include <QtGui>
#include "keypress.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"test";
KeyPress *keyPress = new KeyPress();
keyPress->show();
return a.exec();
}
using external key pad.
qt configuration:
/configure -embedded arm -xplatform qws/linux-arm-gnueabi-g++ -little-endian -qt-gfx-linuxfb -qt-gfx-qvfb -qt-kbd-tty -qt-kbd-qvfb -qt-kbd-linuxinput -qt-mouse-linuxinput -qt-mouse-qvfb -qt-mouse-pc -declarative -confirm-license
on board: export QWS_KEYBOARD="LinuxInput:/dev/input/event0"
when i run cat /dev/input/event0 | hexdump i am able to recieve keycodes for the key pressed. but when i run the the executable keypad is not responding. am i missing something? if so what else i should do to make the keypad work??

Resources