I am trying to use a method called getName() that is defined in the addPlayer.h file below. The getname() method will be used in the constructor called Player. The error that I get is: 'getName' was not declared in this scope'. How can I fix it?
Thanks for helping.
The addPlayer.h file:
#include "../Source/Player.h"
class addPlayer : public QDialog
{
Q_OBJECT
public:
addPlayer(QWidget *parent = 0);
~addPlayer();
tp::Player* makePlayer();
void addPlayer();
QString getName() const;
inline QString addPlayer::getName() const
{
return (ui.name_lineEdit->text());
}
The addPlayer.cpp file:
#include "addPlayer.h"
#include <QMessageBox>
using namespace tp;
addPlayer::addPlayer(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
QObject::connect(ui.ok_pushButton, SIGNAL(clicked()), this,
SLOT(validatePlayer()));
QObject::connect(ui.dateNaissance_pushButton, SIGNAL(clicked()), this,
SLOT(getDate()));
}
addPlayer::~addPlayer()
{
}
Player* makePlayer()
{
return new Player(getName().toStdString());
}
The constructor is as follows:
class Player: public HumanBeing {
public:
//Constructor
Player(const std::string& p_name);
Player::Player(const std::string& p_name): m_name(p_name)
You haven't properly scoped your definition of makePlayer -- you need to prefix addPlayer:::
Player* addPlayer::makePlayer()
{
return new Player(getName().toStdString());
}
Related
I have found a helper class for enums (EnumHelper) to convert enums to strings and strings to enums.
I want to use this class in another class (MyEnums) in which I define different enums and want to provide a ToString() and FromString() method for each enum type.
If I try to convert an enum from a third class (Test) I get an error message that I don't understand.
EnumHelper.h
#pragma once
#include <QMetaEnum>
template <typename E>
class EnumHelper
{
public:
EnumHelper();
QString toString(E value);
};
EnumHelper.cpp
#include "EnumHelper.h"
template<typename E>
EnumHelper<E>::EnumHelper()
{
}
template<typename E>
QString EnumHelper<E>::toString(E value)
{
const int retval = static_cast<int>(value);
return QString::fromUtf8(QMetaEnum::fromType<E>().valueToKey(retval));
}
MyEnums.h
#pragma once
#include <QObject>
#include "EnumHelper.h"
class ColorNameEnums
{
Q_GADGET
public:
enum Value {
White,
Grey,
LightGrey,
DarkerGrey,
DarkGrey,
Mint,
DarkMint,
Red,
DarkRed,
Black,
Blue
};
Q_ENUM(Value)
private:
explicit ColorNameEnums();
};
class MyEnums : QObject
{
public:
explicit MyEnums(QObject* parent = nullptr);
QString colorNameToString(ColorNameEnums value);
ColorNameEnums colorNameFromString(QString value);
private:
EnumHelper<ColorNameEnums> m_colorName;
};
MyEnums.cpp
#include "MyEnums.h"
MyEnums::MyEnums(QObject* parent)
{
}
QString MyEnums::colorNameToString(ColorNameEnums value) { return m_colorName.toString(value); }
ColorNameEnums MyEnums::colorNameFromString(QString value) { return m_colorName.fromString(value); }
Test.h
#pragma once
#include <QObject>
#include "MyEnums.h"
class Test : public QObject
{
Q_OBJECT
public:
explicit Test(QObject* parent = nullptr);
private:
MyEnums* m_enums = new MyEnums();
};
Test.cpp
#include "Test.h"
Test::Test(QObject* parent)
{
}
void Test::test()
{
auto xx = m_enums->colorNameToString(ColorNameEnums::White);
}
Error is:
For a conversion of ""ColorNameEnums::Value"" to""ColorNameEnums"" no suitable constructor is available.
Started using QtRO and generated files inherently complain about vtable:
#ifndef REP_REMOTEEXAMPLE_H
#define REP_REMOTEEXAMPLE_H
// This is an autogenerated file.
// Do not edit this file, any changes made will be lost the next time it is generated.
#include <QtCore/qobject.h>
#include <QtCore/qdatastream.h>
#include <QtCore/qvariant.h>
#include <QtCore/qmetatype.h>
#include <QtRemoteObjects/qremoteobjectnode.h>
#include <QtRemoteObjects/qremoteobjectpendingcall.h>
#include <QtRemoteObjects/qremoteobjectreplica.h>
class remoteExampleReplica : public QRemoteObjectReplica
{
Q_OBJECT
Q_CLASSINFO(QCLASSINFO_REMOTEOBJECT_TYPE, "remoteExample")
Q_CLASSINFO(QCLASSINFO_REMOTEOBJECT_SIGNATURE, "5e40a606abdd95f709878d419edaa735fce25d0d")
public:
remoteExampleReplica() : QRemoteObjectReplica() { initialize(); }
static void registerMetatypes()
{
static bool initialized = false;
if (initialized)
return;
initialized = true;
}
private:
remoteExampleReplica(QRemoteObjectNode *node, const QString &name = QString())
: QRemoteObjectReplica(ConstructWithNode)
{
initializeNode(node, name);
}
void initialize() override
{
remoteExampleReplica::registerMetatypes();
QVariantList properties;
properties.reserve(0);
setProperties(properties);
}
public:
virtual ~remoteExampleReplica() {}
Q_SIGNALS:
void Close(QString a);
void Open(QString b);
public Q_SLOTS:
void onClosed()
{
static int __repc_index = remoteExampleReplica::staticMetaObject.indexOfSlot("onClosed()");
QVariantList __repc_args;
send(QMetaObject::InvokeMetaMethod, __repc_index, __repc_args);
}
void onOpened()
{
static int __repc_index = remoteExampleReplica::staticMetaObject.indexOfSlot("onOpened()");
QVariantList __repc_args;
send(QMetaObject::InvokeMetaMethod, __repc_index, __repc_args);
}
private:
friend class QT_PREPEND_NAMESPACE(QRemoteObjectNode);
};
#if (QT_VERSION < QT_VERSION_CHECK(5, 5, 0))
#endif
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
#endif // REP_REMOTEEXAMPLE_H
The issue points to the private constructor. I only included the replica header in my code without really using the remote object as a test run. I've read a lot about vtables how they work and when the linker complains about the vtable but in this case, with the private constructor, I'm not sure what the issue is. Am I missing an implementation of the remote object? Did I not generate the files correctly?
.rep:
class remoteExample
{
SIGNAL(Close(QString a));
SIGNAL(Open(QString b));
SLOT(onClosed());
SLOT(onOpened());
};
I have an enum that I use in qml
class SettingManager : public QObject
{
Q_OBJECT
public:
enum BookKinds{
BookKind1=0,
BookKind2=1,
};
Q_ENUMS(BookKinds)
Q_PROPERTY(BookKinds bookKind READ bookKind WRITE setBookKind NOTIFY bookKindChanged)
explicit SettingManager(QObject *parent = nullptr);
void setBookKind(BookKinds dkob);
BookKinds bookKind();
signals:
void bookKindChanged();
};
in main.cpp I registerd SettingManager
qmlRegisterType<SettingManager>("Test",1,0,"SettingManager");
I use this in qml file
onCurrentIndexChanged:
{
if(tbarBookKindForDisplay.currentIndex==0)
{
settingManager.bookKind=BookManager.BookKind1;
}
else if(tbarBookKindForDisplay.currentIndex==1){
settingManager.bookKind=BookManager.BookKind2;
}
}
when CurrentIndex of TabBar changes below error occurs:
Error: Cannot assign [undefined] to int
You register the type as SettingManager but use it as BookManager. The correct code is:
settingManager.bookKind = SettingManager.BookKind1;
You should also use Q_ENUM instead of Q_ENUMS.
I am creating a popuo window that can change the message that shows. I have the next class
class NoPutPort : public QDialog, public Ui::NoPortPut
{
Q_OBJECT;
public:
NoPutPort(QWidget *parent=0) {
setupUi(this);
}
~NoPutPort(void) {}
void putPort(QString a){
ui.label_2->setText(a);
}
private:
Ui::NoPortPut ui;
};
The problem if when I call the method putPort, the application crash and I dont know why. If I put ui.label_2, it dont crash, but when I access to the object to modify it, it crash.
Anyone knows how can I modify the label correctly?
You've messed up the code. It should be:
class NoPutPort : public QDialog
{
Q_OBJECT;
public:
NoPutPort(QWidget *parent=0) {
ui.setupUi(this);
}
~NoPutPort(void) {}
void putPort(QString a){
ui.label_2->setText(a);
}
private:
Ui::NoPortPut ui;
};
XOR
class NoPutPort : public QDialog, public Ui::NoPortPut
{
Q_OBJECT;
public:
NoPutPort(QWidget *parent=0) {
setupUi(this);
}
~NoPutPort(void) {}
void putPort(QString a){
label_2->setText(a);
}
};
I have declared QOBJECT macro but still its calling the function without passing the QObject object
Here is my code
keyusermanagertest.cpp
#define private public
#define protected public
#include "keyusermanagertest.h"
#include "storageusermanager.h"
#include "keyusermanager.h"
#include "alkuser.h"
#undef protected
#undef private
#include <QDebug>
#include <QtTest/QtTest>
QTEST_MAIN(KeyUserManagerTest)
void KeyUserManagerTest::init()
{
}
void KeyUserManagerTest::cleanup()
{
}
void KeyUserManagerTest::test_initialization()
{
// Already tested under BackendTest::test_initialization()
}
void KeyUserManagerTest::settersAndGetters()
{
AlkUser userInfo;
QString user="Puneet Goyal";
StorageUserManager* storageuser=new StorageUserManager();
KeyUserManager* keyuser=new KeyUserManager(storageuser);
keyuser->updateUserData(user,userInfo);
qDebug()<<"UPDATION DONE!!!!";
// Now setting the rest of the details for user Puneet Goyal using AlkUser Object
userInfo.setName("Puneet");
userInfo.setContact("21897121");
userInfo.setType("savings");
userInfo.setAccount("123456789");
userInfo.setAmount("100000");
// Now retrieving all the user details using KeyUserManager Object
QVariant vari=keyuser->getUserInfo("Puneet Goyal");
}
keyusermanagertest.h
#ifndef KEYUSERMANAGERTEST_H
#define KEYUSERMANAGERTEST_H
#include <QtCore/QObject>
class KeyUserManager;
class KeyUserManagerTest : public QObject
{
Q_OBJECT
private slots:
void init();
void cleanup();
void test_initialization();
void settersAndGetters();
};
#endif
Its compile output is as follows"
/home/puneet/puneet/office/alkimia/payment/backend/keyusermanagertest.cpp: In member function ‘void KeyUserManagerTest::settersAndGetters()’:
/home/puneet/puneet/office/alkimia/payment/backend/keyusermanagertest.cpp:52: error: no matching function for call to ‘StorageUserManager::StorageUserManager()’
/home/puneet/puneet/office/alkimia/payment/backend/storageusermanager.h:41: note: candidates are: StorageUserManager::StorageUserManager(QObject*)
/home/puneet/puneet/office/alkimia/payment/backend/storageusermanager.h:37: note: StorageUserManager::StorageUserManager(const StorageUserManager&)
/home/puneet/puneet/office/alkimia/payment/backend/keyusermanagertest.cpp:53: error: no matching function for call to ‘KeyUserManager::KeyUserManager(StorageUserManager*&)’
/home/puneet/puneet/office/alkimia/payment/backend/keyusermanager.h:44: note: candidates are: KeyUserManager::KeyUserManager(StorageUserManager*, QObject*)
/home/puneet/puneet/office/alkimia/payment/backend/keyusermanager.h:41: note: KeyUserManager::KeyUserManager(const KeyUserManager&)
Thanks
Add constructor to your KeyUserManagerTest -
in header add
KeyUserManagerTest (QObject* parent=0);
and in cpp
KeyUserManagerTest::KeyUserManagerTest(QObject* parent):QObject(parent){};
QOBJECT macro does not create constructor for you!