简体   繁体   English

如何通过拖动鼠标 QT 来检查按钮? 鼠标移动事件

[英]How to check pushButtons with draging the mouse QT ? MouseMoveEvent

I have 5 pushButtons ( pushButton_i ) i = 1, 2, 3, 4, 5. What I want to do is to drag the mouse ( button pressed ) and then setText of checked buttons on "Yes", otherwise on "NO".我有 5 个按钮 (pushButton_i) i = 1, 2, 3, 4, 5。我想要做的是拖动鼠标(按下按钮),然后在“是”上设置选中按钮的 setText,否则在“否”上。 I tried the following code but the result is : When I press the mouse Button and then move it a little bit, all buttons' texts are set on "NO".我尝试了以下代码,但结果是:当我按下鼠标按钮然后稍微移动它时,所有按钮的文本都设置为“否”。 This is my code :这是我的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include<QEvent>
#include <QMouseEvent>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    QString key;
    for (int i=1;i<=5;i++){
        key = QString("pushButton_%1").arg(i);
        QPushButton *button = ui->centralwidget->findChild<QPushButton*>(key);
        QRect widgetRect = button->geometry();
        widgetRect.moveTopLeft(button->parentWidget()->mapToGlobal(widgetRect.topLeft()));
        if (button->rect().contains(event->pos())) button->setText("Yes");
        else button->setText("No");
}

}


Can someone please explain to me what is going on ?有人可以向我解释发生了什么吗?

From the documentation:从文档:

QMouseEvent::pos() reports the position of the mouse cursor, relative to this widget QMouseEvent::pos()报告鼠标光标的位置,相对于这个小部件

However, you change the position of the button by making it at point (0, 0).但是,您可以通过将其置于 (0, 0) 点来更改按钮的位置。 I don't know why you relocate the button.我不知道你为什么要重新定位按钮。

Try it with this:试试这个:

   void ButtonDrag::mouseMoveEvent(QMouseEvent *event)
    {
      QString key;
      for (int i = 1; i <= 5; i++)
      {
        key = QString("pushButton_%1").arg(i);
        QPushButton *button = ui.centralWidget->findChild<QPushButton*>(key);  
        button->setText("No");
      }

      QWidget* child = ui.centralWidget->childAt(event->pos());
      QPushButton* affectedBtn = dynamic_cast<QPushButton*>(child);
      if (affectedBtn)
        affectedBtn->setText("Yes");
    }

Problem is, that i don't know how to do it without a downcast.问题是,我不知道如何在没有沮丧的情况下做到这一点。 But at least it works xD.但至少它有效 xD。 I set the text of all buttons to "No" and get the affectedBtn (if there's one) by ui.centralWidget->childAt(event->pos()), which i have to downcast to use the setText method我将所有按钮的文本设置为“否”并通过 ui.centralWidget->childAt(event->pos()) 获取受影响的Btn(如果有),我必须向下转换才能使用 setText 方法

void MainWindow::mouseMoveEvent(QMouseEvent *event)
   {
     QString key;
     for (int i = 1; i <= 5; i++)
     {
       key = QString("pushButton_%1").arg(i);
       QPushButton *button = ui->centralwidget->findChild<QPushButton*>(key);
       button->setCheckable(true);

     }

     QWidget* child = ui->centralwidget->childAt(event->pos());
     QPushButton* affectedBtn = dynamic_cast<QPushButton*>(child);

     if (affectedBtn)
       affectedBtn->setChecked(true);
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM