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 remove QLable which is added from example.cpp, not from example.ui of QT application.

How to remove QLable which is added from example.cpp, not from example.ui of QT application.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 943 Views 2 Watching
  • 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 have added QLabel like below, in the example.cpp.

      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);
      }
     else
     {
        if (this->findChild<QLabel*>("my_label")) {
             // Here I want to remove the  my_label. 
           }
      }
    }
    

    In else block, I want to remove the code label called 'my_label'. any idea, please. Thank you.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      The easiest would be to save the label pointer as a class member variable. This way you'd have instant access to it and could simply call delete on it.
      If you don't want to keep the variable around you can use findChild like you did, but you have to name the label first where you created it i.e.

      QLabel* my_label = new QLabel(this);
      my_label->setObjectName("my_label");
      ...
      

      and then you can delete it

      delete this->findChild<QLabel*>("my_label");
      
      
      1 Reply Last reply
      4
      • N Offline
        N Offline
        NageswaRao
        wrote on last edited by NageswaRao
        #3

        @chris I did as you suggested above 3 lines, still the label is not deleting.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          NageswaRao
          wrote on last edited by
          #4

          @Chris-Kawa any other suggestion

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            This should work. You must've made a mistake somewhere. Can you show the code you wrote?

            Btw. unrelated to your question, but you do toImage() and in the next line QPixmap::fromImage(). It would be better to replace the QImage with QPixmap and avoid the extra conversions. They are pretty expensive.

            1 Reply Last reply
            4
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              @NageswaRao said in How to remove QLable which is added from example.cpp, not from example.ui of QT application.:

              QLabel *my_label = new QLabel(this);

              If you added a member variable with the same name and kept this line as is, then it's normal it's not working because you are shadowing the member variable.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • N Offline
                N Offline
                NageswaRao
                wrote on last edited by NageswaRao
                #7

                Thank you, @Chris Kawa and @SGaist . It's working, I did a mistake when I'm placing the delete related code.

                delete this->findChild<QLabel*>("my_label");

                I kept this in the wrong place, so it's not executing.

                1 Reply Last reply
                0

                • Login

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