QTableWidget是一个显示结果类似一个表格,下面是它的简单使用。
main.cpp文件如下:
#include <QtGui/QApplication> #include <QTextCodec> #include <QTableWidget> #include <QWidget> #include <QVBoxLayout> int main(int argc, char *argv[]) { QApplication a(argc, argv); QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030")); QTableWidget *table = new QTableWidget; table->setRowCount(3); table->setColumnCount(3); QStringList horizontalHeader; horizontalHeader<<QObject::tr("年龄")<<QObject::tr("身高")<<QObject::tr("体重"); QStringList verticalHeader; verticalHeader<<QObject::tr("张三")<<QObject::tr("李四")<<QObject::tr("王五"); table->setHorizontalHeaderLabels(horizontalHeader); table->setVerticalHeaderLabels(verticalHeader); table->setItem(0, 0, new QTableWidgetItem(QString("23"))); table->setItem(0, 1, new QTableWidgetItem(QString("177cm"))); table->setItem(0, 2, new QTableWidgetItem(QString("60kg"))); table->setItem(1, 0, new QTableWidgetItem(QString("25"))); table->setItem(1, 1, new QTableWidgetItem(QString("180cm"))); table->setItem(1, 2, new QTableWidgetItem(QString("70kg"))); table->setItem(2, 0, new QTableWidgetItem(QString("29"))); table->setItem(2, 1, new QTableWidgetItem(QString("172cm"))); table->setItem(2, 2, new QTableWidgetItem(QString("65kg"))); QWidget *widget = new QWidget; QVBoxLayout *mainlayout = new QVBoxLayout; mainlayout->addWidget(table); widget->setLayout(mainlayout); widget->resize(400, 200); widget->setWindowTitle(QObject::tr("QTableWidget Demo")); widget->show(); return a.exec(); }
运行效果: