[英]Application crashing when searching for QLineEdit in QFormLayout
I've experienced a weird crash when trying to find a QLineEdit
in a QFormLayout
.尝试在
QFormLayout
中找到QLineEdit
时,我遇到了奇怪的崩溃。
Firstly, I created a QFormLayout
and set a name for it:首先,我创建了一个
QFormLayout
并为其命名:
QFormLayout *charColLayout = new QFormLayout; charColLayout->setObjectName("charColLayout");
Then, I created a QLineEdit
, modified it a bit and add it in to the layout, and I also give it a name:然后,我创建了一个
QLineEdit
,对其进行了一些修改并将其添加到布局中,我还给它起了一个名字:
QLineEdit *delim = new QLineEdit;
delim->setMaxLength(1);
delim->setMaximumWidth(100);
delim->setText("/");
delim->setObjectName("delEdit");
charColLayout->addRow("Delimiter", delim);
Afterward, in a completely different function, I re-searched that layout with findChild()
:之后,在一个完全不同的 function 中,我使用
findChild()
重新搜索了该布局:
QFormLayout *charcoal = secondHoriField->findChild<QFormLayout *>("charColLayout", Qt::FindChildrenRecursively);
It should be noted that secondHoriField
is just a normal QLayout
which my QFormLayout
is located in.需要注意的是,
secondHoriField
只是我的QFormLayout
所在的普通QLayout
。
Finally, I attempted to find that QLineEdit
:最后,我试图找到
QLineEdit
:
QLineEdit *delimEdit = charcoal->findChild<QLineEdit*>("delEdit", Qt::FindChildrenRecursively);
if (delimEdit == nullptr) {cerr << "error\n";} //debug
string curDelim = qStrToStr(delimEdit->text());
And it surprisingly came down with a crash, and as the output shown, it's because the delimEdit
is null
.它令人惊讶地崩溃了,正如 output 所示,这是因为
delimEdit
是null
。
18:06:10: Starting D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe ...
error
18:06:17: The program has unexpectedly finished.
18:06:17: The process was ended forcefully.
18:06:17: D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe crashed.
But when I switched the findChild()
function for this rather bruteforce-y line:但是,当我将
findChild()
function 切换为这条相当蛮力的行时:
QLineEdit *delimEdit = dynamic_cast<QLineEdit*>(charcoal->itemAt(1)->widget());
cerr << qStrToStr(delimEdit->objectName()) << endl; //debug line
The program ran fine, and it showed the same name I set for the QLineEdit
:该程序运行良好,它显示了我为
QLineEdit
设置的相同名称:
18:12:02: Starting D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe ...
delEdit
18:12:11: D:\...\build-cryptog-Desktop_Qt_5_15_2_MinGW_64_bit-Debug\debug\cryptog.exe exited with code 0
Why did this happened?为什么会这样?
Another note: qStrToStr()
is a function I implement to convert QString
to std::string
, and I have hand-checked it.另一个注意事项:
qStrToStr()
是一个 function 我实现将QString
转换为std::string
,我已经手动检查了它。
While findChild
is a QObject method the itemAt
is a QFormLayout method. findChild
是一个 QObject 方法,而itemAt
是一个 QFormLayout 方法。
With addRow
you add an item to the QFormLayout.使用
addRow
可以向 QFormLayout 添加一个项目。 This does not make it a child in the context of the QObject.这不会使其成为 QObject 上下文中的子对象。
The purpose of the QFormLayout is to organize the positioning of QWidgets, it is not meant to serve as a container. QFormLayout 的目的是组织 QWidgets 的定位,而不是充当容器。 Maybe you could check whether the top level QWidget (eg QMainWindow) holding the QFormLayout would be a better choice as a parent for the QLineEdit.
也许您可以检查持有 QFormLayout 的顶级 QWidget(例如 QMainWindow)作为 QLineEdit 的父级是否是更好的选择。
Assuming you have some kind of QMainWindow
:假设您有某种
QMainWindow
:
QMainWindow myMainWindow;
// ...
QLineEdit *delim = new QLineEdit(&myMainWindow);
delim->setObjectName("delEdit");
//...
In another location:在另一个位置:
auto delimEdit = myMainWindow.findChild<QLineEdit*>("delEdit");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.