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



  • This post is deleted!

  • Qt Champions 2016

    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);
    


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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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


  • Qt Champions 2016

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



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


  • Lifetime Qt Champion

    Hi,

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.