QTableWidget

QTableWidget為一個表格樣式的清單,表格中每個儲存格都是一個QTableWidgetItem的實例,我們自行設定每個QTableWidgetItem的值,也可以像QListWidgetItem一樣插入QIcon。


widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTableWidget>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
private:
    QTableWidget *myTableWidget;

};

#endif 

widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{
    setWindowTitle(tr("QTableWidget"));
    myTableWidget = new QTableWidget(this);
    myTableWidget->setRowCount(4);     //設定QTableWidget列數
    myTableWidget->setColumnCount(3);  //設定QTableWidget行數
    myTableWidget->resize(250, 200);

    QStringList header;
    header << tr("名字") << tr("分數");
    myTableWidget->setHorizontalHeaderLabels(header); //設定QTableWidget標頭

    myTableWidget->setItem(0, 0, new QTableWidgetItem("peter"));
    myTableWidget->setItem(1, 0, new QTableWidgetItem("bill"));
    myTableWidget->setItem(0, 1, new QTableWidgetItem(QIcon("good.jpg"), "良好"));
    myTableWidget->setItem(1, 1, new QTableWidgetItem(QIcon("normal.jpg"), "普通"));
}

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

QTableWidget