您的位置:首页 > 编程语言 > Qt开发

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很容易完成,手工编写也可以。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: