Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Hiding a QPushButton, which is in main function



  • Hello everyone!
    I have a function which hide a QTableWidget. I want to hide QPushButtons also.

    void Kobi::FullScreen ()
    {
        tableWidget->hide ();
       
    }
    

    This code is in the use_form.cpp file.

    QPushButton insertR;
        insertR.setText(QStringLiteral("Zeile einfügen"));
    

    This code is written in the main file.

    I want to hide this Button (insertR) in the code which is written in the use_form.cpp but I don't know how to do that?

    Thanks a lot,
    Henrik


  • Moderators

    @HenrikSt.
    here is a rather ugly approach:

    //in main()
    QPushButton insertR;
        insertR.setObjectName("MyButton");
        insertR.setText(QStringLiteral("Zeile einfügen"));
    ...
    // seach
    foreach( QWidget* widget, QApplication::topLevelWidgets() )
    {
          if( QPushButton* button = widget->findChild<QPushButton*>("MyButton") )
          {
                button-> .... ;
                break;
          }
    }
    

    Better (efficient) solution would be to pass the pointer of the button to all objects who need to access it.
    Or implement a custom signal/slot connection, etc.



  • Hi,
    thanks for your answer. That would be possible.

    Do you have an example for the pointer or e.g. a custom signal/slot connection?

    That would be really nice. Thank you,
    Henrik


  • Moderators

    @HenrikSt.
    in your Kobi class, add a setter method:

    class Kobi : public ... 
    {
    public:
    ...
        void setButtonWidget( QPushButton* button )
        {
            m_PushButton = button;
        }
    
       void Kobi::FullScreen ()
       {
           tableWidget->hide ();
           // OR EMIT A SIGNAL HERE AND CONNECT IT TO THE PUSHBUTTON'S hide() SLOT
           if( m_PushButton )
               m_PushButton->hide();
       }
    
    private:
         QPointer<QPushButton*> m_PushButton;
    }
    

    Easiest would be when you could already create the button in this class?