add rightbutton only can dray
This commit is contained in:
@@ -24,18 +24,29 @@ void frmMoveWidget::initForm()
|
|||||||
btn1->setText("按住我拖动(仅限左键拖动)");
|
btn1->setText("按住我拖动(仅限左键拖动)");
|
||||||
MoveWidget *moveWidget1 = new MoveWidget(this);
|
MoveWidget *moveWidget1 = new MoveWidget(this);
|
||||||
moveWidget1->setWidget(btn1);
|
moveWidget1->setWidget(btn1);
|
||||||
|
moveWidget1->setLeftButton(true);
|
||||||
|
moveWidget1->setRightButton(false);
|
||||||
|
|
||||||
QPushButton *btn2 = new QPushButton(this);
|
QPushButton *btn2 = new QPushButton(this);
|
||||||
btn2->setGeometry(10, 40, 250, 25);
|
btn2->setGeometry(10, 40, 250, 25);
|
||||||
btn2->setText("按住我拖动(支持右键拖动)");
|
btn2->setText("按住我拖动(仅限右键拖动)");
|
||||||
MoveWidget *moveWidget2 = new MoveWidget(this);
|
MoveWidget *moveWidget2 = new MoveWidget(this);
|
||||||
moveWidget2->setWidget(btn2);
|
moveWidget2->setWidget(btn2);
|
||||||
moveWidget2->setLeftButton(false);
|
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);
|
QProgressBar *bar = new QProgressBar(this);
|
||||||
bar->setGeometry(10, 70, 250, 25);
|
bar->setGeometry(10, 100, 250, 25);
|
||||||
bar->setRange(0, 0);
|
bar->setRange(0, 0);
|
||||||
bar->setTextVisible(false);
|
bar->setTextVisible(false);
|
||||||
MoveWidget *moveWidget3 = new MoveWidget(this);
|
MoveWidget *moveWidget4 = new MoveWidget(this);
|
||||||
moveWidget3->setWidget(bar);
|
moveWidget4->setWidget(bar);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ MoveWidget::MoveWidget(QObject *parent) : QObject(parent)
|
|||||||
{
|
{
|
||||||
lastPoint = QPoint(0, 0);
|
lastPoint = QPoint(0, 0);
|
||||||
pressed = false;
|
pressed = false;
|
||||||
leftButton = true;
|
leftButton = false;
|
||||||
|
rightButton = false;
|
||||||
inControl = true;
|
inControl = true;
|
||||||
widget = 0;
|
widget = 0;
|
||||||
}
|
}
|
||||||
@@ -16,8 +17,12 @@ bool MoveWidget::eventFilter(QObject *watched, QEvent *event)
|
|||||||
if (widget != 0 && watched == widget) {
|
if (widget != 0 && watched == widget) {
|
||||||
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
QMouseEvent *mouseEvent = (QMouseEvent *)event;
|
||||||
if (mouseEvent->type() == QEvent::MouseButtonPress) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,6 +73,11 @@ void MoveWidget::setLeftButton(bool leftButton)
|
|||||||
this->leftButton = leftButton;
|
this->leftButton = leftButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MoveWidget::setRightButton(bool rightButton)
|
||||||
|
{
|
||||||
|
this->rightButton = rightButton;
|
||||||
|
}
|
||||||
|
|
||||||
void MoveWidget::setInControl(bool inControl)
|
void MoveWidget::setInControl(bool inControl)
|
||||||
{
|
{
|
||||||
this->inControl = inControl;
|
this->inControl = inControl;
|
||||||
|
|||||||
@@ -34,12 +34,15 @@ private:
|
|||||||
QPoint lastPoint; //最后按下的坐标
|
QPoint lastPoint; //最后按下的坐标
|
||||||
bool pressed; //鼠标是否按下
|
bool pressed; //鼠标是否按下
|
||||||
bool leftButton; //限定鼠标左键
|
bool leftButton; //限定鼠标左键
|
||||||
|
bool rightButton; //限定鼠标右键
|
||||||
bool inControl; //限定在容器内
|
bool inControl; //限定在容器内
|
||||||
QWidget *widget; //移动的控件
|
QWidget *widget; //移动的控件
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
//设置是否限定鼠标左键
|
//设置是否限定鼠标左键
|
||||||
void setLeftButton(bool leftButton);
|
void setLeftButton(bool leftButton);
|
||||||
|
//设置是否限定鼠标右键
|
||||||
|
void setRightButton(bool rightButton);
|
||||||
//设置是否限定不能移出容器外面
|
//设置是否限定不能移出容器外面
|
||||||
void setInControl(bool inControl);
|
void setInControl(bool inControl);
|
||||||
//设置要移动的控件
|
//设置要移动的控件
|
||||||
|
|||||||
Reference in New Issue
Block a user