#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QLayout>
#include <QPushButton>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
private slots:
void showLabels();
};
#endif // MAINWINDOW_H
#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
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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(showLabels())); //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();
}
#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(showLabels())); //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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks