Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to update QLable setGeometry(x,y,width,height) x,y positions in QT based on mouse x,y coordinates, with existing QLable name only.



  • Hi Team, I've added QLabel dynamically. And when I move the mouse, I want to get that dynamically added QLabel name, and update its setGeometry() x,y positions with the mouse pointer x,y positions

    void MainWindow::boxClicked()
    {

    if(Cb_Mag->isChecked())
         {
    QLabel *my_label = new QLabel(this);
    QRect crop_rect(200, 500, 250, 250);
    QImage desk = qApp->screens().at(0)->grabWindow(
    QDesktopWidget().winId(),
    crop_rect.left(),
    crop_rect.top(),
    crop_rect.width(),
    crop_rect.height()).toImage();
    my_label->show();
    my_label->setPixmap(QPixmap::fromImage(desk));
    my_label->setStyleSheet("border:5px solid grey");
    my_label->setGeometry(200, 500, 250, 250); }
    

    }

    void MainWindow::mouseMoveEvent(QMouseEvent *mMoveEvent)
    {

    delete this->findChild<QLabel*>("my_label");
    QLabel* my_label = new QLabel(this);
    my_label->setObjectName("my_label");
    
    QRect crop_rect(mMoveEvent->pos().x(), mMoveEvent->pos().y(), 500, 250);
    QImage desk = qApp->screens().at(0)->grabWindow(
    QDesktopWidget().winId(),
    crop_rect.left(),
    crop_rect.top(),
    crop_rect.width(),
    crop_rect.height()).toImage();
    my_label->show();
    my_label->setPixmap(QPixmap::fromImage(desk));
    my_label->setStyleSheet("border:10px solid grey;border-radius: 16px;");
    my_label->setGeometry(mMoveEvent->pos().x(), mMoveEvent->pos().y(), 500, 250);
    

    }

    In the above code currently, I'm just removing the entire label and creating new one. Is it possible to get existed "my_label" in mouseMoveEvent() and update setGeometry() and setPixmap() things only instead of removing entire label and recreating label with new values. Thanks in advance.


  • Qt Champions 2019


Log in to reply