Important: Please read the Qt Code of Conduct -

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

  • This post is deleted!

  • Lifetime Qt Champion

    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.
    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 :)");
         myLabel* m_label1 = new myLabel("MyLabel",this);
         m_label1->setText("No, Click Me :)");
         myLabel* m_label2 = new myLabel("MyLabel",this);
         m_label2->setText("No, Click Me :)");

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

  • Lifetime Qt Champion

    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.

  • @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->setText("Click Me :)");

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

  • Lifetime Qt Champion

    @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 :)

  • @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.

  • Moderators


    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->setText("Click Me :)");

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

  • @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->setStyleSheet("QLabel { background-color : red; color : blue; }");
        QWidget *window = new QWidget();

    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

  • Lifetime Qt Champion


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

Log in to reply