关于焦点的问题,求助啊。。
我做的是一个软件盘
有3条LineEdit
几个1-9的数字键。我按了数字键,焦点跳了,LineEdit显示我输入的值,当我切换到第二条LineEdit。输入的时候,显示还是在第一条LineEdit上。
其实就是没进入函数checkFocus()中的上3个if的选择。
直接执行了
else
ui->ipInfoLineEdit->setFocus();
问题貌似是光标随着我按数字键的时候不在了。请高手指点下
//=========================================
void ipset::checkFocus() //光标定位
{
//bool ipfocus;
// bool macfocus;
// bool gateway;
if(ui->ipInfoLineEdit->hasFocus())
{
//ipfocus = true;
//macfocus = false;
//gateway = false;
showstatic_ip();
}
else if(ui->maskInfoLineEdit->hasFocus())
{
//ipfocus = false;
//macfocus = true;
//gateway = false;
showmask();
}
else if(ui->gatewayInfoLineEdit->hasFocus())
{
//ipfocus = false;
//macfocus = false;
//gateway = true;
showgateway();
}
else
ui->ipInfoLineEdit->setFocus();
//=========================================================================
void ipset::showstatic_ip()
{
qDebug()<<"record"<<record;
ui->ipInfoLineEdit->setText(ui->ipInfoLineEdit->text()+record);
ui->ipInfoLineEdit->setFocus();
record.clear();
}
void ipset::showmask()
{
ui->maskInfoLineEdit->setText(ui->maskInfoLineEdit->text()+record);
ui->maskInfoLineEdit->setFocus();
record.clear();
}
void ipset::showgateway()
{
ui->gatewayInfoLineEdit->setText(ui->gatewayInfoLineEdit->text()+record);
ui->gatewayInfoLineEdit->setFocus();
record.clear();
}
//==========================================================
//软件盘设置
void ipset::on_pushButton_1_clicked()//键1
{
record= "1" ;
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_2_clicked()//键2
{
//QString record;
record="2";
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_3_clicked()//键3
{
record="3";
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_4_clicked()//键4
{
record="4";
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_5_clicked()//键5
{
record="5";
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_6_clicked()//键6
{
record="6";
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_7_clicked()//键7
{
record="7";
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_8_clicked()//键8
{
record="8";
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_9_clicked()//键9
{
record="9";
qDebug()<<"record"<<record;
checkFocus();
}
void ipset::on_pushButton_dian_clicked()//键dian
{
record=".";
qDebug()<<"record"<<record;
checkFocus();
}
[解决办法]
你把数字按键设置成 nofocus
lineEdit的焦点判断用以下形式:
QWidget * fWidget = qApp->focusWidget();
if (ui->lineEdit1 == qobject_cast<QLineEdit *>(fWidget ))
{....}
[解决办法]
按了数字键后,三个LineEdit都失去焦点了
checkFocus直接走else了,于是第一个LineEdit获得了焦点
建议数字键不让它获得焦点
或再加个标志位