I'm a beginner in C++ QT. I'd like to know
Q1. What is the effect when I pass the " parent
" parameter into the parent class => QDialog(parent)
? Q2 .What does this mean ui(new Ui::MyDialog)
?
This is .cpp code.
MyDialog::MyDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MyDialog)
{
ui->setupUi(this);
}
In .h code.
namespace Ui {
class MyDialog;
}
Thank you!
When you create your user-interface, you have at least 2 choices: hard-code or use Qt Designer
which create XML
code which describes your window. Next uic
(user interface compiler) translate this XML
code into real valid C++/Qt
code and save it inside ui_name.h
and you need include this code to your class. It is exactly what code in your question does. It declare your class inside Ui
namespace and allocate memory for this class (in constructor). Next you call setupUi(this);
. This method contains all main code, such as allocating memory for widgets, handling layouts, establish connections, etc.
About parent: it is part of Qt
object-tree system which simplifies memory handling in Qt
. As you know C++
is not a garbage-collected language and you need to delete memory manually. But Qt
offers you new solution where parent destroys all children, so it is simpler way. This parent in your code means that MyDialog
is a children of QDialog
.
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.