03黑马QT笔记之指定父对象(上一篇基础上)
1 看代码。
1).pro项目我就:
QT +=widgets #使用窗口类
SOURCES += \
main.cpp
2)主函数main.cpp:
#include<QApplication>
#include<QWidget>
#include<QPushButton>
int main(int argc,char *argv[]){
QApplication app(argc,argv);
QWidget w;
w.setWindowTitle("Hello World!"); //设置窗口左上角的主题
/* 这样写两个对象是没有联系的 需要指定父对象
QPushButton button1;
button1.setText("按钮1");
button1.show();
*/
/*
指定父对象的方法:1)setparent函数 2)构造函数
指定父对象后 只要显示父对象 其子对象就会跟着显示 上面button1.show()不写则会不显示
*/
//指定父对象方法1
QPushButton button1;
button1.setText("按钮1"); //设置按钮文本内容
button1.move(100,100); //按钮起始坐标位置
button1.setParent(&w);
//指定父对象方法2
QPushButton button2(&w);
button2.setText("按钮2");
w.show(); //显示父对象 两个按钮子对象也会自动显示
return app.exec();
}