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". 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 ?
Aucun commentaire:
Enregistrer un commentaire