在Qt中设置QLineEdit焦点
我有一个qt问题。 我希望 QLineEdit 小部件在应用程序启动时获得焦点。 以下面的代码为例:
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QFont>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
window->setWindowIcon(QIcon("qtest16.ico"));
window->setWindowTitle("QtTest");
QHBoxLayout *layout = new QHBoxLayout(window);
// Add some widgets.
QLineEdit *line = new QLineEdit();
QPushButton *hello = new QPushButton(window);
hello->setText("Select all");
hello->resize(150, 25);
hello->setFont(QFont("Droid Sans Mono", 12, QFont::Normal));
// Add the widgets to the layout.
layout->addWidget(line);
layout->addWidget(hello);
line->setFocus();
QObject::connect(hello, SIGNAL(clicked()), line, SLOT(selectAll()));
QObject::connect(line, SIGNAL(returnPressed()), line, SLOT(selectAll()));
window->show();
return app.exec();
}
为什么 line->setFocus()
只有在布局小部件之后放置并且在它不起作用之前使用时,才会将焦点设置在行小部件@app启动上?
I am having a qt question. I want the QLineEdit widget to have the focus at application startup. Take the following code for example:
#include <QtGui/QApplication>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QFont>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
window->setWindowIcon(QIcon("qtest16.ico"));
window->setWindowTitle("QtTest");
QHBoxLayout *layout = new QHBoxLayout(window);
// Add some widgets.
QLineEdit *line = new QLineEdit();
QPushButton *hello = new QPushButton(window);
hello->setText("Select all");
hello->resize(150, 25);
hello->setFont(QFont("Droid Sans Mono", 12, QFont::Normal));
// Add the widgets to the layout.
layout->addWidget(line);
layout->addWidget(hello);
line->setFocus();
QObject::connect(hello, SIGNAL(clicked()), line, SLOT(selectAll()));
QObject::connect(line, SIGNAL(returnPressed()), line, SLOT(selectAll()));
window->show();
return app.exec();
}
Why does line->setFocus()
sets the focus on the line widget @app startup only if it is placed after laying out the widgets and if used before it's not working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一个可能有效的技巧是使用
singleshot
计时器:实际上,这会在事件发生后立即调用
QLineEdit
实例的setFocus()
槽系统可以“自由”地这样做,即在小部件完全构建之后的某个时间。Another trick that might work is by using the
singleshot
timer:Effectively, this invokes the
setFocus()
slot of theQLineEdit
instance right after the event system is "free" to do so, i.e. sometime after the widget is completely constructed.键盘焦点与小部件相关 Tab 顺序,默认 Tab 顺序基于小部件的构建顺序。 因此,创建更多小部件会改变键盘焦点。 这就是为什么您必须将
QWidget::setFocus
最后调用。我会考虑在主窗口中使用
QWidget
的子类来覆盖showEvent
虚拟函数,然后将键盘焦点设置到lineEdit
。 这将具有在显示窗口时始终给予lineEdit
焦点的效果。Keyboard focus is related to widget tab order, and the default tab order is based on the order in which widgets are constructed. Therefore, creating more widgets changes the keyboard focus. That is why you must make the
QWidget::setFocus
call last.I would consider using a sub-class of
QWidget
for your main window that overrides theshowEvent
virtual function and then sets keyboard focus to thelineEdit
. This will have the effect of always giving thelineEdit
focus when the window is shown.也许这是一个更新,因为最后一个答案是在 2012 年,而 OP 最后一次编辑这个问题是在 2014 年。他们让我做到这一点的方法是改变政策,然后设定焦点。
Perhaps this is an update as the last answer was in 2012 and the OP last edited the question in 2014. They way I got this to work was to change the policy and then set the focus.
在 Qt setFocus() 中,您可以尝试其他重载方法,该方法采用 Qt::FocusReason 参数,如下所示:
您可以在以下链接中阅读有关焦点原因选项的信息:
http://doc.trolltech.com/4.4/qt.html#FocusReason-enum
In Qt setFocus() is a slot, you can try other overloaded method which takes a Qt::FocusReason parameter like the line shown below:
You can read about focus reason options in the following link:
http://doc.trolltech.com/4.4/qt.html#FocusReason-enum