#include #include #include #include #include #include #include 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( "" + QString::number( i + 1 ) + "" ); 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(); }