还是先看看运行效果:
这次我们再Rep文件中使用枚举
class CommonInterface
{
ENUM CarOperation {
Accelerate,
Decelerate,
TurnLeft,
TurnRight
}
SIGNAL(sigMessage(CarOperation operation)) //server下发消息给client
}
如果你用的Qt版本是5.9的话,这里可能会有问题,我用的是Qt5…14.1,这样写没有问题。
Qt5.9的朋友遇到问题不要紧张,看这里:
这次我们直接放代码:
1.客户端(小车)
car.h
#ifndef CAR_H
#define CAR_H
#include <QGraphicsObject>
#include <QBrush>
#include <QRemoteObjectNode>
#include "rep_CommonInterface_replica.h"
class Car : public QGraphicsObject
{
Q_OBJECT
public:
Car();
QRectF boundingRect() const;
public Q_SLOTS:
void accelerate();
void decelerate();
void turnLeft();
void turnRight();
void onSigMessage(CommonInterfaceReplica::CarOperation operation);
Q_SIGNALS:
void crashed();
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
void timerEvent(QTimerEvent *event);
private:
void init();
private:
QBrush color;
qreal wheelsAngle; // used when applying rotation
qreal speed; // delta movement along the body axis
QRemoteObjectNode * m_pRemoteNode = nullptr;
std::shared_ptr<CommonInterfaceReplica> m_pInterface = nullptr;
};
#endif // CAR_H
car.cpp
#include "car.h"
#include <QtWidgets/QtWidgets>
#include <qmath.h>
QRectF Car::boundingRect() const
{
return QRectF(-35, -81, 70, 115);
}
Car::Car() : color(Qt::green), wheelsAngle(0), speed(0)
{
startTimer(1000 / 33);
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsFocusable, true);
init();
}
void Car::accelerate()
{
if (speed < 10)
++speed;
}
void Car::decelerate()
{
if (speed > -10)
--speed;
}
void Car::turnLeft()
{
<