PDA

View Full Version : Hiding/Showing a layout by pressing a button.



kielbas99
25th January 2015, 19:58
Hey guys, I've got a question about the grid layout. I've placed there some labels, edit lines and a combo box. I want to hide/show the layout with all that stuff by pressing a button. It should adjust the whole main window according to that hidden/showed layout. Can someone give me an advice how to do it? Thanks.

ChrisW67
25th January 2015, 22:01
Take a look at the Extension Example (http://doc-snapshot.qt-project.org/qt5-5.4/qtwidgets-dialogs-extension-example.html) for something along these lines.

kielbas99
26th January 2015, 00:11
Ok, I worked out how to hide all that stuff by pressing one button, but I've got another problem. I don't know how to show them again by pressing the same button. I don't have a clue even after studying that extension example.
@EDIT:
My way to do it it's not very effective. I mean look at the code. It should hide and show after pressing the "Inne" button. It shouldn't be "setCheckable(true)." I think the signals are wrong too. Also I should use show() but I don't know how to connect both, hide() and show().


void Kalkulator::on_Inne_clicked()
{
przycisk= ui->Inne;
przycisk->setCheckable(true);

pierwszy= ui->lineEdit_7;
drugi=ui->lineEdit_8;
trzeci=ui->lineEdit_9;
czwarty=ui->lineEdit_10;
piaty=ui->lineEdit_11;
wybor=ui->comboBox;
jeden=ui->label_10;
dwa=ui->label_11;
trzy=ui->label_12;
cztery=ui->label_13;
piec=ui->label_14;

connect(przycisk,SIGNAL(toggled(bool)), pierwszy, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), drugi, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), trzeci, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), czwarty, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), piaty, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), wybor, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), jeden, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), dwa, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), trzy, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), cztery, SLOT(setVisible(bool)));
connect(przycisk,SIGNAL(toggled(bool)), piec, SLOT(setVisible(bool)));

pierwszy->hide();
drugi->hide();
trzeci->hide();
czwarty->hide();
piaty->hide();
wybor->hide();
jeden->hide();
dwa->hide();
trzy->hide();
cztery->hide();
piec->hide();

}

wysota
26th January 2015, 06:58
All the buttons you want to hide should be put into a dedicated widget and you should be hiding/showing that single widget. Or maybe even you should use QStackedWidget to divide your widgets into pages exactly one of which is visible at a time.

ChrisW67
26th January 2015, 08:05
Not sure how I managed to write this and then not post it a few hours back....

Here is one approach:


class Kalkulator {
...
private:
void updateUi();
bool m_visible;
}





Kalkulator::Kalkulator():
m_visible(true) // <<< the initial visibility
{
...
updateUi(); // <<< make the UI match the visibility
}

Kalkulator::updateUi()
{
pierwszy->setVisible(m_visible);
drugi->setVisible(m_visible);
...
}

void on_Inne_Clicked()
{
m_visible = !m_visible; // <<< toggle visibility
updateUi(); // <<< make the UI match the visibility
}


Or even cleaner, put all the hideable widgets inside a single QWidget and simply show or hide that widget. (That's what the example does and wysota suggests)

kielbas99
26th January 2015, 15:05
Thanks. Without you I would be still thinking about a way out. Thanks again for help.