// such as when i press key-down in the widget list. it will not focus the option. i check the log and then find out that when it lost focus that the focus is on the background widget list.
// - if my app has only one widget list, it will not lost focus when i press key-down. // - if my app have two widget lists, and the second one is under the first one. when i press key-down the first time it will focus the first one option, but when i press the key-down second time, it will focus the second widget list.
// The problem has confused me a week, Are there masters can help me with the issue? I am so grateful for you. thanks .
Follow is my test code,
main.cpp
#include <QtGui/QApplication>
#include "dialog.h"
#include <QFile>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont newFont("Times", 7, QFont::Normal);
a.setFont(newFont);
QPalette *palette = new QPalette;
palette->setBrush(QPalette::Window, Qt::white);
palette->setBrush(QPalette::WindowText, Qt::black);
palette->setBrush(QPalette::BrightText, Qt::white);
palette->setBrush(QPalette::Base, Qt::white);
palette->setBrush(QPalette::AlternateBase, Qt::white);
palette->setBrush(QPalette::Highlight, Qt::black);
palette->setBrush(QPalette::Disabled, QPalette::Highlight, Qt::black);
a.setPalette(*palette);
QFile file(":/qss/mainWindow.qss");
file.open(QFile::ReadOnly);
a.setStyleSheet(file.readAll());
Dialog dlg;
//set part of window property: hide title frame
dlg.setWindowFlags(Qt::FramelessWindowHint);
//move window to zero point: location(0,0)
dlg.move(0, 0);
dlg.activateWindow();
dlg.show();
return a.exec();
}
dialog.cpp
#include <QDebug>
#include "dialog.h"
#include "ui_dialog.h"
#include "QKeyEvent"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
InitWidget();
InstallFilters();
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::InitWidget()
{
this->m_pView = NULL;
for(int index=0; index<10; index++)
{
ui->listWidget->insertItem(index, QString("Item %1").arg(index));
}
ui->listWidget->hide();
}
bool Dialog::InstallFilters()
{
ui->lineEdit->installEventFilter(this);
ui->lineEdit_2->installEventFilter(this);
ui->listWidget->installEventFilter(this);
ui->textEdit->installEventFilter(this);
ui->textEdit->setFocus();
ui->textEdit->setText("I am background");
return true;
}
bool Dialog::eventFilter(QObject *o, QEvent *e)
{
qDebug()<<"The Obj is"<<o->objectName()<<"EventType:"<<e->type();
if(e->type()!= QEvent::KeyPress)
return QDialog::eventFilter(o, e);
QKeyEvent *pKeyEvent = dynamic_cast<QKeyEvent*>(e);
switch(pKeyEvent->key())
{
case Qt::Key_F10:
case Qt::Key_M:
{
ui->listWidget->show();
ui->listWidget->setFocus();
ui->listWidget->setCurrentRow(0);
return true;
}
case Qt::Key_F3:
case Qt::Key_C:
{
ui->listWidget->hide();
ui->textEdit->setFocus();
return true;
}
}
return QDialog::eventFilter(o, e);
}
Keypad navigation feature of qt-embedded was disabled as default in the configuration of mine at 4.8.6. But it is opened in 4.8.7. So it displays overlap and we can not find out focus. Therefore the arrow keys changed the active widget instead of the active list element.
Solution, QT_EMBEDDED_KEYPAD_FLAGS = ""