目前的焦点策略
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->resize(400,600);
ui->tableWidget->resize(200,300);
ui->tableWidget_2->resize(200,300);
ui->tableWidget->horizontalHeader()->setHidden(true);
ui->tableWidget_2->horizontalHeader()->setHidden(true);
ui->tableWidget->verticalHeader()->setSectionsClickable(false);
ui->tableWidget_2->verticalHeader()->setSectionsClickable(false);
ui->tableWidget->verticalHeader()->setMinimumWidth(70);
ui->tableWidget->setColumnWidth(0,70);
ui->tableWidget_2->verticalHeader()->setMinimumWidth(70);
ui->tableWidget_2->setColumnWidth(0,70);
for(int rows=0;rows<ui->tableWidget->rowCount();rows++)
{
for(int columns=0;columns<ui->tableWidget_2->columnCount();columns++)
{
ui->tableWidget->setItem(rows,columns,new QTableWidgetItem("linshi"));
ui->tableWidget->item(rows,columns)->setTextAlignment(Qt::AlignCenter);
}
}
for(int rows=0;rows<ui->tableWidget_2->rowCount();rows++)
{
for(int columns=0;columns<ui->tableWidget_2->columnCount();columns++)
{
ui->tableWidget_2->setItem(rows,columns,new QTableWidgetItem("linshi"));
ui->tableWidget_2->item(rows,columns)->setTextAlignment(Qt::AlignCenter);
}
}
QTableWidgetItem *itemT=NULL;
ui->tableWidget->setFocus();
this->setFocusPolicy(Qt::StrongFocus);
ui->tableWidget->setStyleSheet("QTableWidget::focus{border:3px solid #0000ff;}"
"QTableWidget{selection-background-color:green;}"
"QTableWidget::up-button{background-color:red;}"
"QTableWidget::down-button{background-color:black;}");
ui->tableWidget_2->setStyleSheet("QTableWidget::focus{border:3px solid #de2916;}"
"QTableWidget{selection-background-color:red;}"
"QTableWidget::up-button{background-color:red;}"
"QTableWidget::down-button{background-color:black;}");
ui->btn1->setFocusPolicy(Qt::TabFocus);
ui->btn1->setStyleSheet("QPushButton::focus{background-color:blue;}");
ui->btn1_2->setStyleSheet("QPushButton::focus{background-color:black;}");
ui->tableWidget->installEventFilter(this);
ui->tableWidget_2->installEventFilter(this);
ui->btn1->installEventFilter(this);
ui->btn1_2->installEventFilter(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_btn1_clicked()
{
if(ui->tableWidget->hasFocus())
{
ui->tableWidget_2->setFocus();
}else if(ui->tableWidget_2->hasFocus())
{
ui->tableWidget->setFocus();
}
}
void Widget::on_btn1_2_clicked()
{
ui->dateTimeEdit->setCurrentSectionIndex(++m_cntDtedit%ui->dateTimeEdit->sectionCount());
ui->dateTimeEdit->stepBy(0);
if(m_cntDtedit>=6)
{
m_cntDtedit=0;
}
}
void Widget::quit()
{
ui->tableWidget->setFocus();
}
bool Widget::eventFilter(QObject *obj, QEvent *evt)
{
if(obj==ui->tableWidget||obj==ui->tableWidget_2||obj==ui->btn1
||obj==ui->btn1_2)
{
if(evt->type()==QEvent::KeyPress)
{
QKeyEvent *key_evt=static_cast<QKeyEvent*>(evt);
if(key_evt->key()==Qt::Key_Tab
||key_evt->key()==Qt::Key_Down)
{
bool t1=ui->tableWidget->hasFocus();
bool t2=ui->tableWidget_2->hasFocus();
bool b1=ui->btn1->hasFocus();
bool b2=ui->btn1_2->hasFocus();
if(t1)
{
if(ui->tableWidget->currentRow()<ui->tableWidget->rowCount()-1)
{
}
else if(ui->tableWidget->currentRow()==ui->tableWidget->rowCount()-1)
{
ui->tableWidget->setItem(3,0,new QTableWidgetItem(QString::number(ui->tableWidget->currentRow(),10)));
ui->tableWidget_2->setFocus();
ui->tableWidget->clearFocus();
}
}
else if(t2)
{
if(ui->tableWidget_2->currentRow()<ui->tableWidget_2->rowCount()-1)
{
}
else if(ui->tableWidget_2->currentRow()==ui->tableWidget_2->rowCount()-1)
{
ui->btn1->setFocus();
}
}else if(b1)
{
ui->btn1_2->setFocus();
}
else if(b2)
{
ui->tableWidget->setFocus();
}
}
{
bool t1=ui->tableWidget->hasFocus();
bool t2=ui->tableWidget_2->hasFocus();
bool b1=ui->btn1->hasFocus();
bool b2=ui->btn1_2->hasFocus();
if(t1)
{
if(ui->tableWidget->currentRow()>0)
{
focusPreviousChild();
}else if(ui->tableWidget->currentRow()==0)
{
ui->btn1_2->setFocus();
}
}else if(t2)
{
if(ui->tableWidget_2->currentRow()>0)
{
focusPreviousChild();
}else if(ui->tableWidget_2->currentRow()==0)
{
ui->tableWidget->setFocus();
}
}else if(b1)
{
ui->tableWidget_2->setFocus();
}else if(b2)
{
ui->btn1->setFocus();
}
}
}
}
return QWidget::eventFilter(obj,evt);
}