why my widget does not redraw itself?



  • i create a QWidget, then set it as a child of a MFC CDialog, everything seems good, but when i change the size of My Dialog, the widget does not redraw itself correctly, how could i fix this problem? thanks a lot! my code mainly here:

    #include "stdafx.h"
    #include "test.h"
    #include "testDlg.h"
    #include <QApplication>
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    UINT ThreadProc(LPVOID cookie);
    
    // CtestApp
    
    BEGIN_MESSAGE_MAP(CtestApp, CWinApp)
    	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
    END_MESSAGE_MAP()
    
    
    // CtestApp construction
    
    CtestApp::CtestApp()
    {
    	m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
    }
    
    
    // The one and only CtestApp object
    
    
    CtestApp theApp;
    
    
    BOOL CtestApp::InitInstance()
    {
    	INITCOMMONCONTROLSEX InitCtrls;
    	InitCtrls.dwSize = sizeof(InitCtrls);
    	InitCtrls.dwICC = ICC_WIN95_CLASSES;
    	InitCommonControlsEx(&InitCtrls);
    
    	CWinApp::InitInstance();
    
    	AfxEnableControlContainer();
    	CShellManager *pShellManager = new CShellManager;
    
    	SetRegistryKey(_T("Local AppWizard-Generated Applications"));
    
    	AfxBeginThread(ThreadProc, this);
    
    	CtestDlg dlg;
    	m_pMainWnd = &dlg;
    	INT_PTR nResponse = dlg.DoModal();
    	if (nResponse == IDOK)
    	{
    	}
    	else if (nResponse == IDCANCEL)
    	{
    
    	}
    
    	if (pShellManager != NULL)
    	{
    		delete pShellManager;
    	}
    
    	return FALSE;
    }
    
    HWND wid = NULL;
    QWidget* pW = NULL;
    QApplication* pApp = NULL;
    
    UINT ThreadProc(LPVOID cookie)
    {
    	int iArgc = 0;	
    	QApplication qtApp(iArgc, NULL);
    
    	QWidget widget;
    	QHBoxLayout *hbox = new QHBoxLayout(&widget);
    	QLabel *label = new QLabel("Enter text:", &widget);
    	QLineEdit *edit = new QLineEdit(&widget);
    	hbox->addWidget(label);
    	hbox->addWidget(edit);
    	widget.show();
    
    	wid = HWND(widget.winId());
    	pW = &widget;
    
    	pApp = &qtApp;
    
    	return qtApp.exec();
    }
    
    int CtestApp::ExitInstance()
    {
    	return CWinApp::ExitInstance();
    }
    
    
    /////////////////////////////////////////////////////////////////////////
    #include "stdafx.h"
    #include "test.h"
    #include "testDlg.h"
    #include "afxdialogex.h"
    #include <QApplication>
    
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    extern HWND wid;
    extern QApplication* pApp;
    extern QWidget* pW;
    
    // CAboutDlg dialog used for App About
    
    class CAboutDlg : public CDialogEx
    {
    public:
    	CAboutDlg();
    
    // Dialog Data
    	enum { IDD = IDD_ABOUTBOX };
    
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    // Implementation
    protected:
    	DECLARE_MESSAGE_MAP()
    };
    
    CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
    {
    }
    
    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialogEx::DoDataExchange(pDX);
    }
    
    BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
    END_MESSAGE_MAP()
    
    
    
    CtestDlg::CtestDlg(CWnd* pParent /*=NULL*/)
    	: CDialogEx(CtestDlg::IDD, pParent)
    {
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CtestDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialogEx::DoDataExchange(pDX);
    }
    
    BEGIN_MESSAGE_MAP(CtestDlg, CDialogEx)
    	ON_WM_SYSCOMMAND()
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_BN_CLICKED(IDC_BUTTON1, &CtestDlg::OnBnClickedButton1)
    	ON_BN_CLICKED(IDC_BUTTON2, &CtestDlg::OnBnClickedButton2)
    	ON_WM_DESTROY()
    END_MESSAGE_MAP()
    
    BOOL CtestDlg::OnInitDialog()
    {
    	CDialogEx::OnInitDialog();
    
    	//ModifyStyle(NULL, WS_CLIPCHILDREN);
    
    	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    	ASSERT(IDM_ABOUTBOX < 0xF000);
    
    	CMenu* pSysMenu = GetSystemMenu(FALSE);
    	if (pSysMenu != NULL)
    	{
    		BOOL bNameValid;
    		CString strAboutMenu;
    		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
    		ASSERT(bNameValid);
    		if (!strAboutMenu.IsEmpty())
    		{
    			pSysMenu->AppendMenu(MF_SEPARATOR);
    			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    		}
    	}
    
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    void CtestDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    	{
    		CAboutDlg dlgAbout;
    		dlgAbout.DoModal();
    	}
    	else
    	{
    		CDialogEx::OnSysCommand(nID, lParam);
    	}
    }
    
    
    void CtestDlg::OnPaint()
    {
    	if (IsIconic())
    	{
    		CPaintDC dc(this);
    		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
    
    		int cxIcon = GetSystemMetrics(SM_CXICON);
    		int cyIcon = GetSystemMetrics(SM_CYICON);
    		CRect rect;
    		GetClientRect(&rect);
    		int x = (rect.Width() - cxIcon + 1) / 2;
    		int y = (rect.Height() - cyIcon + 1) / 2;
    	}
    	else
    	{
    		CDialogEx::OnPaint();
    	}
    
    	if (pW)
    	{
    		pW->repaint();
    		pW->update();
    	}
    }
    
    HCURSOR CtestDlg::OnQueryDragIcon()
    {
    	return static_cast<HCURSOR>(m_hIcon);
    }
    
    void CtestDlg::OnBnClickedButton1()
    {
    	//QMessageBox dlg;
    	//dlg.setIcon(QMessageBox::Question); 
    	//dlg.warning(0,QObject::tr("waring"),QObject::tr("this is messagebox used by qt create!"),QMessageBox::Ok);
    }
    
    
    //class MyWidget : QWidget
    //{
    //
    //public:
    //	void createx(UINT_PTR WId = 0, bool initializeWindow = true,
    //		bool destroyOldWindow = true)
    //	{
    //		__super::create(WId, initializeWindow, destroyOldWindow);
    //	}
    //
    //};
    
    void CtestDlg::OnBnClickedButton2()
    {
    	if (wid)
    	{
    		HWND hWnd = wid;
    		::SetParent(hWnd, m_hWnd);
    		DWORD dw = GetWindowLong(hWnd, GWL_STYLE);
    		dw |= WS_CHILD;
    		//dw &= ~WS_CAPTION | WS_OVERLAPPED;
    		SetWindowLong(hWnd, GWL_STYLE, dw);
    
    		::MoveWindow(hWnd, 160, 10, 320, 100, TRUE);
    	}
    }
    
    
    void CtestDlg::OnDestroy()
    {
    	if (pApp)
    		pApp->exit(0); // 防止强制关闭QT线程导致的内存泄漏
    
    
    	CDialogEx::OnDestroy();
    }
    


  • What I glean from < This threadt > is that tring to mix Qt and MFC is not recommended.


  • Qt Champions 2018

    Hi @Joshua-Qt,

    why do you actually want to mix MFC and Qt?

    You should be aware, that Qt (same as MFC) uses an event loop in background. If you want to combine both, both event loops must be run. That is surely not impossible, but advanced stuff that needs a deep understanding on what's going on in background.

    Regards


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.