QT 5-无法连接到其他类别的插槽使用

时间:2018-07-28 16:17:10

标签: c++ qt

这是我的第一个QT程序,我无法使其按预期工作。 我试图将按钮连接到Player类中的函数。 编译器没有显示任何错误,但是当我按下按钮时,除了MainWindow中的showPoints()函数外,程序没有达到这些功能。 我也知道showPoints()函数出了点问题,因为它没有隐藏lcdNumbers。 这是我的代码:

    #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

1 个答案:

答案 0 :(得分:1)

小心,您正在MainWindow的构造函数中(在堆栈中)创建对象播放器1和播放器2。到达构造函数末尾时,将销毁playerOne和playerTwo对象。使MainWindow的playerOne和playerTwo成为成员,以便在构造函数结束后它们继续存在,一切都会好起来。