第一个Qt程序

从头体验Qt
先建立一个空项目
empty qmake project

Helloworld.pro

QT += widgets

SOURCES += \
    main.cpp

main.cpp

#include<QApplication>
#include<QWidget>//窗口控件基类
#include<QPushButton>//按钮
int main(int argc,char **argv)
{
    QApplication app(argc,argv);
    QWidget w;
    w.setWindowTitle("helloworld");


    QPushButton b;
    b.setText("~_~");//给按钮设置内容
    b.setParent(&w);//指定父对象,要加取地址符
    b.move(100,100);//移动坐标,默认是左上角

    QPushButton b1(&w);//通过构造函数传参
    b.setText("son2");

    //如果不指定父对象,对象和对象(窗口和窗口)是独立的
     //* a指定b为它的父对象,a放在b的上面
    /*
     * 指定父对象,有2种方式:
     *   1)setParent
     *   2) 通过构造函数传参
     * 指定父对象,只需要父对象显示,上面的子对象自动显示
    */
    w.show();
    app.exec();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wdshhh/article/details/104235735