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. clicked() event for QLabel not working in Expected way.
QtWS25 Last Chance

clicked() event for QLabel not working in Expected way.

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.6k 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.
  • A Offline
    A Offline
    azravian
    wrote on last edited by azravian
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      That was really a strange one as code looked so correct. :)
      Then it hit me.
      They overlap.
      So "3" is on top so it always say 3

      try this and you'll see that you class worked flawlessly but
      using setGeometry with 0,0 for all would make
      them all start there and hence overlap.
      m_label->setGeometry(0,0,400,40);
      m_label1->setGeometry(0,0,400,80);
      m_label2->setGeometry(0,0,400,120);
      :)
      And dont feel to bad about it. I had to run it and go hmm that is odd before
      i went ahhhhhhh. :)

           myLabel* m_label = new myLabel("MyLabel",this);
           m_label->setText("Click Me :)");
           m_label->setID(1);
      
           myLabel* m_label1 = new myLabel("MyLabel",this);
           m_label1->setText("No, Click Me :)");
           m_label1->move(0,m_label->height());
           m_label1->setID(2);
      
           myLabel* m_label2 = new myLabel("MyLabel",this);
           m_label2->setText("No, Click Me :)");
           m_label2->move(0,m_label1->height()+m_label->height());
           m_label2->setID(3);
      
      A 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        That was really a strange one as code looked so correct. :)
        Then it hit me.
        They overlap.
        So "3" is on top so it always say 3

        try this and you'll see that you class worked flawlessly but
        using setGeometry with 0,0 for all would make
        them all start there and hence overlap.
        m_label->setGeometry(0,0,400,40);
        m_label1->setGeometry(0,0,400,80);
        m_label2->setGeometry(0,0,400,120);
        :)
        And dont feel to bad about it. I had to run it and go hmm that is odd before
        i went ahhhhhhh. :)

             myLabel* m_label = new myLabel("MyLabel",this);
             m_label->setText("Click Me :)");
             m_label->setID(1);
        
             myLabel* m_label1 = new myLabel("MyLabel",this);
             m_label1->setText("No, Click Me :)");
             m_label1->move(0,m_label->height());
             m_label1->setID(2);
        
             myLabel* m_label2 = new myLabel("MyLabel",this);
             m_label2->setText("No, Click Me :)");
             m_label2->move(0,m_label1->height()+m_label->height());
             m_label2->setID(3);
        
        A Offline
        A Offline
        azravian
        wrote on last edited by
        #3

        @mrjj Thank You sir, it worked and surely that was a disastrous mistake :)
        and can I ask for the help about the second problem?

        mrjjM 1 Reply Last reply
        0
        • A azravian

          @mrjj Thank You sir, it worked and surely that was a disastrous mistake :)
          and can I ask for the help about the second problem?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @azravian
          yeah it was a sneak one.

          Its a user forum.U can ask as much as you like:)

          If very different from this, a new post might be best.
          If related, just ask away.

          Oh sorry. i missed second question.
          Im not sure what question is.
          The code does add labels to the mainwindow?

          Doing it from slot when clicked button should not be different.
          Did u check that when clicking it did actually call your slot?
          also as u see in the code. make sure to use "this" for parent
          myLabel* m_label = new myLabel("MyLabel",this);<<< this = mainwindow as parent.

          A 1 Reply Last reply
          0
          • mrjjM mrjj

            @azravian
            yeah it was a sneak one.

            Its a user forum.U can ask as much as you like:)

            If very different from this, a new post might be best.
            If related, just ask away.

            Oh sorry. i missed second question.
            Im not sure what question is.
            The code does add labels to the mainwindow?

            Doing it from slot when clicked button should not be different.
            Did u check that when clicking it did actually call your slot?
            also as u see in the code. make sure to use "this" for parent
            myLabel* m_label = new myLabel("MyLabel",this);<<< this = mainwindow as parent.

            A Offline
            A Offline
            azravian
            wrote on last edited by
            #5

            @mrjj Sir, I have mentioned my 2nd problem in the very first post. I need to create a label in MainWindow when a pushButton is pressed. I can create a label in Layout but i need it in mainwindow. I know that i need to set MainWindow as the parent of the label, which i can't.

            myLabel* m_label4 = new myLabel("MyLabel");
            //m_label4->setParent(this);
            m_label4->setGeometry(0,0,400,40);
            m_label4->setText("Click Me :)");
            m_label4->setID(4);
            

            obviously, setParent(here) won't work. but can't set mainwindow as parent.

            mrjjM kshegunovK 2 Replies Last reply
            0
            • A azravian

              @mrjj Sir, I have mentioned my 2nd problem in the very first post. I need to create a label in MainWindow when a pushButton is pressed. I can create a label in Layout but i need it in mainwindow. I know that i need to set MainWindow as the parent of the label, which i can't.

              myLabel* m_label4 = new myLabel("MyLabel");
              //m_label4->setParent(this);
              m_label4->setGeometry(0,0,400,40);
              m_label4->setText("Click Me :)");
              m_label4->setID(4);
              

              obviously, setParent(here) won't work. but can't set mainwindow as parent.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @azravian said:

              myLabel* m_label4 = new myLabel("MyLabel");

              hi, try with "this"

              myLabel* m_label4 = new myLabel("MyLabel", this);

              sorry i missed it in first post :)

              A 1 Reply Last reply
              0
              • mrjjM mrjj

                @azravian said:

                myLabel* m_label4 = new myLabel("MyLabel");

                hi, try with "this"

                myLabel* m_label4 = new myLabel("MyLabel", this);

                sorry i missed it in first post :)

                A Offline
                A Offline
                azravian
                wrote on last edited by
                #7

                @mrjj That's not a problem sir
                and yes slot is called properly,
                and after trying

                myLabel* m_label = new myLabel("MyLabel",this);
                

                The response is nothing.

                1 Reply Last reply
                0
                • A azravian

                  @mrjj Sir, I have mentioned my 2nd problem in the very first post. I need to create a label in MainWindow when a pushButton is pressed. I can create a label in Layout but i need it in mainwindow. I know that i need to set MainWindow as the parent of the label, which i can't.

                  myLabel* m_label4 = new myLabel("MyLabel");
                  //m_label4->setParent(this);
                  m_label4->setGeometry(0,0,400,40);
                  m_label4->setText("Click Me :)");
                  m_label4->setID(4);
                  

                  obviously, setParent(here) won't work. but can't set mainwindow as parent.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by kshegunov
                  #8

                  @azravian

                  I need to create a label in MainWindow when a pushButton is pressed.

                  When created, you should put the label somewhere, right? If you mean to add it to the main window, then maybe you want to add it to the central widget's layout (assuming it has one)? Try something like this:

                  QWidget * central = centralWidget();
                  
                  myLabel * label = new myLabel("MyLabel", central);
                  label->setGeometry(0,0,400,40);
                  label->setText("Click Me :)");
                  label->setID(4);
                  
                  central->layout()->addWidget(label);
                  

                  Although, do make sure that the central widget has a layout, otherwise this'd segfault because QWidget::layout is going to return NULL.

                  Read and abide by the Qt Code of Conduct

                  A 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @azravian

                    I need to create a label in MainWindow when a pushButton is pressed.

                    When created, you should put the label somewhere, right? If you mean to add it to the main window, then maybe you want to add it to the central widget's layout (assuming it has one)? Try something like this:

                    QWidget * central = centralWidget();
                    
                    myLabel * label = new myLabel("MyLabel", central);
                    label->setGeometry(0,0,400,40);
                    label->setText("Click Me :)");
                    label->setID(4);
                    
                    central->layout()->addWidget(label);
                    

                    Although, do make sure that the central widget has a layout, otherwise this'd segfault because QWidget::layout is going to return NULL.

                    A Offline
                    A Offline
                    azravian
                    wrote on last edited by azravian
                    #9

                    @kshegunov Sir
                    I can place my Label into my layout.

                        QHBoxLayout *layout = new QHBoxLayout;
                        myLabel* m_label = new myLabel("MyLabel");
                        m_label->setText("The One created Now.");
                        m_label->setID(4);
                        m_label->setStyleSheet("QLabel { background-color : red; color : blue; }");
                        layout->addWidget(m_label);
                        QWidget *window = new QWidget();
                        window->setLayout(layout);
                        setCentralWidget(window);
                    

                    Now this add label to the layout in the main window but I want to add this label directly to main window.
                    Another problem with this it covers all my MainWindow. I have tried QLayout::setGeometry(), myLabel::setGeometry() and QWidget::Resize() but it all did nothing

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

                      Hi,

                      Directly to main window ? What do you mean by that ? Should it cover the main window ? If so, what for ?

                      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
                      1

                      • Login

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