考虑到布局是在 QWidget
中设置的,代码如下:
setLayout(new QVBoxLayout);
然后需要检索它(向布局添加更多内容)。这是通过以下代码完成的:
QHBoxLayout *hLayoutTime(new QHBoxLayout);
qobject_cast<QVBoxLayout*>(layout())->addLayout(hLayoutTime);
qobject_cast
是适合在这里使用的类型转换吗?
最佳答案
为了避免不必要的转换,这样写:
void YourWidget::setupContents()
{
QVBoxLayout *vLayout = new QVBoxLayout(this); // effectively this does setLayout(new QVBoxLayout);
QHBoxLayout *hLayoutTime(new QHBoxLayout);
vLayout->addLayout(hLayoutTime);
… … …
}
关于c++ - qobject_cast<QVBoxLayout*>(layout()),是否合适?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36503368/