码迷,mamicode.com
首页 > 其他好文 > 详细

qtday01 ubuntu 下手动编写第一个qt程序

时间:2014-06-11 00:38:57      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:code   ext   get   文件   string   set   

//qt 5.2.1
//1t目录下
//1t.cpp
/*
 第一个qt程序
 */
#include <QApplication>
#include<QtWidgets/QLabel>
#include<QString>
#include<QTextCodec>
int main(int argc,char** argv){
    /*构造一个对象*/
    QApplication app(argc,argv);
    QTextCodec* coder=QTextCodec::codecForName("utf-8");
    /*设置编码对象*/
//    QTextCodec::setCodecForTr(coder);
    //set code to the words
    //QLabel qlab(QObject::tr("hello qt你好 ,today is a nice day!"));
    QLabel qlab(coder->toUnicode("hello qt你好 ,today is a nice day!"));
    qlab.show();
    /*让程序进入事件循环*/
    return app.exec();
}
//在1t目录下,输入命令:
//qmake -project  --生成 .pro文件
//vi 1t.pro    在最后加一行  QT+=widget
//qmake           --生成Makefile文件
//make            --执行Makefile文件,生成最终可执行文件
//./1t            --运行程序


//在2t目录下
//2t.cpp
#include<QApplication>
#include<QtWidgets/QLabel>
#include<QtWidgets/QPushButton>//按钮
#include<QWidget>//窗口对象
#include<QObject>
#include<QSlider>
#include<QSpinBox>
int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
    /*堆空间,创建窗口*/
    QWidget * parent=new QWidget();
    parent->resize(400,300);//设置窗口大小
    //
    QSlider * qsli;
    QSpinBox* qspin;
    qsli=new QSlider(parent);
    qspin=new QSpinBox(parent);
    qsli->move(0,20);
    qsli->resize(20,150);
    qspin->move(40,20);
    /*标签*/
    QLabel qlab("second label",parent);
    //qlab.show();
    /*按钮*/
    QPushButton qb1("close win",parent);
    qb1.resize(80,20);
    qb1.move(160,180);//设置按钮位置(居左上角)
    QPushButton qb("close label",parent);
    qb.resize(80,20);
    qb.move(160,160);//设置按钮位置(居左上角)
    /*信号函数(SIGNAL()转换)*/
    /*槽函数(SLOT()转换),    槽函数可当做成员函数用*/
    qspin->setValue(10);
    /*按钮的clicked()函数触发标签的close()事件,关闭label*/
    QObject::connect(&qb,SIGNAL(clicked()),&qlab,SLOT(close()));
    QObject::connect(&qb1,SIGNAL(clicked()),&app,SLOT(quit()));
    QObject::connect(qsli,SIGNAL(sliderMoved(int)),qspin,SLOT(setValue(int)));
    
    //关闭整个程序
    //QObject::connect(&qb,SIGNAL(clicked()),&app,SLOT(quit()));
    
    //qb.show();
    parent->show();
    app.exec();
}

//与1t执行方法一样


qtday01 ubuntu 下手动编写第一个qt程序,布布扣,bubuko.com

qtday01 ubuntu 下手动编写第一个qt程序

标签:code   ext   get   文件   string   set   

原文地址:http://blog.csdn.net/pukuimin1226/article/details/29398913

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!