qt 编程 hello-world
2015-08-04 11:16
260 查看
目的:消除qt 的神秘性,qt 就是一个支持qt库的c++ 程序。
----------------------------------------
qt 的控制台输出:
----------------------------------------
qt debug
qDebug 就恰如printf, qDebug()就恰如stdout 对象,看下面例子。
$ cat hello.cpp
#include <QApplication>
//#include <QDebug> //调试对象,c++方式
#include <stdio.h>
int main(void)
{
qDebug("Hello, welcome to Qt world!");
qDebug("qDebug auto add enter at end");
int i=3;
qDebug("i is %d", i);
printf("i like this before,now it can retired\n");
// qDebug() << "if you include <QDebug>, you can use QDebug object" ;
return 0;
}
$ ./qt
Hello, welcome to Qt world!
qDebug auto add enter at end
i is 3
i like this before,now it can retired
----------------------------------------
qt 图形编程
----------------------------------------
$ cat hello.cpp
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
qDebug("Hello, welcome to Qt world!");
QApplication *app=new QApplication(argc,argv);
QLabel *label = new QLabel("hahaha");
label->show();
app->exec();
}
附录: 编译过程
qmake -project //生成qt.pro
qmake // 生成makefile
make // 编译, 若makefile 不变,只运行make 即可
$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o hello.o hello.cpp
g++ -m64 -Wl,-O1 -o qt hello.o -L/usr/lib/x86_64-linux-gnu -lQtGui -lQtCore -lpthread
看一下它的包含路径:
/usr/include/qt4/QtCore
/usr/include/qt4/QtGui
/usr/include/qt4
.
/usr/share/qt4/mkspecs/linux-g++-64
----------------------------------------
2. signal 与 slot 例子
----------------------------------------
cat hello.cpp
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
qDebug("Hello, welcome to Qt world!");
QApplication *app=new QApplication(argc,argv);
QPushButton *button = new QPushButton("press me to quit");
QObject::connect(button,SIGNAL(click()),app,SLOT(exit()));
button->show();
app->exec();
}
通过宏展开知道,SIGNAL, SLOT 是两个简单宏。
QObject::connect(button,"2""click()",app,"1""exit()");
----------------------------------------
3. layout 及主窗口
----------------------------------------
$ cat hello.cpp
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QSpinBox>
#include <QSlider>
int main(int argc, char *argv[])
{
QApplication *app=new QApplication(argc,argv);
QWidget * widget= new QWidget();
QSpinBox * spinbox = new QSpinBox();
QSlider * slider = new QSlider(Qt::Horizontal);
QObject::connect(spinbox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));
QObject::connect(slider,SIGNAL(valueChanged(int)),spinbox,SLOT(setValue(int)));
spinbox->setValue(40);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(spinbox);
layout->addWidget(slider);
widget->setLayout(layout);
widget->show();
app->exec();
}
并列的两个或多个控件,需要布局。
用一个widget 做主窗口
目前进展为: 创建几个控件,控件可以互发信号协调工作。
1. 先声明窗口部件
2. 设置它们的属性
3. 把窗口添加到布局中,布局会自动设置窗口位置和大小
4. 利用窗口的信号和槽,通过窗口部件之间的连接管理用户的交互行为
以上步骤,用qt 的designer很容易完成,手工编写也可以。
----------------------------------------
qt 的控制台输出:
----------------------------------------
qt debug
qDebug 就恰如printf, qDebug()就恰如stdout 对象,看下面例子。
$ cat hello.cpp
#include <QApplication>
//#include <QDebug> //调试对象,c++方式
#include <stdio.h>
int main(void)
{
qDebug("Hello, welcome to Qt world!");
qDebug("qDebug auto add enter at end");
int i=3;
qDebug("i is %d", i);
printf("i like this before,now it can retired\n");
// qDebug() << "if you include <QDebug>, you can use QDebug object" ;
return 0;
}
$ ./qt
Hello, welcome to Qt world!
qDebug auto add enter at end
i is 3
i like this before,now it can retired
----------------------------------------
qt 图形编程
----------------------------------------
$ cat hello.cpp
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
qDebug("Hello, welcome to Qt world!");
QApplication *app=new QApplication(argc,argv);
QLabel *label = new QLabel("hahaha");
label->show();
app->exec();
}
附录: 编译过程
qmake -project //生成qt.pro
qmake // 生成makefile
make // 编译, 若makefile 不变,只运行make 即可
$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o hello.o hello.cpp
g++ -m64 -Wl,-O1 -o qt hello.o -L/usr/lib/x86_64-linux-gnu -lQtGui -lQtCore -lpthread
看一下它的包含路径:
/usr/include/qt4/QtCore
/usr/include/qt4/QtGui
/usr/include/qt4
.
/usr/share/qt4/mkspecs/linux-g++-64
----------------------------------------
2. signal 与 slot 例子
----------------------------------------
cat hello.cpp
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
qDebug("Hello, welcome to Qt world!");
QApplication *app=new QApplication(argc,argv);
QPushButton *button = new QPushButton("press me to quit");
QObject::connect(button,SIGNAL(click()),app,SLOT(exit()));
button->show();
app->exec();
}
通过宏展开知道,SIGNAL, SLOT 是两个简单宏。
QObject::connect(button,"2""click()",app,"1""exit()");
----------------------------------------
3. layout 及主窗口
----------------------------------------
$ cat hello.cpp
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QSpinBox>
#include <QSlider>
int main(int argc, char *argv[])
{
QApplication *app=new QApplication(argc,argv);
QWidget * widget= new QWidget();
QSpinBox * spinbox = new QSpinBox();
QSlider * slider = new QSlider(Qt::Horizontal);
QObject::connect(spinbox,SIGNAL(valueChanged(int)),slider,SLOT(setValue(int)));
QObject::connect(slider,SIGNAL(valueChanged(int)),spinbox,SLOT(setValue(int)));
spinbox->setValue(40);
QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(spinbox);
layout->addWidget(slider);
widget->setLayout(layout);
widget->show();
app->exec();
}
并列的两个或多个控件,需要布局。
用一个widget 做主窗口
目前进展为: 创建几个控件,控件可以互发信号协调工作。
1. 先声明窗口部件
2. 设置它们的属性
3. 把窗口添加到布局中,布局会自动设置窗口位置和大小
4. 利用窗口的信号和槽,通过窗口部件之间的连接管理用户的交互行为
以上步骤,用qt 的designer很容易完成,手工编写也可以。
相关文章推荐
- Qt:Event Filter事件过滤器
- qtcreator learning skills
- Qt应用程序所有窗口共享OpenGL上下文
- QT::QTableView的使用
- SPOJ QTREE 系列
- libevent安装与libevent定时器
- Qt核心编程___集合类
- Qt信号与槽的参数传递原理
- Qt创建窗体的过程
- 剖析Qt的事件机制原理
- 【Qt OpenGL教程】10:加载3D世界,并在其中漫游
- Qt 子窗口内嵌到父窗口中(无边框附体show即可)
- Qt无边框MainWindow如何拖动四周改变大小
- QTableView实时更新数据
- VS2008+Qt4.7.3+VTK5.10.1+PCL1.5.1
- Qt容器类整理
- Qt Creator 要掌握的快捷操作
- 利用QT来实现基本图形的绘制应用
- QT编程中编译的时候出现c_jkdacx0.obj : error LNK2001: 无法解析的外部符号 "class QTextCodec * GlobalCodec" ?
- Qtcreator 环境使用