Description: The first thing I want to do is to have multiple editable controls on a form (such as Qlineedit, Qtextedit, and so on), when which control gets the focus, which control's background is highlighted for prompting, and the following file should be used with focusinevent () and Focusoutevent (), in the actual process, I made a very serious mistake, at the very beginning I did this: I rewrote the form qwidget, and then passed the Qfocusevent event to the Qlineedit control on the form in the body of the function:
void Widget::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
.....
}
Compile the error, said that there is no call object what, and then asked the next friend to get the perfect answer:
Now that you want the control to get the focus change action, you should override the control's Focusinevent () and Focusoutevent (), which overrides the Qlineedit class, redefine the two handler functions, and then in the main program, include Our own rewrite of the Qlineedit header file, the specific code is as follows:
// MYLINEEDIT_H
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QLineEdit>
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
MyLineEdit(QWidget *parent=0);
~MyLineEdit();
protected:
virtual void focusInEvent(QFocusEvent *e);
virtual void focusOutEvent(QFocusEvent *e);
};
#endif // MYLINEEDIT_H
`
//myLineEdit.cpp
#include "myLineEdit.h"
MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
{
}
MyLineEdit::~MyLineEdit()
{
}
void MyLineEdit::focusInEvent(QFocusEvent *e)
{
QPalette p=QPalette();
p.setColor(QPalette::Base,Qt::green); //QPalette::Base 对可编辑输入框有效,还有其他类型,具体的查看文档
setPalette(p);
}
void MyLineEdit::focusOutEvent(QFocusEvent *e)
{
QPalette p1=QPalette();
p1.setColor(QPalette::Base,Qt::white);
setPalette(p1);
}
`
//widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "MyLineEdit.h"
#include <QGridLayout>
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
init();
}
Widget::~Widget()
{
delete ui;
}
void Widget::init()
{
lineEdit1=new MyLineEdit(this);
lineEdit2=new MyLineEdit(this);
gridLayout=new QGridLayout;
gridLayout->addWidget(lineEdit1,0,0);
gridLayout->addWidget(lineEdit2,1,0);
setLayout(gridLayout);
}
As a program example, when a text box gets the focus, the background is automatically filled with green, the background reverts to White after losing focus, so that I want to focus on the highlight background alert function, but the actual analysis is still very complex, if my program has qtextedit, Qspinbox and so on control, Then I'll have to rewrite all these classes, trouble ... I originally thought that QT provides this kind of gain and loses the focus signal, unfortunately does not have, looks forward to the later version adds, is more convenient ...
Http://www.cnblogs.com/hicjiajia/archive/2012/05/30/2526768.html
Http://www.cnblogs.com/hicjiajia/archive/2012/05/30/2526776.html
Qt Focus Events, Focusinevent () and Focusoutevent ()