Considering the layout was set in a QWidget
with the following code:
setLayout(new QVBoxLayout);
And then it needs to be retrieved (to add more stuff to the layout). This was done with the following code:
QHBoxLayout *hLayoutTime(new QHBoxLayout);
qobject_cast<QVBoxLayout*>(layout())->addLayout(hLayoutTime);
qobject_cast
is the appropriate kind of cast to use here?
To avoid unneded casting write this like this:
void YourWidget::setupContents()
{
QVBoxLayout *vLayout = new QVBoxLayout(this); // effectively this does setLayout(new QVBoxLayout);
QHBoxLayout *hLayoutTime(new QHBoxLayout);
vLayout->addLayout(hLayoutTime);
… … …
}
Looking the code in your current example, Why don't you just get the pointer when creating?
auto *vLayout = new QVBoxLayout;
auto *hLayoutTime = new QHBoxLayout;
vLayout->addlaout(hLayoutTime);
Answering your question, probably the most adequate cast is:
dynamic_cast<QHBoxLayout*>(new QVBoxLayout);
dynamic_cast has several checks and benefits over static_cast, so it is better to use it when possible.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.