Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. 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.
Forum Updated to NodeBB v4.3 + New Features

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.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 274 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NageswaRao
    wrote on last edited by NageswaRao
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • N NageswaRao

      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.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @NageswaRao Take a look at https://doc.qt.io/qt-5/qtwidgets-draganddrop-draggableicons-example.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved