先看效果,绿色表示开,红色表示关,可以举一反三设置更多状态
实现方法
原理其实是在表格中放置QWidget控件,再在QWidget里绘制圆形,初始化表格时应该动态生成控件.
需要快速实现一个自定义的QWidget,很简单,注意看注释
class MyWidget : public QWidget{
Q_OBJECT
public:
MyWidget(){}
~MyWidget(){}
void changeState(bool bIsOpen){//用来改变状态
m_bIsOpen = bIsOpen;
this->update();//刷新绘制,调用paintEvent函数
}
protected:
void paintEvent(QPaintEvent* e){//绘制事件,控件初始化时会自动调用一次
QPainter painter(this);
QPointF certp(15, 17);//绘制的圆的圆心位置
QColor color;
//在这里设置各种状态的颜色
if(m_bIsOpen){
color.setRgb(6, 255, 65);//绿色
}else {
color.setRgb(255, 0, 0);//红色
}
painter.setFont(QFont("Arial", 10));
painter.setPen(QPen(color));
painter.setBrush(QBrush(color));
painter.drawEllipse(certp, 7, 7);//画圆形
}
private:
bool m_bIsOpen = false;
};
简单示例:点击对应按钮将状态改变为开
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPainter>
#include <QProxyStyle>
#include <QStyleOptionTab>
#include <QObject>
#include <QDebug>
namespace Ui {
class MainWindow;
}
class MyWidget : public QWidget{
Q_OBJECT
public:
MyWidget(){}
~MyWidget(){}
void changeState(bool bIsOpen){//用来改变状态
m_bIsOpen = bIsOpen;
this->update();//刷新绘制,调用paintEvent函数
}
protected:
void paintEvent(QPaintEvent* e){//绘制事件,控件初始化时会自动调用一次
QPainter painter(this);
QPointF certp(15, 17);//绘制的圆的圆心位置
QColor color;
//在这里设置各种状态的颜色
if(m_bIsOpen){
color.setRgb(6, 255, 65);//绿色
}else {
color.setRgb(255, 0, 0);//红色
}
painter.setFont(QFont("Arial", 10));
painter.setPen(QPen(color));
painter.setBrush(QBrush(color));
painter.drawEllipse(certp, 7, 7);//画圆形
}
private:
bool m_bIsOpen = false;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton1_clicked();
void on_pushButton2_clicked();
void on_pushButton3_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <functional>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableWidget->setFont(QFont("黑体", 12));
ui->tableWidget->horizontalHeader()->setFont(QFont("黑体", 14));
ui->tableWidget->setStyleSheet("QHeaderView::section{border:0px solid white;"
"background-color:white;}");
ui->tableWidget->setRowCount(3);
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setShowGrid(false);
//设置表头
QStringList h;
h << tr("") << tr("名称") << tr("备注");
ui->tableWidget->verticalHeader()->setVisible(false);
ui->tableWidget->setHorizontalHeaderLabels(h);
ui->tableWidget->setColumnWidth(0, 30);
ui->tableWidget->setColumnWidth(1, 150);
ui->tableWidget->setColumnWidth(2, 150);
for(int i = 0; i < 3; i++){
ui->tableWidget->setItem(i, 0, new QTableWidgetItem());
//添加自定义控件
MyWidget* widget = new MyWidget;
ui->tableWidget->setCellWidget(i, 0, widget);
ui->tableWidget->setItem(i, 1, new QTableWidgetItem("设备" + QString::number(i + 1)));
ui->tableWidget->setItem(i, 2, new QTableWidgetItem());
}
//文字居中
for(int i = 0; i < ui->tableWidget->rowCount(); i++){
for(int j = 0; j < ui->tableWidget->columnCount(); j++){
ui->tableWidget->item(i, j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton1_clicked()
{
MyWidget* widget = (MyWidget*)(ui->tableWidget->cellWidget(0, 0));
widget->changeState(true);
}
void MainWindow::on_pushButton2_clicked()
{
MyWidget* widget = (MyWidget*)(ui->tableWidget->cellWidget(1, 0));
widget->changeState(true);
}
void MainWindow::on_pushButton3_clicked()
{
MyWidget* widget = (MyWidget*)(ui->tableWidget->cellWidget(2, 0));
widget->changeState(true);
}