Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92540
  • 博文数量: 34
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 275
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-13 23:05
文章分类

全部博文(34)

文章存档

2011年(1)

2010年(7)

2009年(26)

我的朋友

分类:

2009-08-16 10:37:08

Widgets Tutorial

Introduction

Widgets are the basic building blocks of graphical user interface (GUI) applications made with Qt. Each GUI component, such as a button, label or text editor, is a widget and can be placed within an existing user interface or displayed as an independent window. Each type of component is provided by a particular subclass of QWidget, which is itself a subclass of QObject.

QWidget is not an abstract class; it can be used as a container for other widgets, and can be subclassed with minimal effort to create custom widgets. It is most often used to create windows in which other widgets are placed.

As with QObjects, widgets can be created with parent objects to indicate ownership, ensuring that objects are deleted when they are no longer used. With widgets, these parent-child relationships have an additional meaning: each child is displayed within the screen area occupied by its parent. This means that, when a window is deleted, all the widgets it contains are automatically deleted.

Creating a Window

首先要有一个  QApplication a(argc, argv);  //#include

             return a.exec();

If a widget is created without a parent, it is treated as a window, or top-level widget, when it is shown. Since it has no parent object to ensure that it is deleted when no longer needed, it is up to the developer to keep track of the top-level widgets in an application.

In the following example, we use QWidget to create and show a window with a default size:

     QWidget *window = new QWidget(); //#include 
     window->resize(320, 240);
     window->show();

We can add a child widget to this window by passing window as the parent to its constructor. In this case, we add a button to the window and place it in a specific location:

     QPushButton *button = new QPushButton(tr("Press me"), window);  //#include 
     button->move(100, 100);
     button->show();

The button is now a child of the window and will be deleted when the window is destroyed. Note that hiding or closing the window does not automatically destroy it.

Using Layouts

Usually, child widgets are arranged inside a window using layout objects rather than by specifying positions and sizes explicitly. Here, we construct a label and line edit widget that we would like to arrange side-by-side.

     QLabel *label = new QLabel(tr("Name:"));  //#include 
     QLineEdit *lineEdit = new QLineEdit();  //#include 

     QHBoxLayout *layout = new QHBoxLayout();  //#include 
     layout->addWidget(label);
     layout->addWidget(lineEdit);
     window->setLayout(layout);

The layout object we construct manages the positions and sizes of widgets supplied to it with theaddWidget() function. The layout itself is supplied to the window itself in the call to setLayout(). Layouts are only visible through the effects they have on the widgets (and other layouts) they are responsible for managing.

In the example above, the ownership of each widget is not immediately clear. Since we construct the widgets and the layout without parent objects, we would expect to see an empty window and two separate windows containing a label and a line edit. However, when we tell the layout to manage the label and line edit and set the layout on the window, both the widgets and the layout itself are ''reparented'' to become children of the window.

Just as widgets can contain other widgets, layouts can be used to provide different levels of grouping for widgets. Here, we want to display a label alongside a line edit at the top of a window, above a table view showing the results of a query.

     QLabel *queryLabel = new QLabel(tr("Query:"));
     QLineEdit *queryEdit = new QLineEdit();
     QTableView *resultView = new QTableView();  //#include 

     QHBoxLayout *queryLayout = new QHBoxLayout();
     queryLayout->addWidget(queryLabel);
     queryLayout->addWidget(queryEdit);

     QVBoxLayout *mainLayout = new QVBoxLayout();
     mainLayout->addLayout(queryLayout);
     mainLayout->addWidget(resultView);
     window->setLayout(mainLayout);

As well as QHBoxLayout and QVBoxLayout, Qt also provides QGridLayout and QFormLayout classes to help with more complex user interfaces.

阅读(571) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~