QT QTableView 样式参考

先看效果:
在这里插入图片描述
mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStandardItemModel>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void init();

    void testData();

private:
    Ui::MainWindow *ui;

    QStandardItemModel *m_remoteDrivingDataModel;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QLabel>
#include <QPushButton>
#include <QDateTime>
#include <QFile>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    init();
    ui->tableView->setModel(m_remoteDrivingDataModel);
    testData();
    ui->tableView->setStyleSheet("QTableView {"
                                 "background-color: rgba(220, 221, 224, 220);"
                                 "color: rgb(90, 90, 90);"
                                 "font: 20px \"PingFang-SC-Regular\";"
                                 "padding: 0xp 10px 0px 10px;"
                                 "}"
                                 "QTableView QHeaderView::section {"
                                 "font: 20px \"PingFang-SC-Regular\";"
                                 "color: rgb(0,0,0);"
                                 "height:70px;"
                                 "background-color:rgb(240, 241, 245);"
                                 "border:0px;"
                                 "}"

                                 "QTableView QTableCornerButton::section {"
                                 "border:5px outset blue;"
                                 "border-bottom: 5px solid rgb(75, 120, 154);"
                                 "height:70px;"
                                 "}"

                                 "QTableView::item{"
                                 "border: none;"
                                 "background-color: rgba(220, 221, 224, 220);"
                                 "margin-top:15px;"
                                 "}"


                                 "QTableView::item:selected{"
                                 "color:white;"
                                 "border: none;"
                                 "}"

                                 "QLabel#left{"
                                 "border-top-left-radius: 10px;"
                                 "border-bottom-left-radius: 10px;"
                                 "font: 20px \"PingFang-SC-Regular\";"
                                 "color: rgb(90, 90, 90);"
                                 "background-color: white;"
                                 "qproperty-alignment: AlignCenter;"
                                 "}"

                                 "QLabel#right{"
                                 "border-top-right-radius: 10px;"
                                 "border-bottom-right-radius: 10px;"
                                 "font: 20px \"PingFang-SC-Regular\";"
                                 "color: rgb(90, 90, 90);"
                                 "background-color: white;"
                                 "qproperty-alignment: AlignCenter;"
                                 "}"

                                 "QLabel#mid{"
                                 "font: 20px \"PingFang-SC-Regular\";"
                                 "color: rgb(90, 90, 90);"
                                 "background-color: white;"
                                 "qproperty-alignment: AlignCenter;"
                                 "}"

                                 "QWidget#pushBtnWidget{"
                                 "background-color: white;"
                                 "}"

                                 "QPushButton#pushbutton{"
                                 "border-radius:20px;"
                                 "background-color:  rgb(131, 139, 152);"
                                 "font: 20px \"Microsoft Yahei\";"
                                 "color: rgb(255, 255, 255);"
                                 "margin: 0px 10px 0px 10px;"
                                 "}"

                                 "QPushButton#pushbutton:pressed {"
                                 "border-radius:20px;"
                                 "background-color:  rgb(101,108,104);"
                                 "font: 20px \"Microsoft Yahei\";"
                                 "color: rgb(255, 255, 255);"
                                 "}"
                                 );

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    ui->tableView->horizontalHeader()->setStretchLastSection(true);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableView->verticalHeader()->setVisible(false);
    ui->tableView->setEditTriggers(QTableView::NoEditTriggers);
    ui->tableView->setShowGrid(false);//隐藏格子的网格线
    ui->tableView->horizontalHeader()->setHighlightSections(false);//表头字体不加粗
    ui->tableView->setFocusPolicy(Qt::NoFocus);
    ui->tableView->setAlternatingRowColors(false);
    m_remoteDrivingDataModel = new QStandardItemModel(ui->tableView);
    m_remoteDrivingDataModel->clear();
    m_remoteDrivingDataModel->setHorizontalHeaderLabels(QStringList() << QStringLiteral("head1") << QStringLiteral("head2") << QStringLiteral("head3")
                                                        << QStringLiteral("head4") << QStringLiteral("head5") << QStringLiteral("head6")
                                                        << QStringLiteral("head7") << QStringLiteral("head8") << QStringLiteral("head9"));
}

void MainWindow::testData()
{
    for (int i=0; i < 7; i++) {
        for(int j=0;j<9;j++){
            m_remoteDrivingDataModel->setItem(i,j,new QStandardItem(""));
            if(j==0){
                QLabel *label = new QLabel(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
                label->setObjectName("left");
                ui->tableView->setIndexWidget(m_remoteDrivingDataModel->index(m_remoteDrivingDataModel->rowCount()-1,0),label);
            }
            else if(j == 2){
                QWidget *pushBtnWidget = new QWidget;
                pushBtnWidget->setObjectName("pushBtnWidget");
                QPushButton *pushbutton = new QPushButton("123",pushBtnWidget);
                pushbutton->setObjectName("pushbutton");
                pushbutton->setMinimumSize(QSize(140,40));

                ui->tableView->setIndexWidget(m_remoteDrivingDataModel->index(m_remoteDrivingDataModel->rowCount()-1,2),pushBtnWidget);
                pushbutton->setGeometry((pushBtnWidget->width()-pushbutton->width())/2,12,140,40);


            }
            else if(j == 8){
                QLabel *label = new QLabel(tr("333"));
                label->setObjectName("right");
                ui->tableView->setIndexWidget(m_remoteDrivingDataModel->index(m_remoteDrivingDataModel->rowCount()-1,8),label);
            }
            else{
                QLabel *label = new QLabel("555");
                label->setObjectName("mid");
                ui->tableView->setIndexWidget(m_remoteDrivingDataModel->index(m_remoteDrivingDataModel->rowCount()-1,j),label);
            }
        }
    }
}


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值