在Qt中设置QLineEdit焦点

发布于 2024-07-13 04:47:14 字数 1206 浏览 12 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稳稳的幸福 2024-07-20 04:47:14

另一个可能有效的技巧是使用 singleshot 计时器:

QTimer::singleShot(0, line, SLOT(setFocus()));

实际上,这会在事件发生后立即调用 QLineEdit 实例的 setFocus() 槽系统可以“自由”地这样做,即在小部件完全构建之后的某个时间。

Another trick that might work is by using the singleshot timer:

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.

痴意少年 2024-07-20 04:47:14

键盘焦点与小部件相关 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 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.

妄司 2024-07-20 04:47:14

也许这是一个更新,因为最后一个答案是在 2012 年,而 OP 最后一次编辑这个问题是在 2014 年。他们让我做到这一点的方法是改变政策,然后设定焦点。

line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();

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.

line->setFocusPolicy(Qt::StrongFocus);
line->setFocus();
决绝 2024-07-20 04:47:14

在 Qt setFocus() 中,您可以尝试其他重载方法,该方法采用 Qt::FocusReason 参数,如下所示:

line->setFocus(Qt::OtherFocusReason);

您可以在以下链接中阅读有关焦点原因选项的信息:

​​ 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:

line->setFocus(Qt::OtherFocusReason);

You can read about focus reason options in the following link:

http://doc.trolltech.com/4.4/qt.html#FocusReason-enum

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文