Qt学习笔记(1)

来源:互联网 发布:杜兰特 数据 编辑:程序博客网 时间:2024/05/16 07:40

因为最近有个课设需要完成,时间也很充裕,我就想用个新东西来完成这次的课设。

最后选定了Qt来开发。

我是win7下开发的,Qt我是从朋友那里拷过来的安装包qt-win-opensource-4.7.1-mingw,一共300+M,还蛮大的。

mingw,我在官网下载了个最新版本的


先装mingw 再装Qt

不过我安装完后貌似没有网上一些教程上说的qt creater 我也没有下载了,干脆直接用edit plus +Qt 4.7.1 Command Prompt了


第一个hello world

#include <QtGui/QApplication> #include <QLabel>  int main(int argc, char *argv[]) {         QApplication a(argc, argv);         QLabel *label = new QLabel("Hello, world!");         label->show();         return a.exec(); }



编写完cpp后在 qt command prompt 中用qmake -project命令创建工程文件

然后再执行qmake命令创建makefile等文件

最后用mingw32-make debug编译

最后去debug目录执行。。

如果执行错误需要设置path环境变量

path = C:\MinGW\bin;C:\MinGW\libexec\gcc\mingw32\4.4.0;D:\qt\4.7.1\bin

信号槽:


QPushButton *button = new QPushButton("Quit");QObject::connect(button,SIGNAL(clicked()),&app,SLOT(quit()));button->show();



期中最关键的一句是第二句代码,当有button的信号clicked(),执行槽函数quit()。


布局layout:


你需要先定义一个布局QHBoxLayout或者是QVBoxLayout  里面的h和v我理解为代表水平和垂直。

layout->addWidget(QWidget*) 向布局里面添加部件,类似QPushButton和QSlider等。。

        QHBoxLayout *layout = new QHBoxLayout;layout->addWidget(spinBox);layout->addWidget(slider);

然后应用布局

        QWidget *window = new QWidget;        window->setLayout(layout);        window->show();

原创粉丝点击