该示例演示了一个"扩张的窗口",主窗口的布局为水平布局,内置两个子窗口,采用定时器设置左边窗口的宽度,达到控制"扩张"的目的。
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QTimer>
class MyWidget : public QWidget
{
//Q_OBJECT
public:
MyWidget(QWidget* parent = nullptr) : QWidget(parent)
{
setFixedSize(600, 400);
hLayout = new QHBoxLayout(this);
widget1 = new QWidget(this);
widget1->setFixedWidth(20);
widget1->setStyleSheet("background-color:red;");
widget2 = new QWidget(this);
hLayout->addWidget(widget1);
hLayout->addWidget(widget2, 1);
widget2->setStyleSheet("background-color:green;");
m_timer = new QTimer(this);
m_timer->start();
m_timer->setInterval(100);
QObject::connect(m_timer, &QTimer::timeout, [=]() {
int nW = widget1->width() + 1;
if (nW >= 100)
return;
widget1->setFixedWidth(nW);
});
}
private:
QWidget* widget1;
QWidget* widget2;
QHBoxLayout *hLayout;
QTimer* m_timer;
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MyWidget w;
w.show();
return app.exec();
}