#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QLabel>QT_BEGIN_NAMESPACEnamespace Ui { class Widget; }QT_END_NAMESPACEclass Widget : public QWidget{ Q_OBJECTpublic: Widget(QWidget *parent = nullptr); ~Widget();private: Ui::Widget *ui;public slots: void press_pbtn_01(); void table_clik(int, int);};#endif // WIDGET_H
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget){ ui->setupUi(this); // отключение курсора ui->tableWidget->setSelectionMode(QAbstractItemView::NoSelection); // инициализация таблицы for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) { ui->tableWidget->setCellWidget(i ,j, new QLabel); } QObject::connect(ui->pbtn_01,SIGNAL(clicked()),this,SLOT(press_pbtn_01())); QObject::connect(ui->tableWidget,SIGNAL(cellClicked(int, int)), this,SLOT(table_clik(int, int)));}Widget::~Widget(){ delete ui;}// clearvoid Widget::press_pbtn_01(){ QString str = "border-radius: 13px; background-color:white;"; for(int row = 0; row < 10; row++) for(int col = 0; col < 10; col++) { QLabel *lbl(qobject_cast<QLabel*>(ui->tableWidget->cellWidget(row, col))); lbl->setStyleSheet(str); } this -> repaint();}// клик в таблицуvoid Widget::table_clik(int row, int col){ static int cx = 0; QString str = "border-radius: 13px; background-color:red;"; if(cx % 2) str = "border-radius: 13px; background-color:blue;"; QLabel *lbl(qobject_cast<QLabel*>(ui->tableWidget->cellWidget(row, col))); lbl->setFixedSize(26, 26); lbl->setStyleSheet(str); cx++;}