QT 使用QtableWidget创建表格,多处使用,封装类调用

QT  使用QtableWidget 写表格 ,多个地方调用相同样式的表格,不想每个地方都写一遍配置,简单封装起来 

效果 

QT 使用QtableWidget创建表格,多处使用,封装类调用_第1张图片

 设计QT 使用QtableWidget创建表格,多处使用,封装类调用_第2张图片

注意 要在 表格外加一层布局

ui->verticalLayout_4->addWidget(myTableWidget);

如果不创建 verticalLayout_4 直接在 ui->widget_3 里创建表格, myTableWidget = new TableWidget(ui->widget_3);    表格不会铺满

QT 使用QtableWidget创建表格,多处使用,封装类调用_第3张图片

 表格类:

#include "tablewidget.h"
#include 
#include 
#include 
#include 

TableWidget::TableWidget(QWidget *parent) :
    QTableWidget(parent)
{

//    tableWidget = new QTableWidget(parent);
//    tableWidget->setRowCount(10);   //设置行数为10
//    tableWidget->setColumnCount(5); //设置列数为5
    this->setFont(QFont("微软雅黑",12));


    this->setShowGrid(false);    //不显示网格线
    this->setSelectionBehavior(QAbstractItemView::SelectRows);   //设置整行选择
    this->setMouseTracking(true);    //开启捕获鼠标功能
    this->setEditTriggers(QAbstractItemView::NoEditTriggers);    //禁止编辑表格
    this->verticalHeader()->setVisible(false);//纵向表头可视化
    this->horizontalHeader()->setStyleSheet("QHeaderView::section {""background-color:#383838;}");
    this->setStyleSheet("background-color:#1a1a1a;color:#fff;gridline-color: rgb(0, 0, 0)");//设置当前颜色
//    this->horizontalHeader()->setDefaultSectionSize(80);  //设置默认宽度
    this->verticalHeader()->setDefaultSectionSize(30);   //设置一行默认高度

//    this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//自适应所有列,让它布满空间。设置了就列就不能拉伸
//    this->horizontalHeader()->resizeSections(QHeaderView::Stretch);//列可以拉伸

    this->horizontalHeader()->setStretchLastSection(true);//最后一列铺满

    this->setAlternatingRowColors(true);// 设置隔行背景色
    this->setPalette(QPalette(QColor(70,70,70)));//参数还可写成QPalette(Qt::blue) QPalette(QColor(36,36,36))
    this->setStyleSheet("selection-background-color:#0e4b72");  //设置选中行颜色


//    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);//设置水平滚动条
//    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);//设置垂直滚动条

}

void TableWidget::setRowAndCol(int row, int col) {
        this->setRowCount(row);   //设置行数为10
        this->setColumnCount(col); //设置列数为5
}

#ifndef TABLEWIDGET_H
#define TABLEWIDGET_H

#include 

class TableWidget : public QTableWidget
{
    Q_OBJECT
public:
    explicit TableWidget(QWidget *parent = nullptr);

    void setRowAndCol(int row, int col);


public:
    QTableWidget *tableWidget;

private:
    int row_;
    int col_;
};

#endif // TABLEWIDGET_H

调用: 

#include "screenmanagement.h"
#include "ui_screenmanagement.h"
#include 
#include 

ScreenManagement::ScreenManagement(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ScreenManagement)
{
    ui->setupUi(this);

    myTableWidget = new TableWidget();
    myTableWidget->setRowAndCol(3, 5);
    //设置表格中每一行的表头
    QStringList header;
    header<<"1"<<"2"<<"3"<<"4"<<"5";
    myTableWidget->setHorizontalHeaderLabels(header);


    for (int i=0; i<3; i++)
        for (int j=0; j<5;j++)
        {
            QTableWidgetItem *item = new QTableWidgetItem(QIcon(":/image/search.png"), "td");
            myTableWidget->setItem(i,j,item);
        }

    ui->verticalLayout_4->addWidget(myTableWidget);

}

ScreenManagement::~ScreenManagement()
{
    delete ui;
}
#ifndef SCREENMANAGEMENT_H
#define SCREENMANAGEMENT_H
#include "res_base/tablewidget.h"
#include 
#include 

namespace Ui {
class ScreenManagement;
}

class ScreenManagement : public QWidget
{
    Q_OBJECT

public:
    explicit ScreenManagement(QWidget *parent = nullptr);
    ~ScreenManagement();


private:
    Ui::ScreenManagement *ui;
    TableWidget *myTableWidget;
};

#endif // SCREENMANAGEMENT_H

你可能感兴趣的:(qt,qt)