How do I connect this function to my connectButton? - c++

I am trying to connect the addDevice() function from my Device class to my connectButton on my GUI. However no matter how I try and code the connect function, it does not work. Currently it is giving me the error:
primary expression expected before the ',' token.
I have tried the follow syntaxes:
connect(ui->connectButton,SIGNAL(clicked(bool)),d,SLOT(startDeviceDiscovery()));
connect(ui->connectButton,SIGNAL(clicked(bool)),d,&Device::startDeviceDiscovery());
connect(ui->connectButton,SIGNAL(clicked(bool)),this,SLOT(startDeviceDiscovery()));
connect(ui->connectButton,SIGNAL(clicked(bool)),this,Device::startDeviceDiscovery());
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QObject>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QtBluetooth>
#include <QDebug>
#include <QtWidgets>
#include "device.h"
#include "deviceinfo.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Device d;
connect(ui->connectButton,SIGNAL(clicked(bool)),Device,SLOT(startDeviceDiscovery()));
//
.
.
.
}
Device.h
#ifndef DEVICE_H
#define DEVICE_H
#include <QObject>
#include <qbluetoothglobal.h>
#include <qbluetoothlocaldevice.h>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QLowEnergyController>
#include <QBluetoothDeviceInfo>
#include <QBluetoothServiceInfo>
#include "deviceinfo.h"
#include <QList>
#include <QVariant>
class Device : public QObject
{
Q_OBJECT
public:
explicit Device(QObject *parent = 0);
QVariant name();
~Device();
signals:
void address(QVariant);
public slots:
void startDeviceDiscovery();
void connectDeivce(const QString &address);
};
#endif // DEVICE_H

For a connect you need a pointer, and you created your Device on the constructor, this means that just after the connect is used, constructor would finish, and this would destroy your device, disconnecting the function.
Correct syntax:
Device *myDevice = new Device();
connect(
ui->connectButton, // the pointer of the signal emitter
&QPushButton::clicked, // the signal you are interested
myDevice, // the pointer to receiving end
&Device::startDeviceDiscovery); // what you wanna trigger
you mixed every possibility in your tests, but in all of them you treated the slot as a method, calling the () operator. you can't call the call operator, on the connect you are passing the pointer of the method to a function that will call that for you when the time is right.

Related

Downloading txt file with qtnetwork

I am trying to download a txt file from a url in QT but i can't seem to make it work.
I am following this guide https://wiki.qt.io/Download_Data_from_URL. I implemented the filedownloader class exactly like it's made in the guide, but when i try to use it like specified in the guide I cannot make it work. I created a slot to be called when the download is finished, but if i try to call the downloader inside like the guide it says it is an undeclared identifier.
Does anyone know how to correctly implement this code?
this is the .cpp of my code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QStringList>
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QList>
#include <QtNetwork/QNetworkReply>
#include <QStringList>
#include <QTimer>
#include <QUrl>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <filedownloader.h>
#include <iostream>
#include <QObject>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl emiurl( "url of my file");
// call to the downloader class.In the guide it's made differently, but it doesn't work
FileDownloader emiload(emiurl,this);
//this connect links the end of the download with the textwriter slot
QObject::connect(&emiload, SIGNAL (downloaded()), this, SLOT (textwriter()));
}
>MainWindow::~MainWindow()
{
delete ui;
}
//slot needed to create the txt file from the downloaded one
void MainWindow::textwriter()
{
QByteArray emibyte;
emibyte=emiload->downloadedData(); //this line gives me error
QFile emifile("emi.txt");
emifile.open(QIODevice::WriteOnly);
std::cout << emibyte.size() << std::endl;
QDataStream out(&emifile);
out << emibyte;
std::cout << emifile.size() << std::endl;
}
Now here's the .h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "filedownloader.h"
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
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;
private slots:
void textwriter();
};
#endif // MAINWINDOW_H
To make the undeclared identifier go away and successfully compile, you need to add FileDownloader to the class' declaration. This way, it will be known throughout the class.
I chose to go with the approach that's usual in Qt, to declare a pointer to FileDownloader.
#pragma once // <--- this is supported by virtually any compiler today
#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class FileDownloader; // <-- forward declaration is enough, but you can also #include "filedownloader.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
private:
Ui::MainWindow* ui = nullptr;
FileDownloader* emiload = nullptr; // <--- the important line!
private slots:
void textwriter();
};
And then instantiate and call emiload in the constructor:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// create an instance of FileDownloader with "new".
emiload = new FileDownloader(QUrl("url of my file"), this);
// using member pointer connection available since Qt5
connect(emiload, &FileDownloader::downloaded, this, &MainWindow::textwriter);
}

