上次我们安装好了整个QT的环境,这次我们开始编写代码。程序呢都是现成的,从http://doc.qt.nokia.com/4.7/widgets-tutorial.html这里可以找到,都是官方的教程,例程,我只是拿来练练手。
在QT Creator里创建的是qt的空白工程,没有自动包含什么类的头文件之类的,目前只需要手动添加一个main.cpp就可以搞定一切了。
首先是一个最简单的窗口程序:
- #include <QtGui>
- /*
- 如果一个widget直接被创建,没有父控件,
- 那么它将被当做一个窗口对待,或者顶层控件,在当它显示的时候
- 因为它没有父对象来保证在他不被需要的时候能够删除,
- 它需要开发者在应用程序中跟踪顶层控件
- */
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QWidget window;
- window.resize(320, 240);/*设置窗口默认大小*/
- window.show();
- /*设置窗口标题*/
- window.setWindowTitle(QApplication::translate("toplevel","Top-level widget"));
- return app.exec();
- }
然后呢是一个稍微复杂点的,用到了一个按钮控件
- #include<QtGui>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QWidget window;
- window.resize(320, 240); /*设置默认大小*/
- /*设置窗口标题*/
- window.setWindowTitle(QApplication::translate("childwidget","Child widget"));
- window.show();
- /*新建一个按钮,建立按钮的函数参数有几种,我们用的这种是
- 提供按钮上显示的文本,指定其父对象的方式*/
- QPushButton *button = new QPushButton(QApplication::translate("childwidget","Press me"),&window);
- /*移动控件到指定位置,在它被显示之前就要发送这个移动信号*/
- button->move(100, 100);
- button->show();
- return app.exec();
- }
- /*
- button是窗口的一个子控件,将会在窗口被删除的时候删除掉
- 注意到,隐藏或者关闭这个窗口不会自动的销毁掉button,
- 在程序退出的时候它才被销毁
- */
再然后呢,是一个更加复杂点的,用到了布局控件
- #include<QtGui>
- /*
- 通常窗口中的子控件是通过布局对象来安排位置的,
- 而不是通过指定大小和移动来确定位置。
- */
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QWidget window;
- /*新建一个标签*/
- QLabel *label = new QLabel(QApplication::translate("windowlayout","Name:"));
- /*新建一个行编辑器*/
- QLineEdit *lineEdit = new QLineEdit();
- /*新建一个水平布局对象*/
- QHBoxLayout *layout = new QHBoxLayout();
- /*
- 把东西装进去
- 通过addWidget()管理了控件的位置和大小
- 布局控件自身对窗口提供了setLayout函数
- */
- layout->addWidget(label);
- layout->addWidget(lineEdit);
- window.setLayout(layout);
- window.setWindowTitle(QApplication::translate("windowlayout", "Window layout"));
- window.show();
- return app.exec();
- }
最后是一个稍微长一点的,用到了C++比较高级的类模板啥的,目前还没搞懂
- #include <QtGui>
- /*
- 只有控件可以包含其他的控件,布局控件可以为控件们提供
- 不同层次的分组。
- 我们在这个程序里使用了两个布局控件
- queryLayout是一个横向布局控件,包含了一个标签和一个行编辑控件
- mainLayout是一个纵向布局控件,包含了queryLayout和一个QTableView
- */
- int main(int argc , char *argv[])
- {
- QApplication app(argc, argv);
- QWidget window;
- QLabel *queryLabel = new QLabel(QApplication::translate("nestedlayouts","Query:"));
- QLineEdit *queryEdit = new QLineEdit();
- QTableView *resultView = new QTableView();
- QHBoxLayout *queryLayout = new QHBoxLayout();
- queryLayout->addWidget(queryLabel);
- queryLayout->addWidget(queryEdit);
- QVBoxLayout *mainLayout = new QVBoxLayout();
- /*
- 调用mainLayout的addLayout来放置queryLayout
- */
- mainLayout->addLayout(queryLayout);
- mainLayout->addWidget(resultView);
- window.setLayout(mainLayout);
- // Set up the model and configure the view...
- /*QStandardItemModel类提供一个通用的模型,为存储数据*/
- QStandardItemModel model;
- /*设置横向的表头,用标签*/
- model.setHorizontalHeaderLabels(
- QStringList() <<QApplication::translate("nestedlayouts", "Name")
- <<QApplication::translate("nestedlayouts","Office"));
- QList<QStringList> rows = QList<QStringList>()
- << (QStringList() << "Verne Nilsen" << "123")
- << (QStringList() << "Carlos Tang" << "77")
- << (QStringList() << "Bronwyn Hawcroft" << "119")
- << (QStringList() << "Alessandro Hanssen" << "32")
- << (QStringList() << "Andrew John Bakken" << "54")
- << (QStringList() << "Vanessa Weatherley" << "85")
- << (QStringList() << "Rebecca Dickens" << "17")
- << (QStringList() << "David Bradley" << "42")
- << (QStringList() << "Knut Walters" << "25")
- << (QStringList() << "Andrea Jones" << "34");
- foreach(QStringList row, rows) {
- QList<QStandardItem*> items;
- foreach(QString text, row)
- items.append(new QStandardItem(text));
- model.appendRow(items);
- }
- /*以上我们遇到了2个很强大的东西:一个是Qlist<>,另一个是QStringList(),这里略过,解释另开一章*/
- resultView->setModel(&model);
- resultView->verticalHeader()->hide();
- resultView->horizontalHeader()->setStretchLastSection(true);
- window.setWindowTitle(
- QApplication::translate("nestedlayouts", "Nested layouts")
- );
- window.show();
- return app.exec();
- }
上面的程序,简单的描述了简单的QT程序怎么写,跟GTK差不多,不过GTK是C写的。
一些C++的语言特性,我还不是很知道,需要查查C++的书。
这x