在Qt中设置QLineEdit焦点 [英] Set QLineEdit focus in Qt

查看:1485
本文介绍了在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);

//添加一些小部件。
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));

//将小部件添加到布局。
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 / code>设置焦点在行小部件@app启动只有如果它是在布局窗口小部件之后,如果使用之前,它不工作?

键盘焦点与小部件标签顺序,默认标签顺序基于订单构建的窗口小部件。因此,创建更多小部件会更改键盘焦点。这就是为什么你必须使QWidget :: setFocus 最后调用。 / p>

我会考虑在你的主窗口中使用QWidget的子类来覆盖 showEvent virtual function,然后将键盘焦点设置为线条编辑。这将产生总是在显示窗口时给出线编辑焦点的效果。


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?

解决方案

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 line edit. This will have the effect of always giving the line edit focus when the window is shown.

这篇关于在Qt中设置QLineEdit焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