PDA

View Full Version : hiding textbox and other widgets when loading a form



prabhudev
1st June 2012, 10:41
In my project i have a form, when i load it i want to hide textboxes and labels in it. When i click on a button then it should be visible...How to do it...?thank you

StrikeByte
1st June 2012, 11:02
The easyest way is to place the items in a widget, then hide the widget at the creation of the form.
connect a self made slot to the clicked signal of the button and show the widget

here is an example: 7781

prabhudev
1st June 2012, 11:10
@StrikeByte
Can you please provide using some snippet. How to place items in a widget and how hide while creation?
Os-> centos6
Qt 4.7
tool->Qcreator

StrikeByte
1st June 2012, 12:49
ok some code

the .h file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QLayout>
#include <QPushButton>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
QLabel *label1;
QLabel *label2;
QLabel *label3;
QWidget *container;
QGridLayout *layout;
QVBoxLayout *containerLayout;
QPushButton *btnShow;
QWidget *centralWidget;

private slots:
void showLabels();
};

#endif // MAINWINDOW_H


the .cpp file

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
resize(800,600);
centralWidget = new QWidget(this); //create the central widget

layout = new QGridLayout(centralWidget); //create mainwindow layout
layout->setSpacing(6);
layout->setContentsMargins(11, 11, 11, 11);

btnShow = new QPushButton("show/hide",centralWidget); //create the button
layout->addWidget(btnShow, 0, 0, 1, 1); //add button to layout
connect(btnShow,SIGNAL(clicked()),this,SLOT(showLa bels())); //connect the signal

container = new QWidget(centralWidget); //create the container
layout->addWidget(container, 1, 0, 1, 1);

containerLayout = new QVBoxLayout(container); //create a layout for the container

label1 = new QLabel("test 1",container); //create the labels
containerLayout->addWidget(label1); //add the labels to the layout
label2 = new QLabel("test 2",container);
containerLayout->addWidget(label2);
label3 = new QLabel("test 3",container);
containerLayout->addWidget(label3);

container->hide(); //hide the container

setCentralWidget(centralWidget); //set the central widget for the mainwindow
}

MainWindow::~MainWindow()
{

}

void MainWindow::showLabels()
{
if (container->isVisible())
container->hide();
else
container->show();
}



PS:
it is always nice to have the desgner and see what code it generates

prabhudev
1st June 2012, 14:24
@StrikeByte
Thanks A Lot