Qt编译不通过,已经包含了头文件的,也定义了,提示QpushButton未定义或者定义不全,请求帮忙看看什么问题?
之前使用了painter自绘制按钮进行拖拽移动。
在这里,MoveButton继承QPushbutton,实现鼠标移动拖拽,键盘上下键位移。
头文件MoveButton.h
#ifndef MOVEBUTTON_H
#define MOVEBUTTON_H
/***********************************************************
*版权声明:Copyright (c) 2019 Kongdemin. All rights reserved.
*许可版本:Corporation & Enterprise
*文件名称:MoveButton.h
*文件功能:can move button
*创建作者:kongdemin
*修改时间:2019-08
*修订说明:版本 01
************************************************************/
#include <QPushButton>
#include <QWidget>
class MoveButton : public QPushButton
{
Q_OBJECT
public:
explicit MoveButton(QWidget *parent = nullptr);
signals:
public slots:
protected:
///
/// \brief mousePressEvent
/// \param event
///
void mousePressEvent(QMouseEvent *event);
///
/// \brief mouseMoveEvent
/// \param event
///
void mouseMoveEvent(QMouseEvent *event);
///
/// \brief keyPressEvent
/// \param event
///
void keyPressEvent(QKeyEvent *event);
private:
///
/// \brief _beginPos
///
QPoint _beginPos;
///
/// \brief _lastPos
///
QPoint _lastPos;
};
#endif // MOVEBUTTON_H
源文件MoveButton.cpp
#include "MoveButton.h"
#include <QMouseEvent>
MoveButton::MoveButton(QWidget *parent) : QPushButton(parent)
{
this->setStyleSheet("background-color: rgb(218,165,32); border: 1px");
}
void MoveButton::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
_beginPos = event->pos();
}
}
void MoveButton::mouseMoveEvent(QMouseEvent *event)
{
_lastPos = event->pos();
int t_xDistance = _lastPos.x() - _beginPos.x();
int t_yDistance = _lastPos.y() - _beginPos.y();
if((this->x()+t_xDistance<=parentWidget()->width()-this->width())
&& (this->y()+t_yDistance<=parentWidget()->height()-this->height())
&& this->x()+t_xDistance>=0
&& this->y()+t_yDistance>=0)
{
this->move(this->x()+t_xDistance,this->y()+t_yDistance);
}
}
void MoveButton::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Left)
{
if(this->x()-4>=0)
{
this->move(this->x()-4,this->y());
}
}
else if(event->key() == Qt::Key_Right)
{
if(parentWidget()->width()-this->width()-this->x()-4>=0)
{
this->move(this->x()+4,this->y());
}
}
else if(event->key() == Qt::Key_Up)
{
if(this->y()-4>=0)
{
this->move(this->x(),this->y()-4);
}
}
else if(event->key() == Qt::Key_Down)
{
if(parentWidget()->height()-this->height()-this->y()-4>=0)
{
this->move(this->x(),this->y()+4);
}
}
}
效果: