判断QlineEdit焦点

chenchizhao 2012-04-07 09:49:08
我设置六个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();*/
请各位指教
...全文
637 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shen_wei 2012-04-18
  • 打赏
  • 举报
回复
if (this->focusWidget() == lineEditJson1)
只能根据这个来判断当前是那个焦点。。。
chenchizhao 2012-04-07
  • 打赏
  • 举报
回复
给点建议啊,各位朋友
chenchizhao 2012-04-07
  • 打赏
  • 举报
回复
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();
}
}
后面都是判断哪个部件得到焦点,以此类推
lexdene 2012-04-07
  • 打赏
  • 举报
回复
代码太长,能不能精简一点?

我觉得没有人愿意看这么长的代码。

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