qt 编程 hello-world

目的:消除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小技巧:
----------------------------------------
1. qt中文显示

    QTextCodec *codec=QTextCodec::codecForName("utf-8");
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);

2. 图形化调试QMessageBox, 信息输出
QMessageBox::information(NULL, "Title", str);

3. 标签风格设置
m_label->setStyleSheet("border:1px solid gray;");
m_label->setStyleSheet("background-color:green;");

pushButton->setGeometry(10,10,200,200); //按钮的位置及大小
pushButton->setFixedSize( mypixmap.width(), mypixmap.height();

4. 密集排版:
排版:
h_layout1->addWidget(m_laybel);
h_layout1->addWidget(m_edit);
v_layout->addLayout(h_layout1);
v_layout->addLayout(h_layout2);
this->setLayout(v_layout);

margin(空白): 控件到布局四周的边缘距离
space(间距): 布局管理器内各部件的距离
v_layout->setSpacing(0);  
v_layout->setMargin(0);  
v_layout->setContentsMargins(0,0,0,0); // 可替代setMargin, 有更准确的控制(left,top,right,botton)


5. 对话框退出返回值
close(), 返回为0
reject(), 返回为0
accept(), 返回为1

6.
QString line="1,2,,,,,7"
args=line.split(",",QString::SkipEmptyParts);


另: QtCreator 是一个集成IDE, 它的调试还是依赖于gdb. 所以, 调试时可以不用QtCreator, 而直接使用gdb

我使用自己熟悉的vim+gdb 来调试!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值