Hi I am currently working on a tournament creator program using QT Creator. I have been working on it at work during my lunch breaks and it has ran fine, but I have brought it home to get it finished for this weekend and it doesnt work.
The error which I am getting is
F:\Documents\Coding\TournamentOrganiser\startscreen.cpp:-1: error: multiple
definition of `StartScreen::StartScreen(QWidget*)'
I have literally only taken the folder placed it on a USB, copied it onto my desktop and ran it and this occurs, when it worked fine at home.
I assume that it is some kind of QT Creator configuration setting but just in case these are the files which are involved in the issue. If anyone can help me out with this it would be much appreciated as I need this working on my desktop for Saturday when I will need to use the app.
template <typename T> using shp = std::shared_ptr<T>;
globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
#include "startscreen.h"
#include "tournamentcreator.h"
#include "player.h"
#include <memory>
#include "util.h"
#include "matchups.h"
namespace globals{
extern shp<StartScreen> g_StartScreen;
extern shp<TournamentCreator> g_TournamentCreator;
extern shp<std::vector<Player>> g_PlayerData;
extern shp<MatchUps> g_MatchUps;
}
#endif // GLOBALS_H
main.cpp
#include "startscreen.h"
#include "tournamentcreator.h"
#include "matchups.h"
#include <QApplication>
#include "util.h"
namespace globals{
shp<StartScreen> g_StartScreen;
shp<TournamentCreator> g_TournamentCreator;
shp<std::vector<Player>> g_PlayerData;
shp<MatchUps> g_MatchUps;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
globals::g_StartScreen = std::make_shared<StartScreen>();
globals::g_TournamentCreator = std::make_shared<TournamentCreator>();
globals::g_PlayerData = std::make_shared<std::vector<Player>>();
globals::g_MatchUps = std::make_shared<MatchUps>();
globals::g_StartScreen->show();
return a.exec();
}
startscreen.h
#ifndef STARTSCREEN_H
#define STARTSCREEN_H
#include <QWidget>
namespace Ui {
class StartScreen;
}
class StartScreen : public QWidget
{
Q_OBJECT
public:
explicit StartScreen(QWidget *parent = 0);
~StartScreen();
private slots:
void on_newEventButton_clicked();
private:
Ui::StartScreen *ui;
};
#endif // STARTSCREEN_H
startscreen.cpp
#include "startscreen.h"
#include "ui_startscreen.h"
#include "globals.h"
StartScreen::StartScreen(QWidget *parent) :
QWidget(parent),
ui(new Ui::StartScreen)
{
ui->setupUi(this);
}
StartScreen::~StartScreen()
{
delete ui;
}
void StartScreen::on_newEventButton_clicked(){
this->hide();
globals::g_TournamentCreator->show();
}
Related
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);
}
I encountered an error and I didn't find any solution (even over the internet)
I created aQt app to receive data using a TCP protocol and plot them using QcustomPlot.
I have the following files:
mainwindow.h :
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"
#include <QVector>
#include <iostream>
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = Q_NULLPTR);
// ...
private:
struct addrinfo _hints;
struct addrinfo* _result = NULL;
// ...
};
mainwindow.cpp :
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#include "mainwindow.h"
#include <QVector>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//...
}
and the main.cpp file:
#include "mainwindow.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I have the following error:
'MainWindow::hints' uses undefined struct 'addrinfo' (compiling source file main.cpp).
I don't understand why I have this error, because I tested the same program in a classic consol app following the microsoft tutorial and it was working.
I believe it comes from the includes, but I still dont have a clue about which one causes that.
You need to #include something in your mainwindow.h that defines struct addrinfo, because your MainWindow class has a member variable of that type. At the moment you include all the socket stuff only in your *.cpp file.
You use #define WIN32_LEAN_AND_MEAN that prevents including many dependent header files and makes you include required header files explicitly. As for addrinfo you have to #include <ws2def.h>.
I donĀ“t know why I got this problem, the header files are just included once but it continuous showing the error, I also already check the .pro file and every SOURCE is just included once. How can I solve this?
I will attach the files I'm using and their include and the code of the little ones
globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
#include <QtGlobal>
#include "structs.h"
QT_BEGIN_NAMESPACE
struct ListaPaquetes;
QT_END_NAMESPACE
extern ListaPaquetes *nuevoPaquete;
#endif // GLOBALS_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtGui>
#include <QDialog>
#include <QtCore>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_startButton_clicked();
void on_planificarProduButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
produccion.h (only the first lines)
#ifndef PRODUCCION_H
#define PRODUCCION_H
#include <QDialog>
structs.h (onyl first lines)
Here is the struct ListaPaquetes, which is the responsible of the error, so the problem I guess is with this file but I don't know why
#ifndef STRUCT_H
#define STRUCT_H
#include <QString>
#include <QList>
struct Nodo
{
int cantidad;
QString tipoPaquete;
Nodo *siguiente;
Nodo *anterior;
Nodo(){}
Nodo (int cant, QString paquete)
{
cantidad = cant;
tipoPaquete = paquete;
siguiente = nullptr;
anterior = nullptr;
}
};
struct ListaPaquetes
{
Nodo *pn;
ListaPaquetes()
{
pn = nullptr;
}
void crearPaquete(int,QString);
QList<QString> paquetesAgregados();
};
void ListaPaquetes::crearPaquete(int cant, QString paquete)
{
if (pn==nullptr)
{
pn = new Nodo(cant, paquete);
pn->siguiente = pn;
pn->anterior = pn;
}
else
{
Nodo *nuevo = new Nodo(cant, paquete);
pn->anterior->siguiente=nuevo;
nuevo->anterior = pn->anterior;
nuevo->siguiente=pn;
pn->anterior = nuevo;
}
}
QList<QString> ListaPaquetes::paquetesAgregados()
{
QList <QString> paquetes;
if (pn!=nullptr)
{
Nodo *tmp = pn;
do
{
paquetes.append(tmp->tipoPaquete);
tmp = tmp->siguiente;
}while(tmp!=pn);
}
return paquetes;
}
#endif // STRUCT_H
globals.cpp
#include "globals.h"
ListaPaquetes *nuevoPaquete = new ListaPaquetes();
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 <QColor>
#include <QColormap>
#include <QtCore>
#include <QtGui>
#include <QMessageBox>
#include "produccion.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_startButton_clicked()
{
if(ui->cantGalletas->text() == "")
{
QMessageBox::warning(this,"Error","Debe introducir una cantidad de
galletas");
}
}
void MainWindow::on_planificarProduButton_clicked()
{
Produccion *ventanaPaquetes = new Produccion();
ventanaPaquetes->show();
}
produccion.cpp (When I run something from here, it shows the error)
#include "produccion.h"
#include "ui_produccion.h"
#include <QtCore>
#include <QMessageBox>
#include "globals.h"
Here are the errors:
You are getting the problem because you are putting the implementation of member functions in header files (here structs.h). Every source file that includes globals.h will include structs.h and compile the functions implemented there. When you link your program these functions are multiply defined
You need to move ListaPaquetes::crearPaquete(int cant, QString paquete) and
ListaPaquetes::crearPaquete(int cant, QString paquete) into structs.cpp
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;
}
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.