简体   繁体   中英

Application crashing when searching for QLineEdit in QFormLayout

I've experienced a weird crash when trying to find a QLineEdit in a QFormLayout .

Firstly, I created a QFormLayout and set a name for it:

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 *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() :

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.

Finally, I attempted to find that 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 .

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:

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 :

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.

While findChild is a QObject method the itemAt is a QFormLayout method.

With addRow you add an item to the QFormLayout. This does not make it a child in the context of the QObject.

The purpose of the QFormLayout is to organize the positioning of QWidgets, it is not meant to serve as a container. 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.

Assuming you have some kind of QMainWindow :

QMainWindow myMainWindow;
// ...
QLineEdit *delim = new QLineEdit(&myMainWindow);
delim->setObjectName("delEdit");
//...

In another location:

auto delimEdit = myMainWindow.findChild<QLineEdit*>("delEdit");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM