2011-10-04 18 views
6

पर कोई कीबोर्ड इनपुट अगर QLineEdit एक माता पिता विजेट के साथ परिभाषित किया गया है है। माउस काम करेगा, दायाँ क्लिक करें, संदर्भ मेनू, पेस्ट काम करेगा - लेकिन सीधे कुंजीपटल इनपुट नहीं। कोई विचार क्यों और कैसे तय किया जा सकता है?फ्रेमलेस पॉपअप विंडो

+0

मैं क्यूटी निर्माता 2.3.1 और क्यूटी 4.7.4 के साथ, विंडोज 7 पर आपकी समस्या को पुन: उत्पन्न नहीं कर सकता। मेरे पास एक QMainWindow है, एक विजेट के साथ जो QLineEdit है - और मैंने QMainWindow पर अपना सेटविंडोफ्लैग (लाइन) कहा है। यह ठीक काम करता था - मैं पाठ टाइप करने में सक्षम था, कोई समस्या नहीं। –

+0

मेरे आवेदन पर कंटेनर विंडो मुख्य विंडो नहीं है। शायद यह अंतर बनाता है। एक अन्य मंच पर एक व्यक्ति ने सुझाव दिया कि उसे एक जैसी समस्या थी जिसे कंटेनर विंडो को सक्रिय करते समय हल किया गया था। इसे यहां आज़माएं और मेरे परिणाम यहां पोस्ट करें। – JasonGenX

+0

मुझे एक ही समस्या है। मेरे पास QDialog से व्युत्पन्न एक साधारण वर्ग है और कन्स्ट्रक्टर में मैंने क्यूटी :: विंडोफ्लैग को क्यूटी :: पॉपअप पर सेट किया है। ऐसा लगता है कि यह एक क्यूटी बग है। विंडोज 7, क्यूटी 4.8 => ठीक काम करता है। विंडोज 7, क्यूटी 5.4 => काम नहीं करता है। विंडोज 8, क्यूटी 5.4 => काम नहीं करता है। हालांकि अगर मैं ब्रेकपॉइंट सेट करता हूं और थोड़ी देर के लिए निष्पादन रोकता हूं (शोवेन्ट (QShowEvent *) में) और फिर निष्पादन जारी रखें मैं वांछित तरीके से QLineEdit का उपयोग कर सकता हूं। –

उत्तर

0
#include "StdAfx.h" 
#include "qfindedit.h" 

QFindEdit::QFindEdit(QWidget *parent) 
: QLineEdit(parent) 
, m_bEditFocus(true) 
{ 
    setPlaceholderText("please input find word"); 

m_stringListmodel = new QStringListModel(this); 
m_pFindWnd = new QListView(this); 
//m_pFindWnd->setWindowFlags(Qt::Popup); 
m_pFindWnd->setEditTriggers(QAbstractItemView::NoEditTriggers); 
m_pFindWnd->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 
m_pFindWnd->setSelectionBehavior(QAbstractItemView::SelectRows); 
m_pFindWnd->setSelectionMode(QAbstractItemView::SingleSelection); 
m_pFindWnd->setParent(0, Qt::Popup); 
m_pFindWnd->setFocusPolicy(Qt::NoFocus); 
m_pFindWnd->setFocusProxy(this); 

connect(this, SIGNAL(textEdited(const QString&)), this, SLOT(textEditedSlot(const QString&))); 
QObject::connect(m_pFindWnd, SIGNAL(clicked(QModelIndex)), 
    this, SLOT(clickedSlot(QModelIndex))); 
QObject::connect(this, SIGNAL(activated(QModelIndex)), 
    m_pFindWnd, SLOT(hide())); 

this->installEventFilter(this); 
m_pFindWnd->installEventFilter(this); 

} 

QFindEdit::~QFindEdit() 
{ 
    delete m_pFindWnd; 
} 

QStringList& QFindEdit::stringList() 
{ 
    return m_stringList; 
} 

void QFindEdit::showFindWnd(const QString& text) 
{ 
QStringList sl; 
foreach(QString word, m_stringList) { 
    if (word.contains(text)) { 
     sl << word; 
    } 
} 

if (sl.size() == 0) 
{ 
    hideFineWnd(); 
    return; 
} 
m_stringListmodel->setStringList(sl); 
m_pFindWnd->setModel(m_stringListmodel); 

m_pFindWnd->resize(rect().width(), 200); 
QPoint pTopleft = mapToGlobal(rect().bottomLeft()); 
m_pFindWnd->move(pTopleft.x(), pTopleft.y()); 
m_pFindWnd->show(); 
} 

void QFindEdit::textEditedSlot(const QString& text) 
{ 
    QString strText = text.trimmed(); 
    if (!strText.isEmpty()) 
    { 
     showFindWnd(strText); 
    } 
    else 
    { 
     hideFineWnd(); 
    } 
} 

