今天突然想起在上个公司解决的一个BUG嵌入式中QTableWidget一般只能看数据不能编辑,或者是选择
所以双击出现空白QTableWidgetItem是不允许的
所说BUG现象
解决
在空白的单元格中,添加不可编辑的QTableWidgetItem方式1不用遍历,在ItemMaxNum处开始添加
方式2遍历,在空白的单元格处开始添加
方式1
- 主要内容
//如果QTableWidgetItem个数不是3的倍数,最后一行没有满,布局会自动补充一个空白控件,多次点击才能出现
if(ItemMaxNum%Column!=0&&(i+1)==ItemMaxNum)
{
// 设置空白项的文本为 ""
for(int k = (ItemMaxNum%Column); k < table->columnCount(); k++) {
QTableWidgetItem* item = new QTableWidgetItem("");
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
table->setItem(table->rowCount() - 1, k, item);
}
}
方式2
- 主要内容
if(ItemMaxNum!=Row*Column)
{
for (int row = 0; row < Row; row++) {
for (int col = 0; col < Column; ++col) {
auto item = table->cellWidget(row,col); //灵活变通:table->item(int row, int column)
if(item==nullptr){
QTableWidgetItem* items = new QTableWidgetItem("");
items->setFlags(items->flags() ^ Qt::ItemIsEditable);
table->setItem(row, col, items);
}
}
}
}
全部内容
#include "testbug.h"
#define Column 3
#define Row 6
#define ItemMaxNum 16
TestBug::TestBug(QWidget *parent)
: QWidget(parent)
{
//窗口将始终显示在其他窗口之上
this->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint);
this->setFixedSize(800,600);
QTableWidget *table = new QTableWidget;
table->horizontalHeader()->hide();
table->verticalHeader()->hide();
table->horizontalHeader()->setStretchLastSection(true);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
table->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
table->setFocusPolicy(Qt::NoFocus); // 去除选中虚线
table->setSelectionMode(QAbstractItemView::NoSelection);
table->setStyleSheet("QTableWidget{gridline-color:white;background:DeepSkyBlue; border:0px solid #ffffff;border-bottom-width:0px;border-right-width:0px;}");
table->setColumnCount(Column);
table->setRowCount(Row);
table->setShowGrid(false);
QHBoxLayout *hboxLayout = new QHBoxLayout(this);
hboxLayout->addWidget(table);
int row =0,col=0;
for (int i = 0; i < ItemMaxNum; i++) {
col = i;
if (col >= Column)
{
if (col % Column == 0)
{
row++;
}
col %= Column;
}
QString style = QString("QPushButton[status=\"1\"]{font-size:28px;background-color:transparent;color:rgb(255,255,255);border-radius:10;border:2px solid white;}"
"QPushButton[status=\"2\"]{background-color:rgb(255,255,255);font-size:28px;color:#06411B;border-radius:10;border:2px solid white;}");
QWidget *pbtnWidget = new QWidget;
QHBoxLayout *pbtnLayout = new QHBoxLayout;
QPushButton *pBtn = new QPushButton(QString::fromLocal8Bit("[%1]号").arg(i), pbtnWidget);
pbtnLayout->addWidget(pBtn);
pbtnWidget->setLayout(pbtnLayout);
pBtn->setFixedSize(QSize(185, 50));
pBtn->setProperty("status", "2");
pBtn->setStyleSheet(style);
pBtn->setFocusPolicy(Qt::NoFocus);
pBtn->setObjectName(QString::number(i));
table->setCellWidget(row, col, pbtnWidget);
//如果QTableWidgetItem个数不是3的倍数,最后一行没有满,布局会自动补充一个空白控件,多次点击才能出现
#if 0
// 方式1
if(ItemMaxNum%Column!=0&&(i+1)==ItemMaxNum)
{
// 设置空白项的文本为 ""
for(int k = (ItemMaxNum%Column); k < table->columnCount(); k++) {
QTableWidgetItem* item = new QTableWidgetItem("");
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
table->setItem(table->rowCount() - 1, k, item);
}
}
#endif
connect(pBtn, SIGNAL(clicked()), this, SLOT(onBtnClick()));
}
//方式2
if(ItemMaxNum!=Row*Column)
{
for (int row = 0; row < Row; row++) {
for (int col = 0; col < Column; ++col) {
auto item = table->cellWidget(row,col); //灵活变通:table->item(int row, int column)
if(item==nullptr){
QTableWidgetItem* items = new QTableWidgetItem("");
items->setFlags(items->flags() ^ Qt::ItemIsEditable);
table->setItem(row, col, items);
}
}
}
}
}
TestBug::~TestBug()
{
}
void TestBug::onBtnClick()
{
QPushButton *pBtn = qobject_cast<QPushButton *>(QObject::sender());
if (pBtn!=nullptr)
{
qDebug()<<pBtn->text()<<" ObjectName ="<<pBtn->objectName();
if (1 == pBtn->property("status").toInt())
pBtn->setProperty("status", "2");
else
pBtn->setProperty("status", "1");
pBtn->setStyleSheet(pBtn->styleSheet());
}
}
效果
- QTableWidget双击出现空白QTableWidgetItem不再会出现输入