Tutorial sui widget - Layout annidati
Proprio come i widget possono contenere altri widget, i layout possono essere utilizzati per fornire diversi livelli di raggruppamento dei widget. In questo caso, vogliamo visualizzare un'etichetta accanto a un campo di immissione testo nella parte superiore di una finestra, sopra una vista tabella che mostra i risultati di una query.
Otteniamo questo risultato creando due layout: queryLayout è un QHBoxLayout che contiene i widget QLabel e QLineEdit disposti fianco a fianco; mainLayout è un QVBoxLayout che contiene queryLayout e un QTableView disposti verticalmente.
| ![]() |
Si noti che chiamiamo la funzione addLayout() del widget mainLayout per inserire il widget queryLayout sopra la tabella resultView.
Abbiamo omesso il codice che imposta il modello contenente i dati visualizzati dal widget QTableView, resultView. Per completezza, lo riportiamo di seguito.
Oltre a QHBoxLayout e QVBoxLayout, Qt fornisce anche le classi QGridLayout e QFormLayout per facilitare la creazione di interfacce utente più complesse. È possibile visualizzarle eseguendo Qt Widgets Designer.
Configurazione del modello
Nel codice sopra riportato non abbiamo indicato la provenienza dei dati della tabella perché volevamo concentrarci sull’uso dei layout. Qui vediamo che il modello contiene un numero di elementi corrispondente alle righe, ciascuno dei quali è configurato per contenere i dati di due colonne.
QStandardItemModel model;
model.setHorizontalHeaderLabels({ QApplication::translate("nestedlayouts", "Name"),
QApplication::translate("nestedlayouts", "Office") });
const QStringList rows[] = {
QStringList{ QStringLiteral("Verne Nilsen"), QStringLiteral("123") },
QStringList{ QStringLiteral("Carlos Tang"), QStringLiteral("77") },
QStringList{ QStringLiteral("Bronwyn Hawcroft"), QStringLiteral("119") },
QStringList{ QStringLiteral("Alessandro Hanssen"), QStringLiteral("32") },
QStringList{ QStringLiteral("Andrew John Bakken"), QStringLiteral("54") },
QStringList{ QStringLiteral("Vanessa Weatherley"), QStringLiteral("85") },
QStringList{ QStringLiteral("Rebecca Dickens"), QStringLiteral("17") },
QStringList{ QStringLiteral("David Bradley"), QStringLiteral("42") },
QStringList{ QStringLiteral("Knut Walters"), QStringLiteral("25") },
QStringList{ QStringLiteral("Andrea Jones"), QStringLiteral("34") }
};
QList<QStandardItem *> items;
for (const QStringList &row : rows) {
items.clear();
for (const QString &text : row)
items.append(new QStandardItem(text));
model.appendRow(items);
}
resultView->setModel(&model);
resultView->verticalHeader()->hide();
resultView->horizontalHeader()->setStretchLastSection(true);L’uso di modelli e viste è trattato negli Esempi di viste degli elementi e nella panoramica sulla programmazione Modello/Vista.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
