Widgets Tutorial - Nested Layouts▲
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.
We achieve this by creating two layouts: queryLayout is a QHBoxLayout that contains QLabel and QLineEdit widgets placed side-by-side; mainLayout is a QVBoxLayout that contains queryLayout and a QTableView arranged vertically.
Sélectionnez
|
|
Note that we call the mainLayout's addLayout() function to insert the queryLayout above the resultView table.
We have omitted the code that sets up the model containing the data shown by the QTableView widget, resultView. For completeness, we show this below.
As well as QHBoxLayout and QVBoxLayout, Qt also provides QGridLayout and QFormLayout classes to help with more complex user interfaces. These can be seen if you run Qt Designer.
Setting up the Model▲
In the code above, we did not show where the table's data came from because we wanted to concentrate on the use of layouts. Here, we see that the model holds a number of items corresponding to rows, each of which is set up to contain data for two columns.
QStandardItemModel model;
model.setHorizontalHeaderLabels(
QStringList() &
lt;&
lt; QApplication::
translate("nestedlayouts"
, "Name"
)
&
lt;&
lt; QApplication::
translate("nestedlayouts"
, "Office"
));
QList&
lt;QStringList&
gt; rows =
QList&
lt;QStringList&
gt;()
&
lt;&
lt; (QStringList() &
lt;&
lt; "Verne Nilsen"
&
lt;&
lt; "123"
)
&
lt;&
lt; (QStringList() &
lt;&
lt; "Carlos Tang"
&
lt;&
lt; "77"
)
&
lt;&
lt; (QStringList() &
lt;&
lt; "Bronwyn Hawcroft"
&
lt;&
lt; "119"
)
&
lt;&
lt; (QStringList() &
lt;&
lt; "Alessandro Hanssen"
&
lt;&
lt; "32"
)
&
lt;&
lt; (QStringList() &
lt;&
lt; "Andrew John Bakken"
&
lt;&
lt; "54"
)
&
lt;&
lt; (QStringList() &
lt;&
lt; "Vanessa Weatherley"
&
lt;&
lt; "85"
)
&
lt;