使用show而不是exec时,QWidget不会更新

时间:2013-06-12 12:11:52

标签: qt qt4 qwidget qt-mfc-migration

在我的项目中,我需要在我的Mfc应用程序的顶部创建一个非阻塞的QWizard。为此,我按照此处的说明(link

这样做有一个奇怪的副作用,当我点击我的QWizardPage中的QRadioButton时,我的UI看起来没有正确更新。请参见以下屏幕截图:

1 - 我的QWizard中的第一步。一切都很好看

First step in my QWizard. Everything looks good

2 - 我点击了QRadioButton。请注意,“下一步”按钮处于奇怪的状态。

I clicked on a QRadioButton.  See, the "Next" button is in a weird state.

3 - 我点击了第二个QRadioButton。看,两个QRadioButton看起来都选中了。 (是的,它们是相互排斥的!

I clicked on the second QRadioButton. See, both QRadioButton look selected.  (Yes, they are mutually exclusive!)

4 - 现在,如果我正在“鼠标悬停”,通过“下一步”按钮和QRadioButton,这会强制更新,我的用户界面很好

Now, if I am doing a "mouse over", over the "Next" button and over QRadioButton, that forces an update and my UI is fine

我的问题是:如何在使用show而不是exec时正确更新我的UI? (我确实尝试过exec并且我的UI正在正确更新)

如果我使用show?

,我猜主线程没有更新我的UI

谢谢。

修改

以下是实例化向导的函数的代码:

void QtMfcFacade::startDevicesConfigurationWizard(HWND hWnd)
{
    QWinWidget* win = new QWinWidget( hWnd );
    win->showCentered();
    DevicesConfigurationWizard *devicesConfigurationWizardUI = new DevicesConfigurationWizard(win);
    devicesConfigurationWizardUI->setModal(true);
    devicesConfigurationWizardUI->show();
}

以下是我的QWizard课程:

DevicesConfigurationWizard::DevicesConfigurationWizard(QWidget *parent, Qt::WFlags flags)
    : QWizard(parent, flags),
{
    ui.setupUi(this);
    this->setWindowFlags( ( (this->windowFlags() | Qt::CustomizeWindowHint)
                       & ~Qt::WindowCloseButtonHint & ~Qt::WindowContextHelpButtonHint) );
    this->setAttribute( Qt::WA_DeleteOnClose, true );
    connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(onClick(int)));
}

void DevicesConfigurationWizard::onClick(int pageId)
{
    if (m_currentPageId < pageId)
    {        
        //user clicked next button.
        switch (pageId)
        {
            case PAGE_NUMBER_SINGLE_USER:
            {
                setupSingleUserPage();
                break;
            }
            default :
            {
                //problem!!!
            }
        }
        m_currentPageId = pageId;
    }
}

void DevicesConfigurationWizard::onRadioButtonClick()
{
    this->button(this->NextButton)->setEnabled(true);
    this->button(this->FinishButton)->setEnabled(true);
}

void DevicesConfigurationWizard::setupSingleUserPage()
{
    this->button(this->NextButton)->setEnabled(false);
    int newPositionY = 0;
        QVBoxLayout* layout = ui.wpSINGLE_USER->findChild<QVBoxLayout*>("verticalLayout");
        for (vector<Events::VCS::PnPDevice>::const_iterator it=m_devices.begin(); it!=m_devices.end(); it++)
        {
            if (it->type == Events::VCS::HEADSET)
            {
                //add a radio button
                stringstream text;
                text <<  (it->name) << " " << (it->serialNumber) ;
                QRadioButton* radioButton = new QRadioButton(this->ui.wpSINGLE_USER);
                radioButton->setGeometry(X, Y + newPositionY, WIDHT, HEIGHT);
                radioButton->setText(text.str().c_str());
                radioButton->setIconSize(QSize(HEIGHT,HEIGHT));
                newPositionY = newPositionY + HEIGHT;
                layout->insertWidget(0, radioButton);
                connect(radioButton, SIGNAL(clicked()), this, SLOT(onRadioButtonClick()));
            }
    }
}

EDIT尝试强制更新到目前为止还没有解决问题

void DevicesConfigurationWizard::onRadioButtonClick()
{
    this->button(this->NextButton)->setEnabled(true);
    this->button(this->FinishButton)->setEnabled(true);
    QWidget::update();
    this->update();
    this->button(this->NextButton)->update();
    QList<QRadioButton*> listButton = ui.wpSINGLE_USER->findChildren<QRadioButton*>();
    listButton[0]->update();
    listButton[1]->update();
    QApplication::processEvents();
}

2 个答案:

答案 0 :(得分:0)

也许这会有所帮助:

void DevicesConfigurationWizard::setupSingleUserPage()
{
    this->button(this->NextButton)->setEnabled(false);
    int newPositionY = 0;
        QVBoxLayout* layout = ui.wpSINGLE_USER->findChild<QVBoxLayout*>("verticalLayout");
        QButtonGroup* group = new QButtonGroup(this); // add this
        for (vector<Events::VCS::PnPDevice>::const_iterator it=m_devices.begin(); it!=m_devices.end(); it++)
        {
            if (it->type == Events::VCS::HEADSET)
            {
                //add a radio button
                stringstream text;
                text <<  (it->name) << " " << (it->serialNumber) ;
                QRadioButton* radioButton = new QRadioButton(this->ui.wpSINGLE_USER);
                group->addButton(radioButton); // and add this
                radioButton->setGeometry(X, Y + newPositionY, WIDHT, HEIGHT);
                radioButton->setText(text.str().c_str());
                radioButton->setIconSize(QSize(HEIGHT,HEIGHT));
                newPositionY = newPositionY + HEIGHT;
                layout->insertWidget(0, radioButton);
                connect(radioButton, SIGNAL(clicked()), this, SLOT(onRadioButtonClick()));
            }
    }
}

当我尝试在上下文菜单中插入单选按钮时遇到了类似的问题。

soo long zai

答案 1 :(得分:0)

问题来自我的Mfc应用程序。计时器每100毫秒向事件循环发送一个计时器事件,因此,我的Qt应用程序无法正常更新。所以,我做了什么,我在Mfc应用程序中将QCoreApplication :: processEvents()添加到OnTimer中。

这样,QCoreApplication :: processEvents每100 milisedond调用一次,我的Qt UI现在正确更新。