您的位置:

Qttablewidget使用指南

一、qtablewidget用法

Qttablewidget是一个基于QAbstractItemView的类,旨在提供一种可编辑的表格式数据显示。它可以存储、分类、编辑和显示大量的数据。这里简单总结一下Qttablewidget的用法。

1、创建一个Qttablewidget,设置它的行数、列数:

QTableWidget *tableWidget = new QTableWidget();
tableWidget->setRowCount(3);
tableWidget->setColumnCount(4);

2、设置表格中的内容:

tableWidget->setItem(0,0,new QTableWidgetItem("Row0, Column0"));
tableWidget->setItem(0,1,new QTableWidgetItem("Row0, Column1"));
tableWidget->setItem(0,2,new QTableWidgetItem("Row0, Column2"));
tableWidget->setItem(0,3,new QTableWidgetItem("Row0, Column3"));

3、获取表格中的内容:

QString text = tableWidget->item(0,0)->text();

二、qtablewidget样式表

Qttablewidget可以通过设置样式表来改变它的外观,具体可以设置的属性如下,包括行高、列宽、字体颜色、背景颜色、网格线颜色等:

QTableWidget {
  background-color: rgb(255, 255, 255);
}
QTableWidget::item {
  border: 1px solid gray;
}
QTableWidget::item:selected {
  background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                              stop: 0 #FF92BB, stop: 1 #7B007B);
  color: white;
}

三、设置qtablewidget

除了基本的用法,Qttablewidget还可以进行更高级的操作,如设置列宽、行高、单元格格式,以及启用编辑模式等。

1、设置列宽、行高:

tableWidget->setColumnWidth(0, 100);
tableWidget->setRowHeight(0, 50);

2、设置单元格格式:

tableWidget->setStyleSheet("QTableView {"
                             "selection-background-color: green;"
                          "}");

3、启用编辑模式:

tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);

四、qttablewidget排序

Qttablewidget可以通过实现自定义代理来对表格进行排序,并且支持多列排序。

在使用时,需要定义一个自定义代理类,并重载QSortFilterProxyModel类中的lessThan函数,如下:

class CustomSortProxyModel : public QSortFilterProxyModel
{
public:
    explicit CustomSortProxyModel(QObject* parent = 0);

    virtual bool lessThan(const QModelIndex& left, const QModelIndex& right) const;
};

bool CustomSortProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
{
    QVariant leftData = sourceModel()->data(left);
    QVariant rightData = sourceModel()->data(right);

    if(leftData.type() == QVariant::Int)
    {
        return leftData.toInt() < rightData.toInt();
    }

    return QSortFilterProxyModel::lessThan(left, right);
}

QTableWidget* tableWidget = new QTableWidget(this);
CustomSortProxyModel* sortModel = new CustomSortProxyModel(this);
sortModel->setSourceModel(tableWidget->model());
tableWidget->setModel(sortModel); 

五、qt的tabwidget选取

当我们在Qt中使用QTabWidget时,可以通过以下方法来设置tablable中被选中页的样式:

QTabWidget::tab:selected {
    background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
                                stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
}

六、完整代码示例

#include 
#include 
   
#include 
    
#include 
     
#include 
      
#include 
       
        class CustomSortProxyModel : public QSortFilterProxyModel { public: explicit CustomSortProxyModel(QObject* parent = 0); virtual bool lessThan(const QModelIndex& left, const QModelIndex& right) const; }; CustomSortProxyModel::CustomSortProxyModel(QObject* parent) : QSortFilterProxyModel(parent) { } bool CustomSortProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const { QVariant leftData = sourceModel()->data(left); QVariant rightData = sourceModel()->data(right); if(leftData.type() == QVariant::Int) { return leftData.toInt() < rightData.toInt(); } return QSortFilterProxyModel::lessThan(left, right); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QTableWidget *tableWidget = new QTableWidget(); tableWidget->setRowCount(3); tableWidget->setColumnCount(4); tableWidget->setItem(0,0,new QTableWidgetItem("Row0, Column0")); tableWidget->setItem(0,1,new QTableWidgetItem("Row0, Column1")); tableWidget->setItem(0,2,new QTableWidgetItem("Row0, Column2")); tableWidget->setItem(0,3,new QTableWidgetItem("Row0, Column3")); tableWidget->setColumnWidth(0, 100); tableWidget->setRowHeight(0, 50); tableWidget->setStyleSheet("QTableView {" "selection-background-color: green;" "}"); tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers); tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); CustomSortProxyModel* sortModel = new CustomSortProxyModel(); sortModel->setSourceModel(tableWidget->model()); tableWidget->setModel(sortModel); QDesktopWidget* dDesk = new QDesktopWidget; int nH = dDesk->height() / 2, nW = dDesk->width() / 2; tableWidget->setGeometry(nW - 300, nH - 200, 600, 400); tableWidget->verticalHeader()->hide(); tableWidget->show(); return a.exec(); }