2013-11-14 38 views

回答

1

對話框初始化:

void MainWindow::on_button1_clicked() { 
    dialog = new QInputDialog(); 
    dialog->installEventFilter(this); 
    dialog->show(); 
} 

事件過濾器:

bool MainWindow::eventFilter(QObject *o, QEvent *e) { 
    if (e->type() == QEvent::KeyPress) { 
    if (static_cast<QKeyEvent*>(e)->matches(QKeySequence::InsertParagraphSeparator)) { 
     qDebug() << dialog->textValue(); //use this value as you wish 
     dialog->setTextValue(QString()); 
     return true; //block this event 
    } 
    } 
    return false; 
} 

注意,對話仍然可以使用鼠標單擊 「確定」 關閉。