判断QlineEdit焦点
我设置六个QlineEdit,和一个选择文件对话框按钮,这是我的槽函数
connect(NoteButton,SIGNAL(clicked()), this, SLOT(ChooseLinkFile()));
void ActionDialog::ChooseLinkFile()
{
/*lineEditJson1->setFocus();
lineEditJson2->setFocus();
lineEditJson3->setFocus();
lineEditJson4->setFocus();
lineEditJson5->setFocus();
lineEditJson6->setFocus();*/
if (this->focusWidget() == lineEditJson1)
{
QString temp = m_Propath + "/Layout";
QString CurrentFile = m_Propath + "/Layout/home.json";
QString cpath = QFileDialog::getOpenFileName(
this, "Select json", temp,
"All File(*.*);;json file(*.json)");
if (cpath == CurrentFile)
{
QMessageBox message(QMessageBox::Warning, "Warning",
"Can not select the current template",QMessageBox::Yes, NULL);
if (message.exec() == QMessageBox::Yes)
{
lineEditJson1->setFocus();
lineEditJson1->clear();
return;
}
}
QString filename = cpath.mid(cpath.lastIndexOf("/") + 1);
QString filename1 = filename.left(filename.lastIndexOf("."));
if(!cpath.isNull())
{
qDebug()<<__FILE__<<__LINE__<<lineEditJson1->focusWidget();
lineEditJson1->setText(filename1);
lineEditJson1->clearFocus();
}
}
else if (this->focusWidget() == lineEditJson2)
{
QString temp = m_Propath + "/Layout";
QString CurrentFile = m_Propath + "/Layout/home.json";
QString cpath = QFileDialog::getOpenFileName(
this, "Select json", temp,
"All File(*.*);;json file(*.json)");
if (cpath == CurrentFile)
{
QMessageBox message(QMessageBox::Warning, "Warning",
"Can not select the current template",QMessageBox::Yes, NULL);
if (message.exec() == QMessageBox::Yes)
{
lineEditJson2->setFocus();
lineEditJson2->clear();
return;
}
}
QString filename = cpath.mid(cpath.lastIndexOf("/") + 1);
QString filename1 = filename.left(filename.lastIndexOf("."));
if(!cpath.isNull())
{
qDebug()<<__FILE__<<__LINE__<<lineEditJson1->focusWidget();
lineEditJson2->setText(filename1);
lineEditJson2->clearFocus();
}
}
else if (this->focusWidget() == lineEditJson3)
{
QString temp = m_Propath + "/Layout";
QString CurrentFile = m_Propath + "/Layout/home.json";
QString cpath = QFileDialog::getOpenFileName(
this, "Select json", temp,
"All File(*.*);;json file(*.json)");
if (cpath == CurrentFile)
{
QMessageBox message(QMessageBox::Warning, "Warning",
"Can not select the current template",QMessageBox::Yes, NULL);
if (message.exec() == QMessageBox::Yes)
{
lineEditJson3->setFocus();
lineEditJson3->clear();
return;
}
}
QString filename = cpath.mid(cpath.lastIndexOf("/") + 1);
QString filename1 = filename.left(filename.lastIndexOf("."));
if(!cpath.isNull())
{
qDebug()<<__FILE__<<__LINE__<<lineEditJson1->focusWidget();
lineEditJson3->setText(filename1);
lineEditJson3->clearFocus();
}
}
else if (this->focusWidget() == lineEditJson4)
{
QString temp = m_Propath + "/Layout";
QString CurrentFile = m_Propath + "/Layout/home.json";
QString cpath = QFileDialog::getOpenFileName(
this, "Select json", temp,
"All File(*.*);;json file(*.json)");
if (cpath == CurrentFile)
{
QMessageBox message(QMessageBox::Warning, "Warning",
"Can not select the current template",QMessageBox::Yes, NULL);
if (message.exec() == QMessageBox::Yes)
{
lineEditJson4->setFocus();
lineEditJson4->clear();
return;
}
}
QString filename = cpath.mid(cpath.lastIndexOf("/") + 1);
QString filename1 = filename.left(filename.lastIndexOf("."));
if(!cpath.isNull())
{
qDebug()<<__FILE__<<__LINE__<<lineEditJson1->focusWidget();
lineEditJson4->setText(filename1);
lineEditJson4->clearFocus();
}
}
else if (this->focusWidget() == lineEditJson5)
{
QString temp = m_Propath + "/Layout";
QString CurrentFile = m_Propath + "/Layout/home.json";
QString cpath = QFileDialog::getOpenFileName(
this, "Select json", temp,
"All File(*.*);;json file(*.json)");
if (cpath == CurrentFile)
{
QMessageBox message(QMessageBox::Warning, "Warning",
"Can not select the current template",QMessageBox::Yes, NULL);
if (message.exec() == QMessageBox::Yes)
{
lineEditJson5->setFocus();
lineEditJson5->clear();
return;
}
}
QString filename = cpath.mid(cpath.lastIndexOf("/") + 1);
QString filename1 = filename.left(filename.lastIndexOf("."));
if(!cpath.isNull())
{
qDebug()<<__FILE__<<__LINE__<<lineEditJson1->focusWidget();
lineEditJson5->setText(filename1);
lineEditJson6->clearFocus();
}
}
else if (this->focusWidget() == lineEditJson6)
{
QString temp = m_Propath + "/Layout";
QString CurrentFile = m_Propath + "/Layout/home.json";
QString cpath = QFileDialog::getOpenFileName(
this, "Select json", temp,
"All File(*.*);;json file(*.json)");
if (cpath == CurrentFile)
{
QMessageBox message(QMessageBox::Warning, "Warning",
"Can not select the current template",QMessageBox::Yes, NULL);
if (message.exec() == QMessageBox::Yes)
{
lineEditJson6->setFocus();
lineEditJson6->clear();
return;
}
}
QString filename = cpath.mid(cpath.lastIndexOf("/") + 1);
QString filename1 = filename.left(filename.lastIndexOf("."));
if(!cpath.isNull())
{
qDebug()<<__FILE__<<__LINE__<<lineEditJson1->focusWidget();
lineEditJson6->setText(filename1);
lineEditJson6->clearFocus();
}
}
}
我通过focusWidget()函数判断是谁接受了焦点,
现在主要问题是,如何对六个QlineEdit设置焦点,让focusWidget()进行判断焦点,进行处理,当我设置第一个编辑框为焦点时,函数进行处理,当我选择第四个编辑框时,函数进行处理,这个六个QlineEdit如何判断设置当前选择的焦点
/*lineEditJson1->setFocus();
lineEditJson2->setFocus();
lineEditJson3->setFocus();
lineEditJson4->setFocus();
lineEditJson5->setFocus();
lineEditJson6->setFocus();*/
请各位指教 --------------------编程问答-------------------- 给点建议啊,各位朋友 --------------------编程问答-------------------- 代码太长,能不能精简一点?
我觉得没有人愿意看这么长的代码。 --------------------编程问答-------------------- void ActionDialog::ChooseLinkFile()
{
if (this->focusWidget() == lineEditJson1)
{
QString temp = m_Propath + "/Layout";
QString CurrentFile = m_Propath + "/Layout/home.json";
QString cpath = QFileDialog::getOpenFileName(
this, "Select json", temp,
"All File(*.*);;json file(*.json)");
if (cpath == CurrentFile)
{
QMessageBox message(QMessageBox::Warning, "Warning",
"Can not select the current template",QMessageBox::Yes, NULL);
if (message.exec() == QMessageBox::Yes)
{
lineEditJson1->setFocus();
lineEditJson1->clear();
return;
}
}
QString filename = cpath.mid(cpath.lastIndexOf("/") + 1);
QString filename1 = filename.left(filename.lastIndexOf("."));
if(!cpath.isNull())
{
qDebug()<<__FILE__<<__LINE__<<lineEditJson1->focusWidget();
lineEditJson1->setText(filename1);
lineEditJson1->clearFocus();
}
}
后面都是判断哪个部件得到焦点,以此类推 --------------------编程问答-------------------- if (this->focusWidget() == lineEditJson1)
只能根据这个来判断当前是那个焦点。。。
补充:移动开发 , Qt