void QFindEdit::clickedSlot(QModelIndex modelIndex) 
{ 
    setText(m_pFindWnd->model()->data(modelIndex).toString()); 
    hideFineWnd(); 
} 

void QFindEdit::hideFineWnd() 
{ 
    m_pFindWnd->hide(); 
} 

bool QFindEdit::eventFilter(QObject *o, QEvent *e) 
{ 
if (m_bEditFocus && (o == this) && e->type() == QEvent::FocusOut) 
{ 
    if (m_pFindWnd && m_pFindWnd->isVisible()) 
     return true; 
} 

if (o != m_pFindWnd) 
    return __super::eventFilter(o, e); 

switch (e->type()) 
{ 
case QEvent::KeyPress: 
    { 
     QKeyEvent *ke = static_cast<QKeyEvent *>(e); 
     QModelIndex curIndex = m_pFindWnd->currentIndex(); 
     QModelIndexList selList = m_pFindWnd->selectionModel()->selectedIndexes(); 
     const int key = ke->key(); 

     if ((key == Qt::Key_Up || key == Qt::Key_Down) && selList.isEmpty() && curIndex.isValid()) 
     { 
      m_pFindWnd->setCurrentIndex(curIndex); 
      return true; 
     } 

     switch (key) 
     { 
     case Qt::Key_End: 
     case Qt::Key_Home: 
      if (ke->modifiers() & Qt::ControlModifier) 
       return false; 
      break; 

     case Qt::Key_Up: 
      if (!curIndex.isValid()) 
      { 
       int rowCount = m_pFindWnd->model()->rowCount(); 
       QModelIndex lastIndex = m_pFindWnd->model()->index(rowCount - 1, m_pFindWnd->modelColumn()); 
       m_pFindWnd->setCurrentIndex(lastIndex); 
       return true; 
      } 
      else if (curIndex.row() == 0) 
      { 
       return true; 
      } 
      return false; 

     case Qt::Key_Down: 
      if (!curIndex.isValid()) 
      { 
       QModelIndex firstIndex = m_pFindWnd->model()->index(0, m_pFindWnd->modelColumn()); 
       m_pFindWnd->setCurrentIndex(firstIndex); 
       return true; 
      } 
      else if (curIndex.row() == m_pFindWnd->model()->rowCount() - 1) 
      { 
       return true; 
      } 
      return false; 
     } 

     m_bEditFocus = false; 
     this->event(ke); 
     m_bEditFocus = true; 
     if (e->isAccepted() || !m_pFindWnd->isVisible()) { 
      if (!this->hasFocus()) 
       hideFineWnd(); 
      if (e->isAccepted()) 
       return true; 
     } 

     switch (key) 
     { 
     case Qt::Key_Return: 
     case Qt::Key_Enter: 
     case Qt::Key_Tab: 
      hideFineWnd(); 
      if (curIndex.isValid()) 
      { 
       QString text = m_pFindWnd->currentIndex().data().toString(); 
       setText(text); 
      } 
      break; 

     case Qt::Key_F4: 
      if (ke->modifiers() & Qt::AltModifier) 
       hideFineWnd(); 
      break; 

     case Qt::Key_Backtab: 
     case Qt::Key_Escape: 
      hideFineWnd(); 
      break; 

     default: 
      break; 
     } 

     return true; 
    } 
case QEvent::MouseButtonPress: 
    if (!m_pFindWnd->underMouse()) 
    { 
     hideFineWnd(); 
     return true; 
    } 
    return false; 
case QEvent::InputMethod: 
case QEvent::ShortcutOverride: 
    QApplication::sendEvent(this, e); 
    break; 

default: 
    return false; 
} 
return false; 
} 
1

जैसा कि मैंने एक टिप्पणी मैं एक ही समस्या थी में पहले उल्लेख, लेकिन अब यह निम्न कोड के साथ तय हो गई है:

// virtual override 
void MyDialog::showEvent(QShowEvent* aShowEvent) 
{ 
    QDialog::showEvent(aShowEvent); 
    activateWindow(); 
} 

बाद मैं activateWindow() फ़ंक्शन कॉल जोड़ा मैं QLineEdit पर इस्तेमाल कर सकते हैं मेरे पॉपअप संवाद।

मैं विंडोज 8.1 पर विजुअल स्टूडियो 2013 और क्यूटी 5.4.1 का उपयोग करता हूं।

+0

धन्यवाद में मदद नहीं करता !!! यह मेनू क्रिया में विजेट के लिए भी काम करता है। इसके बिना, यह इनपुट विधि को सक्रिय नहीं कर सका। – tamlok

संबंधित मुद्दे