QT 1, Nested Layouts

2307阅读 0评论2010-12-29 kingliang123
分类:C/C++

  1. #include <QtGui>

  2. int main(int argc, char *argv[])
  3. {
  4.     QApplication app(argc, argv);
  5.     QWidget window;

  6.     QLabel *queryLabel = new QLabel(
  7.             QApplication::translate("nestedlayouts", "Query:"));
  8.     QLineEdit *queryEdit = new QLineEdit();
  9.     QTableView *resultView = new QTableView();

  10.     QHBoxLayout *queryLayout = new QHBoxLayout();
  11.     queryLayout->addWidget(queryLabel);
  12.     queryLayout->addWidget(queryEdit);

  13.     QVBoxLayout *mainLayout = new QVBoxLayout();
  14.     mainLayout->addLayout(queryLayout);
  15.     mainLayout->addWidget(resultView);
  16.     window.setLayout(mainLayout);

  17.     // Set up the model and configure the view...
  18.     QStandardItemModel model;
  19.     QStringList qstringlist;
  20.     qstringlist << "Name" << "Number";

  21.     model.setHorizontalHeaderLabels (qstringlist);
  22.     // model.setHorizontalHeaderLabels(
  23.      // QStringList() << QApplication::translate("nestedlayouts", "Name")
  24.       // << QApplication::translate("nestedlayouts", "Office"));

  25.          QList<QStringList> rows = QList<QStringList>()
  26.              << (QStringList() << "Verne Nilsen" << "123")
  27.              << (QStringList() << "Carlos Tang" << "77")
  28.              << (QStringList() << "Bronwyn Hawcroft" << "119")
  29.              << (QStringList() << "Alessandro Hanssen" << "32")
  30.              << (QStringList() << "Andrew John Bakken" << "54")
  31.              << (QStringList() << "Vanessa Weatherley" << "85")
  32.              << (QStringList() << "Rebecca Dickens" << "17")
  33.              << (QStringList() << "David Bradley" << "42")
  34.              << (QStringList() << "Knut Walters" << "25")
  35.              << (QStringList() << "Andrea Jones" << "34");

  36.          foreach (QStringList row, rows) {
  37.              QList<QStandardItem *> items;
  38.              foreach (QString text, row)
  39.                  items.append(new QStandardItem(text));
  40.              model.appendRow(items);
  41.          }

  42.          resultView->setModel(&model);
  43.          resultView->verticalHeader()->hide();
  44.          resultView->horizontalHeader()->setStretchLastSection(true);


  45.     window.setWindowTitle(
  46.             QApplication::translate("nestedlayouts", "Nested layouts"));
  47.     window.show();
  48.     return app.exec();
  49. }
  50. /*
  51. QT学习的第一天, 代码都是QT助手上的。
  52. 在这里学到一个东西,匿名类对象,如 : QStringList() << "Verne Nilsen" << "123"
  53. 这里的foreach, 是QT中特有的一个C++方法,类似于C#中的foreach,具体使用,查看QT助手,这个助手很强大的说
  54. */
上一篇:升级了cublog..
下一篇:QT 2, Address Book