在QtCreator中,添加Spacer控件只需要,将其从组件库中拖到窗口上就行了。
对象窗口中显示类型为Spacer,但是程序中无法找到Spacer这个类。
需要创建一个layout对象,然后执行代码 layout->addStretch(); 就可以了。
alarm::alarm(QWidget *parent) :QWidget(parent){QHBoxLayout *mHLayout = new QHBoxLayout();QLabel *mRedAlarmLabel = new QLabel();mRedAlarmLabel->setPixmap(QPixmap(":/res/res/alarm/redlight-on.png"));QLabel *mYellowAlarmLabel = new QLabel();mYellowAlarmLabel->setPixmap(QPixmap(":/res/res/alarm/yellowlight-on.png"));QLabel *mGreenAlarmLabel = new QLabel();mGreenAlarmLabel->setPixmap(QPixmap(":/res/res/alarm/greenlight-on.png"));
mHLayout->addWidget(mRedAlarmLabel);
mHLayout->addWidget(mYellowAlarmLabel);
mHLayout->addWidget(mGreenAlarmLabel);
mHLayout->addStretch();//添加Spacerparent->setLayout(mVLayout);
}
原文地址: http://blog.163.com/sdzhangyuyin@126/blog/static/13841374820150582348768/