How to connect QObbject (QPushButton) to a method from other class?

I'm currently working on a project that need the connection between a QPushButton of a Qwidget class (window) and a void method from a "classical" class.
I've tried to connect them with all the solution that I've read but none works correctly.
Either the compiler returns me
QObject::connect: No such slot QWidget::Class::metoh()
or it won't compile at all without errors.
Here's the simpliest code that I've tried. I read the Qt documentation but it didn't help me. I've also tried to include the Q_OBJECT Macro but it lead to errors. I've also read that with Qt5 it's no more mandatory to define slots. Have I understood correctly ?
How can I connect the Some_Class method to the QPushButton ?
// Classical class header //
#ifndef DEF_CLASS
#define DEF_CLASS
#include <iostream>
#include <cstdlib>
#include <string>
class Some_class
{
protected: // permet l'acces pour les methodes style soin
Some attributes;
public:
Some_Class(int id);
//Personnage(int id, int vieAtStart, int degatsArmeAtStart);
void method();
// obligation de passer par des references pour avoir une modification effective
~Some_Class();
};
#endif
// Classical class cpp //
#include "Some_Class.h"
#include <QObject>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
Some_Class::Some_Class(int id)
{
Id = id;
some attributes;
};
void Some_Class::method()
{
modification of attributes;
};
// Window class header //
#ifndef DEF_WINDOW
#define DEF_WINDOW
#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QIcon>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QImage>
#include <QPixmap>
#include <QLabel>
#include <QGraphicsPixmapItem>
#include "Some_Class.h"
class Window : public QWidget // On hérite de QWidget (IMPORTANT)
{
public:
Window(Some_Class test);
// public slots:
// void go_right();
private:
QPushButton *m_button;
};
#endif
// Window class cpp //
#include "Window.h"
#include <string>
#include <QObject>
Window::Window(Some_Class test) : QWidget()
{
setFixedSize(874, 968);
m_button = new QPushButton("test", this);
QObject::connect(m_button, SIGNAL(clicked()), this, SLOT(Some_Class::method()));
//QObject::connect(m_bouton_droite, SIGNAL(clicked()), qApp, Personnage::go_right());
//QObject::connect(m_bouton_droite, &QPushButton::clicked,qApp, Personnage::go_right());
}
#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QIcon>
#include "Window.h"
#include "Some_Class.h"
#include <QObject>
// main.cpp //
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window win(Heros);
win.show();
return app.exec();
}
First, you will need an object of Some_Class with an appropriate scope. Currently you only have test in your constructor which is a value parameter and thus gets destroyed when the constructor returns. One option would be to pass in test as a pointer if the object you are passing in has the correct lifetime.
Window::Window(Some_Class *test)
If that is not appropriate in your case you will need to keep a copy inside your Window object. For the rest of this answer I will however assume you pass in a pointer.
With a normal C++ object (not a QObject) you cannot use the SIGNAL/SLOT macro's because they require the use of the slot/signal markers in the class definition.
I see you tried to use the modern method pointer syntax but you made one error. The third parameter needs to be the address of an object of type Some_Class.
QObject::connect(m_button, &QPushButton::clicked, test, &Some_Class::method);
However that is not all, for this to work the signature of clicked has to match with Some_Class::method which it doesn't. clicked has a bool checked parameter.
The construct with the SIGNAL/SLOT macro's allows the slot to have less parameters but this version of connect is more strict and requires a strict match.
You can either add the parameter to method or use a lambda if you have C++11.
QObject::connect(m_button, &QPushButton::clicked, [test] (bool) {
test->method();
});

QT: Error [expected ")" before "*" token and MainWindow does not name a type]

