add rightbutton only can dray

This commit is contained in:
chenshijie
2020-11-02 19:07:49 +08:00
parent 85a577fe90
commit 7b9079d279
3 changed files with 32 additions and 8 deletions

View File

@@ -24,18 +24,29 @@ void frmMoveWidget::initForm()
btn1->setText("按住我拖动(仅限左键拖动)");
MoveWidget *moveWidget1 = new MoveWidget(this);
moveWidget1->setWidget(btn1);
moveWidget1->setLeftButton(true);
moveWidget1->setRightButton(false);
QPushButton *btn2 = new QPushButton(this);
btn2->setGeometry(10, 40, 250, 25);
btn2->setText("按住我拖动(支持右键拖动)");
btn2->setText("按住我拖动(仅限右键拖动)");
MoveWidget *moveWidget2 = new MoveWidget(this);
moveWidget2->setWidget(btn2);
moveWidget2->setLeftButton(false);
moveWidget2->setRightButton(true);
QPushButton *btn3 = new QPushButton(this);
btn3->setGeometry(10, 70, 250, 25);
btn3->setText("按住我拖动(支持左右键拖动)");
MoveWidget *moveWidget3 = new MoveWidget(this);
moveWidget3->setWidget(btn3);
moveWidget3->setLeftButton(true);
moveWidget3->setRightButton(true);
QProgressBar *bar = new QProgressBar(this);
bar->setGeometry(10, 70, 250, 25);
bar->setGeometry(10, 100, 250, 25);
bar->setRange(0, 0);
bar->setTextVisible(false);
MoveWidget *moveWidget3 = new MoveWidget(this);
moveWidget3->setWidget(bar);
MoveWidget *moveWidget4 = new MoveWidget(this);
moveWidget4->setWidget(bar);
}

View File

@@ -6,7 +6,8 @@ MoveWidget::MoveWidget(QObject *parent) : QObject(parent)
{
lastPoint = QPoint(0, 0);
pressed = false;
leftButton = true;
leftButton = false;
rightButton = false;
inControl = true;
widget = 0;
}
@@ -16,8 +17,12 @@ bool MoveWidget::eventFilter(QObject *watched, QEvent *event)
if (widget != 0 && watched == widget) {
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if (mouseEvent->type() == QEvent::MouseButtonPress) {
//如果限定了只能鼠标左键拖动则判断当前是否是鼠标左键
if (leftButton && mouseEvent->button() != Qt::LeftButton) {
//如果鼠标左键和鼠标右键都可以拖动
if (leftButton && rightButton) {
}
//如果限定了只能鼠标左键拖动则判断当前是否是鼠标左键,如果限定了只能鼠标右键拖动则判断当前是否是鼠标右键
else if ((leftButton && mouseEvent->button() != Qt::LeftButton) || (rightButton && mouseEvent->button() != Qt::RightButton)) {
return false;
}
@@ -68,6 +73,11 @@ void MoveWidget::setLeftButton(bool leftButton)
this->leftButton = leftButton;
}
void MoveWidget::setRightButton(bool rightButton)
{
this->rightButton = rightButton;
}
void MoveWidget::setInControl(bool inControl)
{
this->inControl = inControl;

View File

@@ -34,12 +34,15 @@ private:
QPoint lastPoint; //最后按下的坐标
bool pressed; //鼠标是否按下
bool leftButton; //限定鼠标左键
bool rightButton; //限定鼠标右键
bool inControl; //限定在容器内
QWidget *widget; //移动的控件
public Q_SLOTS:
//设置是否限定鼠标左键
void setLeftButton(bool leftButton);
//设置是否限定鼠标右键
void setRightButton(bool rightButton);
//设置是否限定不能移出容器外面
void setInControl(bool inControl);
//设置要移动的控件