| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: layout.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of an example program for Qt. This example
|
|---|
| 7 | ** program may be used, distributed and modified without limitation.
|
|---|
| 8 | **
|
|---|
| 9 | *****************************************************************************/
|
|---|
| 10 |
|
|---|
| 11 | #include <qapplication.h>
|
|---|
| 12 | #include <qlabel.h>
|
|---|
| 13 | #include <qcolor.h>
|
|---|
| 14 | #include <qpushbutton.h>
|
|---|
| 15 | #include <qlayout.h>
|
|---|
| 16 | #include <qlineedit.h>
|
|---|
| 17 | #include <qmultilineedit.h>
|
|---|
| 18 | #include <qmenubar.h>
|
|---|
| 19 | #include <qpopupmenu.h>
|
|---|
| 20 |
|
|---|
| 21 | class ExampleWidget : public QWidget
|
|---|
| 22 | {
|
|---|
| 23 | public:
|
|---|
| 24 | ExampleWidget( QWidget *parent = 0, const char *name = 0 );
|
|---|
| 25 | ~ExampleWidget();
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | ExampleWidget::ExampleWidget( QWidget *parent, const char *name )
|
|---|
| 29 | : QWidget( parent, name )
|
|---|
| 30 | {
|
|---|
| 31 | // Make the top-level layout; a vertical box to contain all widgets
|
|---|
| 32 | // and sub-layouts.
|
|---|
| 33 | QBoxLayout *topLayout = new QVBoxLayout( this, 5 );
|
|---|
| 34 |
|
|---|
| 35 | // Create a menubar...
|
|---|
| 36 | QMenuBar *menubar = new QMenuBar( this );
|
|---|
| 37 | menubar->setSeparator( QMenuBar::InWindowsStyle );
|
|---|
| 38 | QPopupMenu* popup;
|
|---|
| 39 | popup = new QPopupMenu( this );
|
|---|
| 40 | popup->insertItem( "&Quit", qApp, SLOT(quit()) );
|
|---|
| 41 | menubar->insertItem( "&File", popup );
|
|---|
| 42 |
|
|---|
| 43 | // ...and tell the layout about it.
|
|---|
| 44 | topLayout->setMenuBar( menubar );
|
|---|
| 45 |
|
|---|
| 46 | // Make an hbox that will hold a row of buttons.
|
|---|
| 47 | QBoxLayout *buttons = new QHBoxLayout( topLayout );
|
|---|
| 48 | int i;
|
|---|
| 49 | for ( i = 1; i <= 4; i++ ) {
|
|---|
| 50 | QPushButton* but = new QPushButton( this );
|
|---|
| 51 | QString s;
|
|---|
| 52 | s.sprintf( "Button %d", i );
|
|---|
| 53 | but->setText( s );
|
|---|
| 54 |
|
|---|
| 55 | // Set horizontal stretch factor to 10 to let the buttons
|
|---|
| 56 | // stretch horizontally. The buttons will not stretch
|
|---|
| 57 | // vertically, since bigWidget below will take up vertical
|
|---|
| 58 | // stretch.
|
|---|
| 59 | buttons->addWidget( but, 10 );
|
|---|
| 60 | // (Actually, the result would have been the same with a
|
|---|
| 61 | // stretch factor of 0; if no items in a layout have non-zero
|
|---|
| 62 | // stretch, the space is divided equally between members.)
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | // Make another hbox that will hold a left-justified row of buttons.
|
|---|
| 66 | QBoxLayout *buttons2 = new QHBoxLayout( topLayout );
|
|---|
| 67 |
|
|---|
| 68 | QPushButton* but = new QPushButton( "Button five", this );
|
|---|
| 69 | buttons2->addWidget( but );
|
|---|
| 70 |
|
|---|
| 71 | but = new QPushButton( "Button 6", this );
|
|---|
| 72 | buttons2->addWidget( but );
|
|---|
| 73 |
|
|---|
| 74 | // Fill up the rest of the hbox with stretchable space, so that
|
|---|
| 75 | // the buttons get their minimum width and are pushed to the left.
|
|---|
| 76 | buttons2->addStretch( 10 );
|
|---|
| 77 |
|
|---|
| 78 | // Make a big widget that will grab all space in the middle.
|
|---|
| 79 | QMultiLineEdit *bigWidget = new QMultiLineEdit( this );
|
|---|
| 80 | bigWidget->setText( "This widget will get all the remaining space" );
|
|---|
| 81 | bigWidget->setFrameStyle( QFrame::Panel | QFrame::Plain );
|
|---|
| 82 |
|
|---|
| 83 | // Set vertical stretch factor to 10 to let the bigWidget stretch
|
|---|
| 84 | // vertically. It will stretch horizontally because there are no
|
|---|
| 85 | // widgets beside it to take up horizontal stretch.
|
|---|
| 86 | // topLayout->addWidget( bigWidget, 10 );
|
|---|
| 87 | topLayout->addWidget( bigWidget );
|
|---|
| 88 |
|
|---|
| 89 | // Make a grid that will hold a vertical table of QLabel/QLineEdit
|
|---|
| 90 | // pairs next to a large QMultiLineEdit.
|
|---|
| 91 |
|
|---|
| 92 | // Don't use hard-coded row/column numbers in QGridLayout, you'll
|
|---|
| 93 | // regret it when you have to change the layout.
|
|---|
| 94 | const int numRows = 3;
|
|---|
| 95 | const int labelCol = 0;
|
|---|
| 96 | const int linedCol = 1;
|
|---|
| 97 | const int multiCol = 2;
|
|---|
| 98 |
|
|---|
| 99 | // Let the grid-layout have a spacing of 10 pixels between
|
|---|
| 100 | // widgets, overriding the default from topLayout.
|
|---|
| 101 | QGridLayout *grid = new QGridLayout( topLayout, 0, 0, 10 );
|
|---|
| 102 | int row;
|
|---|
| 103 |
|
|---|
| 104 | for ( row = 0; row < numRows; row++ ) {
|
|---|
| 105 | QLineEdit *ed = new QLineEdit( this );
|
|---|
| 106 | // The line edit goes in the second column
|
|---|
| 107 | grid->addWidget( ed, row, linedCol );
|
|---|
| 108 |
|
|---|
| 109 | // Make a label that is a buddy of the line edit
|
|---|
| 110 | QString s;
|
|---|
| 111 | s.sprintf( "Line &%d", row+1 );
|
|---|
| 112 | QLabel *label = new QLabel( ed, s, this );
|
|---|
| 113 | // The label goes in the first column.
|
|---|
| 114 | grid->addWidget( label, row, labelCol );
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | // The multiline edit will cover the entire vertical range of the
|
|---|
| 118 | // grid (rows 0 to numRows) and stay in column 2.
|
|---|
| 119 |
|
|---|
| 120 | QMultiLineEdit *med = new QMultiLineEdit( this );
|
|---|
| 121 | grid->addMultiCellWidget( med, 0, -1, multiCol, multiCol );
|
|---|
| 122 |
|
|---|
| 123 | // The labels will take the space they need. Let the remaining
|
|---|
| 124 | // horizontal space be shared so that the multiline edit gets
|
|---|
| 125 | // twice as much as the line edit.
|
|---|
| 126 | grid->setColStretch( linedCol, 10 );
|
|---|
| 127 | grid->setColStretch( multiCol, 20 );
|
|---|
| 128 |
|
|---|
| 129 | // Add a widget at the bottom.
|
|---|
| 130 | QLabel* sb = new QLabel( this );
|
|---|
| 131 | sb->setText( "Let's pretend this is a status bar" );
|
|---|
| 132 | sb->setFrameStyle( QFrame::Panel | QFrame::Sunken );
|
|---|
| 133 | // This widget will use all horizontal space, and have a fixed height.
|
|---|
| 134 |
|
|---|
| 135 | // we should have made a subclass and implemented sizePolicy there...
|
|---|
| 136 | sb->setFixedHeight( sb->sizeHint().height() );
|
|---|
| 137 |
|
|---|
| 138 | sb->setAlignment( AlignVCenter | AlignLeft );
|
|---|
| 139 | topLayout->addWidget( sb );
|
|---|
| 140 |
|
|---|
| 141 | topLayout->activate();
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | ExampleWidget::~ExampleWidget()
|
|---|
| 145 | {
|
|---|
| 146 | // All child widgets are deleted by Qt.
|
|---|
| 147 | // The top-level layout and all its sub-layouts are deleted by Qt.
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | int main( int argc, char **argv )
|
|---|
| 151 | {
|
|---|
| 152 | QApplication a( argc, argv );
|
|---|
| 153 |
|
|---|
| 154 | ExampleWidget f;
|
|---|
| 155 | a.setMainWidget(&f);
|
|---|
| 156 | f.setCaption("Qt Example - Caption");
|
|---|
| 157 | f.show();
|
|---|
| 158 |
|
|---|
| 159 | return a.exec();
|
|---|
| 160 | }
|
|---|