I got two errors in Qt, I wish you can help me with this. This is my first time I post something, please me help. This is the code:
#include <QMainWindow>
#include "mainwindow.h"
#include <QThread>
#include <QtCore>
#include "ui_mainwindow.h"
class HiloPrincipal : public QThread
{
Q_OBJECT
public:
explicit HiloPrincipal( MainWindow* parent = 0); // First error
MainWindow * _parent; // Second error
void run(); // Sacar Botella y poner botella.
signals:
public slots:
};
#endif // HILOS_H
I almost sure you have created include cycle.
Fix it like this:
#ifndef HILOS_H // this was missing! Probably when you did copy paste to question
#define HILOS_H
#include <QMainWindow>
// remove line: #include "mainwindow.h"
#include <QThread>
#include <QtCore>
// remove line: #include "ui_mainwindow.h"
// forward declaration
class MainWindow;
class HiloPrincipal : public QThread
{
Q_OBJECT
public:
explicit HiloPrincipal( MainWindow* parent = 0); // First error
MainWindow * _parent; // Second error
void run(); // Sacar Botella y poner botella.
signals:
public slots:
};
#endif // HILOS_H
Than include this headers files mainwindow.h in HiloPrincipal.cpp. This should solve build issue, but not actual problem.
Note problem is caused by invalid design of classes. Your HiloPrincipal thread shouldn't have any knowledge about MainWindow.
Also you're doing that wrong - this is invalid use of QThread.

C++ accessing object, QT

I'd appreciate it, when someone could help me. I am not used to C++. I am new to it.
My problem: I want to make an object "player" usable for another class. I don't get how to archive this.
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
#include <QMainWindow>
#include <QMediaPlayer>
#include <QDebug>
#include "Leap.h"
#include <leapmotionlistener.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QMediaPlayer* player;
private slots:
....
private:
Ui::MainWindow *ui;
void initialize();
Controller controller;
LeapMotionListener leapMotionListener;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSound>
#include <iostream>
#include <QfileDialog>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
connect(player, &QMediaPlayer::positionChanged, this, &MainWindow::on_positionChanged);
connect(player, &QMediaPlayer::durationChanged, this, &MainWindow::on_durationChanged);
initialize();
}
QString path;
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initialize(){
controller.addListener(leapMotionListener);
leapMotionListener.setPlayer(player);
controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
}
...
leapmotionlistener.h
#ifndef LEAPMOTIONLISTENER
#define LEAPMOTIONLISTENER
#include <iostream>
#include <cstring>
#include "Leap.h"
#include <QFile>
#include <QAudioOutput>
#include <QMediaPlayer>
using namespace Leap;
class LeapMotionListener : public Listener {
public:
LeapMotionListener();
void setPlayer(QMediaPlayer&);
//... some more methods
private:
QMediaPlayer player;
};
#endif // LEAPMOTIONLISTENER
leapmotionlistener.cpp
//calls the player object in one method like the following
player.stop();
My main questions are: What am I doing wrong while referencing and instantiating? And how can leapmotionlistener access the same player object (I want to influence the audio)?
I get the Error:
MusicPlayer\mainwindow.cpp:32: Error: C2664: 'void LeapMotionListener::setPlayer(QMediaPlayer &)' : converting from argument 1 'QMediaPlayer *' in 'QMediaPlayer &' not possible
This little project is downloadable for a quick view with the following link:
https://www.dropbox.com/s/igr7ywnvicdlxxd/MusicPlayer.zip?dl=0
Thanks in advance!
All you need to do is
leapMotionListener.setPlayer(*player);
An lvalue reference binds to an object, not to the pointer to the object. In order to get the object from a pointer to it, you need to dereference the pointer using the *.
Among other problems, the QMediaPlayer you're trying to pass to setPlayer is a pointer, not a reference to an object.
You can either instantiate your player object like this:
QMediaPlayer player;
Or you can change your setPlayer function signature to this:
void setPlayer(QMediaPlayer* mplayer);
If you update your function signature, you'll also need fix other function signatures and leave the ampersands out of your connect() statements when you're referencing the player.
the following worked now:
in MainWindow.h:
public:
QMediaPlayer* player;
in MainWindow.cpp:
player = new QMediaPlayer(this);
leapMotionListener.setPlayer(player);
controller.addListener(leapMotionListener);
in LeapMotionListener.h:
private:
QMediaPlayer *player;
public:
void setPlayer(QMediaPlayer *mplayer);
in LeapMotionListener.cpp:
LeapMotionListener::LeapMotionListener()
: player(0)
{}
void LeapMotionListener::setPlayer(QMediaPlayer *mplayer){
player = mplayer;
}

Emitting a signal not working properly

