Раскрывающийся текст
#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
class MyButton : public QPushButton
{
Q_OBJECT
public:
MyButton(QWidget *parent = 0);
public slots:
void slotSetParent(bool checked);
private:
QWidget *parent;
};
MyButton::MyButton(QWidget *p)
: QPushButton(0)
{
parent = p;
setCheckable(true);
setText("Click");
connect(this, SIGNAL(clicked(bool)), this, SLOT(slotSetParent(bool)));
}
void MyButton::slotSetParent(bool checked)
{
if (checked)
{
setParent(parent);
parent->layout()->addWidget(this);
}
else
{
setParent(0);
show();
}
};
// нужнен после объявления класса, т.к. и объявление и реализация класса в одном файле.
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QVBoxLayout *lay = new QVBoxLayout();
QWidget wgt;
wgt.resize(150, 70);
wgt.setLayout(lay);
wgt.show();
MyButton *btn = new MyButton(&wgt);
btn->show();
return app.exec();
}