I'm trying to create a custom class for QTableWidget where when clicking on a cell (or eventually just pressing the mouse down over a cell) it changes the background color.
This is what I have so far... it's fairly simple but it doesn't work yet and I'm not sure what I'm missing. Sorry, I'm new to the Qt environment.
Excerpt from mainwindow.cpp
int row = 10;
int col = 10;
// set the amount of rows and columns
ui->board->setRowCount(row);
ui->board->setColumnCount(col);
// remove table headers
ui->board->horizontalHeader()->setVisible(false);
ui->board->verticalHeader()->setVisible(false);
// table styling
ui->board->setFocusPolicy(Qt::NoFocus);
// ui->board->setStyleSheet("selection-background-color: transparent");
// set fixed column and row width
for (int i = 0; i < row; i++)
ui->board->setRowHeight(i, 50);
for (int i = 0; i < col; i++)
ui->board->setColumnWidth(i, 50);
// populate with items
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
}
}
int row = 10;
int col = 10;
// set the amount of rows and columns
ui->board->setRowCount(row);
ui->board->setColumnCount(col);
// remove table headers
ui->board->horizontalHeader()->setVisible(false);
ui->board->verticalHeader()->setVisible(false);
// table styling
ui->board->setFocusPolicy(Qt::NoFocus);
ui->board->setSelectionMode(QAbstractItemView::NoSelection);
// ui->board->setStyleSheet("selection-background-color: transparent");
ui->board->setEditTriggers(QAbstractItemView::NoEditTriggers);
// set fixed column and row width
for (int i = 0; i < row; i++)
ui->board->setRowHeight(i, 50);
for (int i = 0; i < col; i++)
ui->board->setColumnWidth(i, 50);
// populate with items
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
ui->board->setItem(i, j, new QTableWidgetItem);
}
}
To copy to clipboard, switch view to plain text mode
excerpt from table.h
#include <QTableWidget>
{
public:
Table
(QWidget *parent
= nullptr
) {}
Q_SIGNALS:
};
#include <QTableWidget>
class Table : public QTableWidget
{
public:
Table(QWidget *parent = nullptr) {}
Q_SIGNALS:
void itemClicked(QTableWidgetItem *item);
};
To copy to clipboard, switch view to plain text mode
excerpt from table.cpp
#include "table.h"
item->setBackground(Qt::red);
}
#include "table.h"
void Table::itemClicked(QTableWidgetItem *item) {
item->setBackground(Qt::red);
}
To copy to clipboard, switch view to plain text mode
Bookmarks