Search code examples
c++qtqwidget

hide QWidget from a different QWidget


I have a MainWindow with two widgets, buttonsWidget and infoWidget. I'm trying to to hide infoWidget after clicking a button within buttonsWidget (and ultimately show a different widget).

I've tried:

mainwindow.h

public:
void hideInfo();

mainwindow.cpp

void MainWindow::hideInfo()
{
ui->info->hide();
}

buttonsWidget.cpp

void buttonsWidget::on_timingButton_clicked()
{
MainWindow::hideInfo();

//Then will do something to show 'timingWidget'..

}

Many thanks


Solution

  • You should use Signals and Slots for this.

    Add a signal in the buttonsWidget.h.

    signals:
        void hideInfoSignal();
    

    In the main function, connect the button signal with the mainwindow method hideInfo().

    QObject::connect(this->info, SIGNAL(hideInfoSignal),this, SLOT(hideInfo));
    

    I haven't tested this, because I dont have Qt on this machine, but that should work, with possible minor modifications. If any errors appear, let me know and I will help. Also, read the signals and slots documentation.