前言
tableWidget用于数据显示,继承tableView,想要了解更多关于tableWidget及、tableView。点击qtableview和qtablewidget区别
例子
这里以一个LPL春季赛战队排名数据为例,只搞了前10,可以点击表头排序
关键代码
maindialog.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidget>
#include <QHeaderView>
#include <QDebug>
#include <QStyle>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("LPL春季赛战队积分(2020/3/5)");
QTableWidget *tableWidget = new QTableWidget(10,4,this);
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
connect(tableWidget->horizontalHeader(),&QHeaderView::sectionClicked,this,[=](int column){
if(m_ListBl.at(column)){
tableWidget->sortByColumn(column,Qt::AscendingOrder);
}else{
tableWidget->sortByColumn(column,Qt::DescendingOrder);
}
tableWidget->setSortingEnabled(true);
m_ListBl[column]=!m_ListBl[column];
});
tableWidget->setWindowTitle("QTableWidget & Item");
tableWidget->resize(size());
QStringList header;
header<<"排名"<<"战队"<<"胜率"<<"积分";
tableWidget->setHorizontalHeaderLabels(header);
for (int i=0;i<4;i++) {
m_ListBl.append(false);
}
QStringList listName;
listName<<"WBG"<<"V5"<<"EDG"<<"RNG"<<"LNG"
<<"TES"<<"JDG"<<"BLG"<<"RA"<<"OMG";
QList<int> listScore;
listScore<<9<<9<<8<<8<<8
<<7<<7<<7<<6<<5;
QList<int> listTotal;
listTotal<<11<<11<<11<<11<<11
<<11<<12<<12<<11<<12;
QList<int> listWin;
listWin<<9<<9<<8<<8<<8
<<7<<7<<7<<6<<5;
for (int i=0;i<10;i++){
QTableWidgetItem* pItem = new QTableWidgetItem;
pItem->setData(Qt::EditRole, i+1);
tableWidget->setItem(i,0,pItem);
if(i<3){
pItem->setIcon(
QIcon(QPixmap(QString(":/image/%1.png").arg(i+1)).scaled(QSize(24,24))));
}
pItem->setTextAlignment(Qt::AlignCenter);
tableWidget->setItem(i,1,new QTableWidgetItem(listName.at(i)));
tableWidget->item(i,1)->setTextAlignment(Qt::AlignCenter);
if(i==0){
tableWidget->item(i,1)->setIcon(
QIcon(QPixmap(":/image/wbg.png").scaled(QSize(24,24))));
}else if(i==1){
tableWidget->item(i,1)->setIcon(
QIcon(QPixmap(":/image/v5.png").scaled(QSize(24,24))));
}else if(i==2){
tableWidget->item(i,1)->setIcon(
QIcon(QPixmap(":/image/edg.png").scaled(QSize(24,24))));
}
tableWidget->setCellWidget(i, 2, getBar(listWin.at(i),listTotal.at(i)));
QTableWidgetItem* pItem_win = new QTableWidgetItem;
pItem_win->setData(Qt::EditRole, listWin.at(i)*100/listTotal.at(i));
tableWidget->setItem(i,2,pItem_win);
pItem_win->setTextAlignment(Qt::AlignCenter);
QTableWidgetItem* pItem_score = new QTableWidgetItem;
pItem_score->setData(Qt::EditRole, listScore.at(i));
tableWidget->setItem(i,3,pItem_score);
pItem_score->setTextAlignment(Qt::AlignCenter);
}
tableWidget->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
QProgressBar* MainWindow::getBar(int percent,int total)
{
QProgressBar *bar = new QProgressBar;
bar->setRange(0, total);
bar->setValue(percent);
QColor bgColor = QColor(255, 255, 255);
QColor chunkColor1 = QColor(100, 184, 255);
QColor chunkColor2 = QColor(24, 189, 155);
QColor chunkColor3 = QColor(255, 107, 107);
QColor textColor1 = QColor(10, 10, 10);
QColor textColor2 = QColor(255, 255, 255);
QColor textColor3 = QColor(255, 255, 255);[添加链接描述](https://download.csdn.net/download/kchmmd/83456262)
QString qss = QString("QProgressBar{background:%1;border-width:0px;border-radius:0px;text-align:center;}"
"QProgressBar::chunk{border-radius:0px;}").arg(bgColor.name());
if (percent*100/total < 50) {
qss += QString("QProgressBar{color:%1;}"
"QProgressBar::chunk{background:%2;}").arg(textColor1.name()).arg(chunkColor1.name());
} else if (percent*100/total < 80) {
qss += QString("QProgressBar{color:%1;}"
"QProgressBar::chunk{background:%2;}").arg(textColor2.name()).arg(chunkColor2.name());
} else {
qss += QString("QProgressBar{color:%1;}"
"QProgressBar::chunk{background:%2;}").arg(textColor3.name()).arg(chunkColor3.name());
}
bar->setStyleSheet(qss);
return bar;
}
源码
点击此处下载源码
|