先看效果:
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);
}
}
}
}