I have a class called dosecalibration which contains dosecalibration.cpp and dosecalibration.h. The class is associated to a separate ui form. On the form, when a button is clicked, a signal is emitted.
There is a connection within main window to receive this signal, however it doesn't seem to be working. The code is as following:
dosecalibration.h :
#ifndef DOSECALIBRATION_H
#define DOSECALIBRATION_H
#include <QDialog>
#include <QDebug>
namespace Ui {
class dosecalibration;
}
class dosecalibration : public QDialog
{
Q_OBJECT
public:
explicit dosecalibration(QWidget *parent = 0);
~dosecalibration();
double dosefactor;
//bool dose;
private slots:
void on_useCharge_clicked();
void on_useCounts_clicked();
void on_pushButton_clicked();
// void on_pCSB_valueChanged();
// void on_countsSB_valueChanged();
signals:
void applydose();
private:
Ui::dosecalibration *ui;
};
#endif // DOSECALIBRATION_H
dosecalibration.cpp :
#include "dosecalibration.h"
#include "ui_dosecalibration.h"
dosecalibration::dosecalibration(QWidget *parent) :
QDialog(parent),
ui(new Ui::dosecalibration)
{
ui->setupUi(this);
ui->countsSB->setEnabled(false);
ui->countsSB->setValue(ui->pCSB->value()*100/9.6);
}
dosecalibration::~dosecalibration()
{
delete ui;
}
void dosecalibration::on_useCharge_clicked()
{
ui->countsSB->setEnabled(false);
ui->pCSB->setEnabled(true);
}
void dosecalibration::on_useCounts_clicked()
{
ui->pCSB->setEnabled(false);
ui->countsSB->setEnabled(true);
}
void dosecalibration::on_pushButton_clicked()
{
if(ui->useCharge->isChecked()){
dosefactor = ui->pCSB->value();
}
else if(ui->useCounts->isChecked()){
dosefactor = ui->countsSB->value();
}
emit applydose();
}
//void dosecalibration::on_pCSB_valueChanged()
//{
// ui->countsSB->setValue(ui->pCSB->value()*100/9.6);
//}
//void dosecalibration::on_countsSB_valueChanged()
//{
// ui->pCSB->setValue(ui->countsSB->value()*9.6/100);
//}
And mainwindow.h (only included the 'includes' and the slots):
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <AFE_variables.h>
#include <QMainWindow>
#include "FPGA/fpga.h"
#include "FPGA/okFrontPanelDLL.h"
#include "decoder.h"
#include "analysis.h"
#include "about.h"
#include "logfile.h"
#include "Graph/graphicsscene.h"
#include "Graph/graphdialog.h"
#include "Graph/qcustomplot.h"
#include "Graph/graphicsview.h"
#include "settingsdialog.h"
#include "dosecalibration.h"
#include <QFileDialog>
#include <QProgressBar>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsTextItem>
#include <QDialog>
#include <QDebug>
#include <math.h>
slots:
//Dose calibration
void InitialiseGraphsAfterDose();
void on_actionDose_Calibration_4_triggered();
//void doseCalibrationEnabled();
private:
//Initalisation
void onStart();
void AllocateMemory();
void ConnectFPGA();
bool CheckConnection();
Ui::MainWindow *ui;
okCFrontPanel *xem;
FPGA *fpga;
QProgressBar *progressBar;
QTimer *PlayTimer;
LogFile *logfiledialog;
LogFile *logfileanalysis;
QString LoadLogFilePath;
dosecalibration *dose;
and a snippet from graphing.cpp, which is a part of the main window class:
//connects for dose calibration
dose = new dosecalibration(this);
connect(dose,SIGNAL(applydose()),this,SLOT(InitialiseGraphsAfterDose()));
}
void MainWindow::InitialiseGraphsAfterDose()
{
apply_dose = true;
InitialiseGraphs();
qDebug() << apply_dose;
}
So the applydose() signal is emitted at the push of a button in the dosecalibration ui. The connect should mean that the value of apply_dose is sent to console, however nothing is displayed.
EDIT:
Placing the connect within an if statement to determine if it is truly connecting confirms that it is indeed working correctly.
//connects for dose calibration
dose = new dosecalibration(this);
if(connect(dose,SIGNAL(applydose()),this,SLOT(InitialiseGraphsAfterDose())))
{
qDebug() << "connect worked";
}
}
The code above successfully outputs the message confirming the connect.
Any idea?
Remove the multiple instances of dosecalibration, or make sure to connect each one of those, if you really need multiple instances.