Qt findChild返回0 [英] Qt findChild returning 0

查看:1274
本文介绍了Qt findChild返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用一个很好的方法来使用GUI修改对象内容在运行时,项目使用Qt。



所以我想传递一个QLayout对象,让他们创建自己的GUI。作为POC,我创建了这个(display是QVBoxLayout *参数的名称):

  QPushButton * button = new QPushButton (); 
button-> setText(foo);
button-> setObjectName(bar);
display-> addWidget(button);

它的工作原理与预期一样好。但是我需要读取用户键入的GuI,所以这是我做的下一步:

  QPushButton * button2 = display-> findChild< QPushButton *>(); 
if(button2)
std :: cout<< button2-> objectName()。toStdString()<< std :: endl;

这里没有输出任何内容 - if()语句为false!

  QList< QWidget *> widgets = display-> findChildren< QWidget *>(); 
foreach(QWidget * b,widgets){
std :: cout<< b-> objectName()。toStdString()<< std :: endl;类似地,窗口小部件列表为空。




$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b

我尝试通过 http ://doc.qt.digia.com/qt/qvboxlayout-members.html ,但findChild / findChildren似乎最适合我的需要...



最好的问候,
LDer



解决方案



这是我现在处理的方式:



而不是传递一个QVBoxLayout到GUI创建,一个QWidget应该传递,显示。

  QVBoxLayout * layout = new QVBoxLayout(); 
display-> setLayout(layout);

QPushButton * button = new QPushButton();
button-> setText(foo);
button-> setObjectName(bar);

layout-> addWidget(button);

现在到findChild / findChildren:

  QPushButton * button2 = display-> findChild< QPushButton *>(bar); 
if(button2)
std :: cout<< button2-> objectName()。toStdString()<< std :: endl;

QList< QWidget *> widgets = display-> findChildren< QWidget *>();
foreach(QWidget * b,widgets){
std :: cout<< b-> objectName()。toStdString()<< std :: endl;
}

这两种方法对我都有效。



对Slavik81的感谢

解决方案

QWidget 的父级必须是 QWidget QLayout 不是 QWidget



移动并调整 QWidget 的子项。虽然您可以通过在布局上调用来添加子窗口小部件,但最终,它们的父窗口将是布局所在的 QWidget



说明:

  QWidget * widget = new QWidget; 
qDebug(Widget:%p,widget);

QHBoxLayout * layout = new QHBoxLayout;
qDebug(Layout:%p,layout);

QWidget * child = new QWidget;
layout-> addWidget(child);

qDebug(setLayout:%p之前的子级父级,child-> parent());
widget-> setLayout(layout);
qDebug(setLayout:%p之后的子级父级,child-> parent());

输出:

 code> Widget:0x8e1c1e0 
布局:0x8e1c3f0
setLayout之前的子级父级:0x0
setLayout后的子级父级:0x8e1c1e0


I'm currently working on a nice way to use a GUI to modify object contents at runtime, project uses Qt.

So I thought of passing a QLayout to the objects, to let them create their own GUI. As POC, I created this ("display" is the name of the QVBoxLayout* parameter):

QPushButton* button = new QPushButton();
button->setText("foo");
button->setObjectName("bar");
display->addWidget(button);

which works just as fine as expected. But I will need to read what the user typed into the GuI, so this is what I did next:

QPushButton *button2 = display->findChild<QPushButton *>();
if(button2)
    std::cout << button2->objectName().toStdString() << std::endl;

here nothing is put out - if() statement is false!

QList<QWidget *> widgets = display->findChildren<QWidget *>();
foreach (QWidget* b, widgets) {
    std::cout << b->objectName().toStdString() << std::endl;
}

similarly, the "widgets" list is empty.

I tried looking through the full member list at http://doc.qt.digia.com/qt/qvboxlayout-members.html, but findChild/findChildren seems like the best fit to my needs...

Best regards, LDer

SOLUTION BELOW

This is how I handle it now:

instead of passing a QVBoxLayout to the GUI creation, a QWidget should be passed, in following named "display" as above.

QVBoxLayout* layout = new QVBoxLayout();
display->setLayout(layout);

QPushButton* button = new QPushButton();
button->setText("foo");
button->setObjectName("bar");

layout->addWidget(button);

now to findChild / findChildren:

QPushButton *button2 = display->findChild<QPushButton *>("bar");
if(button2)
    std::cout << button2->objectName().toStdString() << std::endl;

QList<QWidget *> widgets = display->findChildren<QWidget *>();
foreach (QWidget* b, widgets) {
    std::cout << b->objectName().toStdString() << std::endl;
}

both methods work for me as expected! (Plus, now the layout can be chosen freely by the GUI creation!)

Kudos to Slavik81

解决方案

The parent of a QWidget must be a QWidget. QLayout is not a QWidget.

Layouts exist to move and resize the children of a QWidget. Though you may add child widgets by making calls on the layout, ultimately, their parent will be the QWidget that the layout resides on.

To illustrate:

QWidget* widget = new QWidget;
qDebug("Widget: %p", widget);

QHBoxLayout* layout = new QHBoxLayout;
qDebug("Layout: %p", layout);

QWidget* child = new QWidget;
layout->addWidget(child);

qDebug("Child's parent before setLayout: %p", child->parent());
widget->setLayout(layout);
qDebug("Child's parent after setLayout: %p", child->parent());

Output:

Widget: 0x8e1c1e0
Layout: 0x8e1c3f0
Child's parent before setLayout: 0x0
Child's parent after setLayout: 0x8e1c1e0

这篇关于Qt findChild返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