第三章 窗口部件

 1 /*    3-1 myWidget1   */
 2 #include <Qtgui>
 3 
 4 int main (int argc, char* argv[])//为什么是两个参数?
 5 {
 6     QApplication a(argc,argv);
 7     QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));//在代码中可以使用中文
 8 
 9     QWidget *widget =new QWidget();//new动态分配内存
10     widget->setWindowTitle(QObject::tr("我是widget"));//QObject::tr为了不出现乱码
11 
12 
13     QLabel *label =new QLabel ();
14     label->setWindowTitle(QObject::tr("我是label"));
15     label->setText(QObject::tr("label:我是个窗口"));
16 
17     label->resize(400,200); //改变部件大小
18 
19 
20     QLabel *label2=new QLabel (widget);//label2指定了父窗口是widget,所以label2不是窗口
21     label2->setText(QObject::tr("label2:我不是独立窗口,只是widget的子部件"));
22  
23 
24     label-> show();
25     widget->show();
26    // label2->show();   //因为label2不是一个窗口,所以不能显示出来
27 
28     int ret=a.exec();
29     delete label;//释放内存
30     delete widget;//会自动释放label2
31 
32     return ret;//时间循环
33 }

 

 1 /****************3-2   程序调试*****************
 2 1. 设置断点
 3 2. 单步调试(F11)
 4 3. 使用qDebug()函数
 5 */
 6 
 7 
 8 //#include <QtGui>
 9 #include <QApplication>
10 #include <QWidget>
11 #include <QDebug>
12 
13 int main(int argc,char *argv[])
14 {
15     QApplication a(argc,argv);
16 
17 
18     QWidget widget;
19     widget.resize (400,300);//其中数值,是不包括边框的
20     widget.move(200,100); //设置窗口位置
21     widget.show();
22 
23 
24     int x=widget.x();//分别返回部件的位置坐标x、y
25     qDebug("x:%d",x); //输出x的值
26     int y=widget.y();
27     qDebug("y:%d",y);
28 
29     QRect geometry=widget.geometry(); //返回没有边框和有边框架矩形的值
30     QRect frame=widget.frameGeometry();
31     qDebug()<<"geometry: "<<geometry <<"frame  " << frame;
32     qDebug()<<"1111";
33 
34 
35     return a.exec();
36 
37 }

 

posted @ 2015-10-16 11:11  蜗牛在奔跑  阅读(269)  评论(0编辑  收藏  举报