PageRenderTime 124ms CodeModel.GetById 50ms RepoModel.GetById 0ms app.codeStats 0ms

/local/samples+demos/anchorlayout/main.cpp

http://mingw-lib.googlecode.com/
C++ | 129 lines | 67 code | 20 blank | 42 comment | 0 complexity | 0f2c77e1c2746e4d22a99f0dbc45d531 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the examples of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** Commercial Usage
  11. ** Licensees holding valid Qt Commercial licenses may use this file in
  12. ** accordance with the Qt Commercial License Agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and Nokia.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 as published by the Free Software
  19. ** Foundation and appearing in the file LICENSE.LGPL included in the
  20. ** packaging of this file. Please review the following information to
  21. ** ensure the GNU Lesser General Public License version 2.1 requirements
  22. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  23. **
  24. ** In addition, as a special exception, Nokia gives you certain additional
  25. ** rights. These rights are described in the Nokia Qt LGPL Exception
  26. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  27. **
  28. ** GNU General Public License Usage
  29. ** Alternatively, this file may be used under the terms of the GNU
  30. ** General Public License version 3.0 as published by the Free Software
  31. ** Foundation and appearing in the file LICENSE.GPL included in the
  32. ** packaging of this file. Please review the following information to
  33. ** ensure the GNU General Public License version 3.0 requirements will be
  34. ** met: http://www.gnu.org/copyleft/gpl.html.
  35. **
  36. ** If you have questions regarding the use of this file, please contact
  37. ** Nokia at qt-info@nokia.com.
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <QGraphicsWidget>
  42. #include <QGraphicsProxyWidget>
  43. #include <QGraphicsAnchorLayout>
  44. #include <QtGui>
  45. static QGraphicsProxyWidget *createItem(const QSizeF &minimum = QSizeF(100.0, 100.0),
  46. const QSizeF &preferred = QSize(150.0, 100.0),
  47. const QSizeF &maximum = QSizeF(200.0, 100.0),
  48. const QString &name = "0")
  49. {
  50. QGraphicsProxyWidget *w = new QGraphicsProxyWidget;
  51. w->setWidget(new QPushButton(name));
  52. w->setData(0, name);
  53. w->setMinimumSize(minimum);
  54. w->setPreferredSize(preferred);
  55. w->setMaximumSize(maximum);
  56. w->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  57. return w;
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. QApplication app(argc, argv);
  62. QGraphicsScene scene;
  63. scene.setSceneRect(0, 0, 800, 480);
  64. QSizeF minSize(30, 100);
  65. QSizeF prefSize(210, 100);
  66. QSizeF maxSize(300, 100);
  67. QGraphicsProxyWidget *a = createItem(minSize, prefSize, maxSize, "A");
  68. QGraphicsProxyWidget *b = createItem(minSize, prefSize, maxSize, "B");
  69. QGraphicsProxyWidget *c = createItem(minSize, prefSize, maxSize, "C");
  70. QGraphicsProxyWidget *d = createItem(minSize, prefSize, maxSize, "D");
  71. QGraphicsProxyWidget *e = createItem(minSize, prefSize, maxSize, "E");
  72. QGraphicsProxyWidget *f = createItem(QSizeF(30, 50), QSizeF(150, 50), maxSize, "F (overflow)");
  73. QGraphicsProxyWidget *g = createItem(QSizeF(30, 50), QSizeF(30, 100), maxSize, "G (overflow)");
  74. QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout;
  75. l->setSpacing(0);
  76. QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window);
  77. w->setPos(20, 20);
  78. w->setLayout(l);
  79. // vertical
  80. QGraphicsAnchor *anchor = l->addAnchor(a, Qt::AnchorTop, l, Qt::AnchorTop);
  81. anchor = l->addAnchor(b, Qt::AnchorTop, l, Qt::AnchorTop);
  82. anchor = l->addAnchor(c, Qt::AnchorTop, a, Qt::AnchorBottom);
  83. anchor = l->addAnchor(c, Qt::AnchorTop, b, Qt::AnchorBottom);
  84. anchor = l->addAnchor(c, Qt::AnchorBottom, d, Qt::AnchorTop);
  85. anchor = l->addAnchor(c, Qt::AnchorBottom, e, Qt::AnchorTop);
  86. anchor = l->addAnchor(d, Qt::AnchorBottom, l, Qt::AnchorBottom);
  87. anchor = l->addAnchor(e, Qt::AnchorBottom, l, Qt::AnchorBottom);
  88. anchor = l->addAnchor(c, Qt::AnchorTop, f, Qt::AnchorTop);
  89. anchor = l->addAnchor(c, Qt::AnchorVerticalCenter, f, Qt::AnchorBottom);
  90. anchor = l->addAnchor(f, Qt::AnchorBottom, g, Qt::AnchorTop);
  91. anchor = l->addAnchor(c, Qt::AnchorBottom, g, Qt::AnchorBottom);
  92. // horizontal
  93. anchor = l->addAnchor(l, Qt::AnchorLeft, a, Qt::AnchorLeft);
  94. anchor = l->addAnchor(l, Qt::AnchorLeft, d, Qt::AnchorLeft);
  95. anchor = l->addAnchor(a, Qt::AnchorRight, b, Qt::AnchorLeft);
  96. anchor = l->addAnchor(a, Qt::AnchorRight, c, Qt::AnchorLeft);
  97. anchor = l->addAnchor(c, Qt::AnchorRight, e, Qt::AnchorLeft);
  98. anchor = l->addAnchor(b, Qt::AnchorRight, l, Qt::AnchorRight);
  99. anchor = l->addAnchor(e, Qt::AnchorRight, l, Qt::AnchorRight);
  100. anchor = l->addAnchor(d, Qt::AnchorRight, e, Qt::AnchorLeft);
  101. anchor = l->addAnchor(l, Qt::AnchorLeft, f, Qt::AnchorLeft);
  102. anchor = l->addAnchor(l, Qt::AnchorLeft, g, Qt::AnchorLeft);
  103. anchor = l->addAnchor(f, Qt::AnchorRight, g, Qt::AnchorRight);
  104. scene.addItem(w);
  105. scene.setBackgroundBrush(Qt::darkGreen);
  106. QGraphicsView view(&scene);
  107. view.show();
  108. return app.exec();
  109. }