Answer the question
In order to leave comments, you need to log in
Error LNK2019: Qt C++. How to eliminate?
Hello dear C++ programmers. I am programming a game in Qt. I was about to do player control, but there was an error: LNK2019. Here is the error itself: player.obj:-1: error: LNK2019: " __imp_GetAsyncKeyState "public: void __cdecl Player::moving(void)" ([email protected]@@QEAAXXZ). I tried to find a solution, they say that you need to restart qmake, but it does not help. Here is the game code:
Player.h:
#pragma once
#include <QObject>
#include <QGraphicsItem>
#include <QTimer>
#include <QPixmap>
#include <QPainter>
#include <QLabel>
class Player: public QObject, public QGraphicsItem
{
Q_OBJECT
public:
Player(QObject *parent = 0);
int pWidth = 110;
int pHeigth = 110;
int x = 205;
int y = 400;
public slots:
void moving();
private:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) override;
QRectF boundingRect() const override;
QPixmap *spriteImage; // В данный объект pixmap будет помещен спрайт
};
#pragma once
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QObject>
#include "player.h"
class View :public QGraphicsView
{
public:
View();
void view_elements();
void gameStarted();
private:
QGraphicsScene mainScene;
Player pl;
QTimer *timer; // Таймер
};
#include "view.h"
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
View v;
v.show();
return a.exec();
}
#include "view.h"
#include "player.h"
#include <QObject>
#include <QTimer>
View::View()
{
setWindowTitle("Space Rangers");
setRenderHint(QPainter::Antialiasing);
setFrameStyle(0);
setSceneRect(0, 0, 500,600);
setFixedSize(500, 600);
mainScene.setSceneRect(0, 0, 500,600);
mainScene.setItemIndexMethod(QGraphicsScene::NoIndex);
setScene(&mainScene);
timer = new QTimer();
connect(timer, &QTimer::timeout, &pl, &Player::moving);
view_elements();
}
void View::view_elements(){
pl.setPos(205, 400);
mainScene.addItem(&pl);
}
#include "player.h"
#include <QtWidgets>
using namespace Qt;
Player::Player(QObject *parent):QObject(parent), QGraphicsItem()
{
spriteImage = new QPixmap();
spriteImage->load(":/Player.png");
}
QRectF Player::boundingRect() const{
return QRectF(0, 0, pWidth, pHeigth);
}
void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget){
painter->drawPixmap(0, 0, pWidth, pHeigth, *spriteImage);
Q_UNUSED(option);
Q_UNUSED(widget);
}
void Player::moving()
{
//*******************Обработка нажатий клавиш на клавиатуре*********************************************
if(GetAsyncKeyState(VK_LEFT)){
setPos(mapToParent(-4, 0));
}
if(GetAsyncKeyState(VK_RIGHT)){
setPos(mapToParent(4, 0));
}
if(GetAsyncKeyState(VK_UP)){
setPos(mapToParent(0, 5));
}
if(GetAsyncKeyState(VK_DOWN)){
setPos(mapToParent(0, -4));
}
}
I will be very grateful for the answer.
Answer the question
In order to leave comments, you need to log in
You need to add a link with User32.lib in the *.pro project file
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question