Reputation: 1
So this is my first QT program and I can't get it to work as intended. I tried to connect buttons to functions in the Player class. The compiler shows me no errors but the program doesn't reach the functions when I press the buttons except for the function showPoints() in MainWindow. I also know there is something wrong in my showPoints() function as it doesn't hide the lcdNumbers. Here is my code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QObject>
#include "player.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
Ui::MainWindow *ui;
public slots:
void showPoints();
};
#endif // MAINWINDOW_H
maindwindow.h
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QTextEdit>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
Player playerOne(nullptr, ui->textEdit_outputPlayerOne, ui->lcdNumber_pointsPlayerOne),
playerTwo(nullptr, ui->textEdit_outputPlayerTwo, ui-> lcdNumber_pointsPlayerTwo);
// connect(ui->pushButton_normalPlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addNormal()));
connect(ui->pushButton_normalPlayerOne, &QPushButton::clicked, &playerOne, &Player::addNormal);
connect(ui->pushButton_normalPlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addNormal()));
connect(ui->pushButton_rarePlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addRare()));
connect(ui->pushButton_rarePlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addRare()));
connect(ui->pushButton_epicPlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addEpic()));
connect(ui->pushButton_epicPlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addEpic()));
connect(ui->pushButton_LegendaryPlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addLegendary()));
connect(ui->pushButton_legendaryPlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addLegendary()));
connect(ui->pushButton_goldenNormalPlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addGoldenNormal()));
connect(ui->pushButton_goldenNormalPlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addGoldenNormal()));
connect(ui->pushButton_goldenRarePlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addGoldenRare()));
connect(ui->pushButton_goldenRarePlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addGoldenRare()));
connect(ui->pushButton_goldenEpicPlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addGoldenEpic()));
connect(ui->pushButton_goldenEpicPlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addGoldenEpic()));
connect(ui->pushButton_goldenLegendaryPlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addGoldenLegendary()));
connect(ui->pushButton_goldenLegendaryPlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addGoldenLegendary()));
connect(ui->pushButton_pointsPlayerOne ,SIGNAL(clicked()), &playerOne, SLOT(addRoundPoints()));
connect(ui->pushButton_pointsPlayerTwo ,SIGNAL(clicked()), &playerTwo, SLOT(addRoundPoints()));
//connect(ui->pushButton_showPoints, SIGNAL(clicked()), this, SLOT(showPoints()));
connect(ui->pushButton_showPoints, &QPushButton::clicked, this, &MainWindow::showPoints);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showPoints()
{
qDebug() << "hitting the method";
if(ui->lcdNumber_pointsPlayerOne->isVisible() == true)
{
ui->lcdNumber_pointsPlayerOne->hide();
ui->lcdNumber_pointsPlayerTwo->hide();
}
else
{
ui->lcdNumber_pointsPlayerOne->show();
ui->lcdNumber_pointsPlayerTwo->show();
}
}
maindwindow.cpp
#ifndef PLAYER_H
#define PLAYER_H
#define NORMAL_RARITY 1
#define GOLDEN_NORMAL_RARITY 1
#define RARE_RARITY 1
#define GOLDEN_RARE_RARITY 1
#define EPIC_RARITY 1
#define GOLDEN_EPIC_RARITY 1
#define LEGENDARY_RARITY 1
#define GOLDEN_LEGENDARY_RARITY 1
#define POINTS_FOR_RARITY 1
#include <sstream>
#include <string>
#include <QObject>
#include <QWidget>
#include <QTextEdit>
#include <QString>
#include <QLCDNumber>
class Player : public QWidget
{
Q_OBJECT
private:
static int _instance;
int _id;
int _roundpoints = 0;
int _points = 0;
int _cardsPerRound = 0;
QTextEdit * _textEdit;
QLCDNumber * _pointDisplay;
public:
Player(QWidget *parent = nullptr, QTextEdit * textEdit = nullptr, QLCDNumber *pointDisplay = nullptr);
public slots:
int returnPoints() const;
std::string returnRoundpoints() const;
void addNormal();
void addGoldenNormal();
void addRare();
void addGoldenRare();
void addEpic();
void addGoldenEpic();
void addLegendary();
void addGoldenLegendary();
void addRoundPoints();
void resetPoints();
void resetRoundPoints();
};
#endif // PLAYER_H
player.h
#include "player.h"
#include <QDebug>
Player::Player(QWidget *parent, QTextEdit *textEdit, QLCDNumber *pointDisplay) : QWidget(parent), _id(_instance++), _textEdit(textEdit), _pointDisplay(pointDisplay)
{
}
int Player::returnPoints() const
{
return _points;
}
std::string Player::returnRoundpoints() const
{
std::stringstream s;
s << "In dem aktuellen Pack haben Sie eine Punktzahl von " << _roundpoints << " erreicht.";
return s.str();
}
void Player::addNormal()
{
qDebug() << "hitting the method";
_roundpoints += POINTS_FOR_RARITY / NORMAL_RARITY;
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
}
void Player::addGoldenNormal()
{
qDebug() << "hitting the method";
_roundpoints += POINTS_FOR_RARITY / GOLDEN_NORMAL_RARITY;
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
}
void Player::addRare()
{
qDebug() << "hitting the method";
_roundpoints += POINTS_FOR_RARITY / RARE_RARITY;
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
}
void Player::addGoldenRare()
{
qDebug() << "hitting the method";
_roundpoints += POINTS_FOR_RARITY / GOLDEN_RARE_RARITY;
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
}
void Player::addEpic()
{
qDebug() << "hitting the method";
_roundpoints += POINTS_FOR_RARITY / EPIC_RARITY;
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
}
void Player::addGoldenEpic()
{
qDebug() << "hitting the method";
_roundpoints += POINTS_FOR_RARITY / GOLDEN_EPIC_RARITY;
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
}
void Player::addLegendary()
{
qDebug() << "hitting the method";
_roundpoints += POINTS_FOR_RARITY / LEGENDARY_RARITY;
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
}
void Player::addGoldenLegendary()
{
qDebug() << "hitting the method";
_roundpoints += POINTS_FOR_RARITY / GOLDEN_LEGENDARY_RARITY;
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
}
void Player::addRoundPoints()
{
qDebug() << "hitting the method";
if(_cardsPerRound == 4)
{
_points += _roundpoints;
resetRoundPoints();
_textEdit->setText(QString::fromStdString(returnRoundpoints()));
_pointDisplay->display(_points);
}
else
{
std::stringstream s;
s << "Sie haben für das Pack nur " << _cardsPerRound + 1 << "ausgewählt";
_textEdit->setText(QString::fromStdString(s.str()));
}
}
void Player::resetPoints()
{
qDebug() << "hitting the method";
_points = 0;
}
void Player::resetRoundPoints()
{
qDebug() << "hitting the method";
_roundpoints = 0;
_cardsPerRound = 0;
}
int Player::_instance = 0;
player.cpp
#include "mainwindow.h"
#include "player.h"
#include <QApplication>
#include <QObject>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
main.cpp
Upvotes: 0
Views: 134
Reputation: 11
Careful, you are creating the objects player one and player two inside MainWindow's constructor (in the stack). When the constructor end is reached, playerOne and playerTwo objects are destructed. Make playerOne and playerTwo members of MainWindow, so they continue existing after the constructor has ended, and everything will be fine.
Upvotes: 1