//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执行方法一样