QTableWidget
ist zwar kein Layout, mit dieser Klasse sich Daten aber recht praktisch in einer Tabelle darstellen. Das besondere an diesem Widget ist, dass neben Text auch Widgets dargestellt werden können.
Bevor wir die Tabelle noch mit Daten füllen, wollen wir sie erstmal anzeigen:
#include <QApplication> #include <QTableWidget> #include <QHeaderView> int main( int argc, char *argv[] ) { QApplication app( argc, argv ); QTableWidget table; // Größe festlegen table.setRowCount( 5 ); table.setColumnCount( 5 ); // Die Größe der Zellen in der Tabelle hängt vom Inhalt ab table.resizeRowsToContents(); table.resizeColumnsToContents(); // Die Tabelle soll vollständig dargestellt werden, ohne eine // Scrollbar anzuzeigen. // https://stackoverflow.com/a/37615363 // https://stackoverflow.com/a/40300974 table.setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ); table.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setFixedSize( table.horizontalHeader()->length() + table.verticalHeader()->width() + table.frameWidth() * 2, table.verticalHeader()->length() + table.horizontalHeader()->height() + table.frameWidth() * 2 ); // Titel setzen und Widget anzeigen table.setWindowTitle( "TableWidget" ); table.show(); return app.exec(); }
Im obigen Beispiel sehen wir, dass am oberen und linken Rand automatisch Beschriftungen verwendet werden. Diese sind nicht schön, aber können leicht modifiziert werden:
#include <QApplication> #include <QTableWidget> #include <QHeaderView> int main( int argc, char *argv[] ) { QApplication app( argc, argv ); QTableWidget table; // Größe festlegen table.setRowCount( 5 ); table.setColumnCount( 5 ); // Labels setzen QStringList hLabels, vLabels; hLabels << "H1" << "H2" << "H3" << "H4" << "H5"; vLabels << "V1" << "V2" << "V3" << "V4" << "V5"; table.setHorizontalHeaderLabels( hLabels ); table.setVerticalHeaderLabels( vLabels ); // Die Größe der Zellen in der Tabelle hängt vom Inhalt ab table.resizeRowsToContents(); table.resizeColumnsToContents(); // Die Tabelle soll vollständig angezeigt werden, ohne eine // Scrollbar anzuzeigen. // https://stackoverflow.com/a/37615363 // https://stackoverflow.com/a/40300974 table.setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ); table.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setFixedSize( table.horizontalHeader()->length() + table.verticalHeader()->width() + table.frameWidth() * 2, table.verticalHeader()->length() + table.horizontalHeader()->height() + table.frameWidth() * 2 ); // Titel setzen und Widget anzeigen table.setWindowTitle( "TableWidget" ); table.show(); return app.exec(); }
Wir können diese Balken natürlich auch komplett ausblenden:
#include <QApplication> #include <QTableWidget> #include <QHeaderView> int main( int argc, char *argv[] ) { QApplication app( argc, argv ); QTableWidget table; // Größe festlegen table.setRowCount( 5 ); table.setColumnCount( 5 ); // Balken verstecken table.horizontalHeader()->setVisible( false ); table.verticalHeader()->setVisible( false ); // Die Größe der Zellen in der Tabelle hängt vom Inhalt ab table.resizeRowsToContents(); table.resizeColumnsToContents(); // Die Tabelle soll vollständig angezeigt werden, ohne eine // Scrollbar anzuzeigen. // https://stackoverflow.com/a/37615363 // https://stackoverflow.com/a/40300974 table.setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ); table.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setFixedSize( table.horizontalHeader()->length() + table.frameWidth() * 2, table.verticalHeader()->length() + table.frameWidth() * 2 ); // Titel setzen und Widget anzeigen table.setWindowTitle( "TableWidget" ); table.show(); return app.exec(); }
Als nächstes wollen wir Text in die Zellen einfügen. Wir machen es uns einfach und schreiben als Ergebnis einfach das Produkt aus Zeilen- und Spaltenindex in die Zelle. Um Text in eine Zelle einzufügen, muss ein QTableWidgetItem
über die Methode setItem()
gesetzt werden.
#include <QApplication> #include <QTableWidget> #include <QHeaderView> int main( int argc, char *argv[] ) { QApplication app( argc, argv ); QTableWidget table; // Größe festlegen table.setRowCount( 5 ); table.setColumnCount( 5 ); // Zellen mit Produkten als Text füllen for( int i = 0; i < table.rowCount(); i++ ) { for( int j = 0; j < table.columnCount(); j++ ) table.setItem( i, j, new QTableWidgetItem( QString::number( ( i + 1 ) * ( j + 1) ) ) ); } // Die Größe der Zellen in der Tabelle hängt vom Inhalt ab table.resizeRowsToContents(); table.resizeColumnsToContents(); // Die Tabelle soll vollständig angezeigt werden, ohne eine // Scrollbar anzuzeigen. // https://stackoverflow.com/a/37615363 // https://stackoverflow.com/a/40300974 table.setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ); table.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setFixedSize( table.horizontalHeader()->length() + table.verticalHeader()->width() + table.frameWidth() * 2, table.verticalHeader()->length() + table.horizontalHeader()->height() + table.frameWidth() * 2 ); // Titel setzen und Widget anzeigen table.setWindowTitle( "TableWidget" ); table.show(); return app.exec(); }
Neben Text kann eine Tabelle auch Widgets enthalten, die darin ganz normal dargestellt werden. Ähnlich wie mit setItem()
, wird über setCellWidget()
das Widget einer Zelle gesetzt. Natürlich kann man das innerhalb einer Tabelle mischen, wobei eine Zelle logischerweise nur einen Text bzw. ein Widget beinhalten kann. Als Beispiel konstruieren wir folgende Tabelle:
QSpinBox
mit Zeilennummer als WertCode:
#include <QApplication> #include <QTableWidget> #include <QHeaderView> #include <QPushButton> #include <QProgressBar> #include <QSpinBox> #include <QLabel> int main( int argc, char *argv[] ) { QApplication app( argc, argv ); QTableWidget table; // Größe festlegen table.setRowCount( 5 ); table.setColumnCount( 5 ); // 1. Spalte: Zeilennummer als Text for( int i = 0; i < table.rowCount(); i++ ) table.setItem( i, 0, new QTableWidgetItem( QString::number( i + 1 ) ) ); // 2. Spalte: Zeilennummer als Button for( int i = 0; i < table.rowCount(); i++ ) table.setCellWidget( i, 1, new QPushButton( QString::number( i + 1 ) ) ); // 3. Spalte: Zeilennummer * 10 als Fortschrittsbalken for( int i = 0; i < table.rowCount(); i++ ) { QProgressBar *pBar = new QProgressBar(); pBar->setValue( ( i + 1 ) * 10 ); table.setCellWidget( i, 2, pBar ); } // 4. Spalte: ''QSpinBox'' mit Zeilennummer als Wert for( int i = 0; i < table.rowCount(); i++ ) { QSpinBox *spinBox = new QSpinBox(); spinBox->setMinimum( 0 ); spinBox->setMaximum( 100 ); spinBox->setValue( i + 1 ); table.setCellWidget( i, 3, spinBox ); } // 5. Spalte: Label mit Zeilennummer als fetter, grüner Text for( int i = 0; i < table.rowCount(); i++ ) { QLabel *label = new QLabel(); label->setText( "<b><font color=\"lightgreen\">" + QString::number( i + 1 ) + "</font></b>" ); table.setCellWidget( i, 4, label ); } // Die Größe der Zellen in der Tabelle hängt vom Inhalt ab table.resizeRowsToContents(); table.resizeColumnsToContents(); // Die Tabelle soll vollständig angezeigt werden, ohne eine // Scrollbar anzuzeigen. // https://stackoverflow.com/a/37615363 // https://stackoverflow.com/a/40300974 table.setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ); table.setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); table.setFixedSize( table.horizontalHeader()->length() + table.verticalHeader()->width() + table.frameWidth() * 2, table.verticalHeader()->length() + table.horizontalHeader()->height() + table.frameWidth() * 2 ); // Titel setzen und Widget anzeigen table.setWindowTitle( "TableWidget" ); table.show(); return app.exec(); }
Damit haben wir auch schon die wichtigsten Funktionalitäten von Tabellen geklärt. QTableWidget
ist zwar kein Layout, trotzdem eine sehr mächtige Klasse zur Darstellung von Daten.