QT(2)Widget的小例子

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

  在上一次我们处理好了QT学习的环境,现在可以安安静静地去学习QT,可以参考http://doc.qt.nokia.com/latest/tutorials.html。下面是step by step,创建我们的第一个QT小程序。

一、搭建一个QT程序

#include <QtGui>

/* In this example, we will test QApplication usage. */
int main(int argc, char *argv[])
{
      /* 对于一个使用QT的GUI应用,无论是0个或者N个窗口,必须有一个QApplication的对象,而一个非GUI的QT应用,则使用QCoreApplication。他们之间的继承关系:QApplication -> QCoreApplication -> QObject */
      QApplication app(argc, argv);

      // ... 这里是程序的主体,我们在后面慢慢填充之...    

      return app.exec();
}

  创建一个QAppcliation对象,可以将命令行的参数传递过去。QApplication::exec()将唤起QT事件loop,这也是我们在main最后调用的函数。

二、创建一个窗口Windows

#include <QtGui>

/* In this example, we will test QApplication usage. */
int main(int argc, char *argv[])
{
      QApplication app(argc, argv);
      //如果一个widget创建是没有parent,这样将作为一个window或者一个top-level widget
      QWidget window;

      // Test 1: 显示Window,对于设置resize的情况,在MeeGo Handset IA32模拟器中是不生效的,Handset中认为所有的windows都是满屏,后面我将视这个window为panel,在上面加上其他组件。
     window.resize(240,120);
     window.setWindowTitle("Test 1: Show Windows");
     window.show();

      return app.exec();
}

三、在Windows上加载Widget

#include <QtGui>

/* In this example, we will test QApplication usage. */
int main(int argc, char *argv[])
{
       QApplication app(argc, argv);
       QWidget window;

      window.setWindowTitle("Test 2: Child Widgets");
      window.show();

      //我们采用QPushButton为例子:和不带parent创建windows不一样,button是一个windows的child,当windows destroy时,也将会被删除。注意隐藏和关闭windows是不会导致windows destroy的,但是程序结束可以。
      QPushButton * button = new QPushButton("Hello,world!",&window);
      button -> move(100,100);  //指定button的精确位置
      button -> show();

      return app.exec();
}

四、采用layout加载Widget

  一般而言,在程序中,很少直接采用指定精确位置,一般layout的方式。

#include <QtGui>

/* In this example, we will test QApplication usage. */
int main(int argc, char *argv[])
{
       QApplication app(argc, argv);
      QWidget window;

       window.setWindowTitle("Test 3: Using Layouts");
      window.show();

     //通常,子widget通过一个layout的对象进行排版,而不是直接精确地指定位置或者大小。这里我们采用QLable和输入框为例子,在创建的过程中,没有作为windows的子widget。
      QLabel * label = new QLabel("Name:");
      QLineEdit * lineEdit = new QLineEdit ();
      QHBoxLayout * layout = new QHBoxLayout();

      //Layout对象将管理在其上面的widget的位置和大小,通过addWidget()将widget加上。QHBBoxLayout的H表示水平方向。.
      layout->addWidget(label);
      layout->addWidget(lineEdit);

     //而layout本身加入window通过调用setLayout()。windows加入layout后,则layout上的widget成为window的子widget.
      window.setLayout(layout);

      return app.exec();
}

五、多层次layout(同时演示QDebug和QList的使用方法)

    

#include <QtGui>
#include <QDebug> //QDebug是很重要的,可以打印QString(不能使用printf)

/* In this example, we will test QApplication usage. */
int main(int argc, char *argv[])
{
      QApplication app(argc, argv);
      QWidget window;

      window.setWindowTitle("Test 4: Nested Layouts");
      window.show();

      //layout有QHBoxLayout和QVBoxLayout,分别是横向或者纵向。此外Qt还提供比较复杂的QGridLayout和QFormLayout。
      QLabel * label = new QLabel("Name:");
      QLineEdit * lineEdit = new QLineEdit ();
      QTableView * resultView = new QTableView();

      //上面的layout放置一个输入框,如同上一个例子。
      QHBoxLayout * layout = new QHBoxLayout();
      layout->addWidget(label);
      layout->addWidget(lineEdit);

      //创建一个纵向的layout,上面放置一个layout(一个输入框),通过addLayout(),下面放一个widget(表格)
      QVBoxLayout * mainLayout = new QVBoxLayout();
      mainLayout->addLayout(layout);
      mainLayout->addWidget(resultView);
      window.setLayout(mainLayout);

      //下面这一部分,在表格( QStandardItemModel)中加入内容,并且学习一下QT的一些语句用法,学习QList的使用。QList在内存方对象,这这个例子中,存放QStringList数组。在QT中在一个数组中加入一个entry采用“<<"的方式,如果继续加入,则继续“<<"。在表格中,每行有两个元素,放置在QStringList数组中,采用QStringList << "1" << "2" 。
      QStandardItemModel * model = new QStandardItemModel();
      model->setHorizontalHeaderLabels(QStringList() << "Name" << 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值