C++ 在 Qt 中设置 QLineEdit 焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/526761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set QLineEdit focus in Qt
提问by hyperboreean
I am having a qt question. I want the QLineEdit widget to have the focus at application startup. Take the following code for example:
我有一个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();
}
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?
为什么line->setFocus()
仅在布局小部件之后放置并且在它不起作用之前使用时才将焦点设置在行小部件@app启动上?
回答by Ariya Hidayat
Another trick that might work is by using the singleshot
timer:
另一个可能有效的技巧是使用singleshot
计时器:
QTimer::singleShot(0, line, SLOT(setFocus()));
Effectively, this invokes the setFocus()
slot of the QLineEdit
instance right after the event system is "free" to do so, i.e. sometime after the widget is completely constructed.
实际上,这会在事件系统“空闲”之后立即调用实例的setFocus()
插槽QLineEdit
,即在小部件完全构造之后的某个时间。
回答by Judge Maygarden
Keyboard focusis 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.
键盘焦点与小部件的Tab 键顺序有关,默认 Tab 键顺序基于小部件的构造顺序。因此,创建更多小部件会更改键盘焦点。这就是为什么你必须QWidget::setFocus
最后打电话。
I would consider using a sub-class of QWidget
for your main window that overrides the showEvent
virtual function and then sets keyboard focus to the lineEdit
. This will have the effect of always giving the lineEdit
focus when the window is shown.
我会考虑QWidget
为您的主窗口使用一个子类来覆盖showEvent
虚函数,然后将键盘焦点设置为lineEdit
. 这将具有在lineEdit
显示窗口时始终提供焦点的效果。
回答by bandito40
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.
也许这是一个更新,因为最后一个答案是在 2012 年,而 OP 上次编辑该问题是在 2014 年。他们让我开始工作的方式是更改政策,然后设置焦点。
line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();
回答by milot
In Qt setFocus() is a slot, you can try other overloaded method which takes a Qt::FocusReason parameter like the line shown below:
在 Qt setFocus() 是一个插槽,您可以尝试其他重载方法,它采用 Qt::FocusReason 参数,如下所示:
line->setFocus(Qt::OtherFocusReason);
You can read about focus reason options in the following link:
您可以在以下链接中了解焦点原因选项: