1
votes

I am porting a MFC application to Qt 5.4 and using a QWinWidget as a container for a QDialog derived class. Everything works great except when I type , say, Ctrl+X, Ctrl+V, Ctrl+S etc, then these key combinations are handled by the main MFC document event handlers, by-passing the QT dialog completely. A keyPress() event filter in the QDialog widget does not see them.

QWinWidget *win = new QWinWidget(::AfxGetApp()->m_pMainWnd);
win->showCentered();
CMyDlg dlg(win);
if (dlg.exec()) {
    QMessageBox::information(win, "ModalDialog Result", "OK");
}
else {
    QMessageBox::information(win, "ModalDialog Result", "Cancel");
}
1
I am very familiar with QT and MFC message handling. This is a little corner of the QT world related to moving an app from MFC to QT bit by bit ( not many people do this any more). There are several helper C++ classes QWinWidget and QMFCApp that help. The QMFCApp handles all Windows Messages, creates QT Messages and send them to the appropriate QWidgets. Works great for all "non-modal" Windows. My problem was related to an attempt to create a version of the MFC CDialog. As noted in my answer, the solution is place the QWinWidget inside the CDialogAndrewC

1 Answers

1
votes

Basically I have found this is the "wrong way" to create a Modal Dialog using a QT widget in MFC. The correct way, is to use a MFC CDialog and insert the QWinWidget inside the CDialog.

int CQTControlTestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct){
    if (CDialog::OnCreate(lpCreateStruct)==-1){
        return -1;
    }
    QWinWidget *winWidget = new QWinWidget(this->m_hWnd); 
    CRect rect;
    this->GetClientRect(&rect);
    winWidget->setGeometry(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top-40);

    QHBoxLayout *layout = new QHBoxLayout;  
    layout->addWidget(widget_);  
    winWidget->setLayout(layout);
    winWidget->move(0,0);     
    winWidget->show(); 
    return 0;
}