代码之家  ›  专栏  ›  技术社区  ›  Opera

从QMain窗口启动QWidget

  •  2
  • Opera  · 技术社区  · 15 年前

    我有一个有几个按钮的主窗口,当我点击一个按钮时,我想打开另一个窗口。

    以下是我迄今为止编写的代码:

    
    #include <iostream>
    #include "MyWidgetClass.hpp"
    #include "MyMainWindowClass.hpp"
    #include "ui_MyMainWindowClassUi.h"
    
    

    MyMainWindowClass::MyMainWindowClass(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::MyMainWindowClassUi) { m_ui->setupUi(this); initConnect(); }

    void MyMainWindowClass::initConnect() { QObject::connect(m_ui->SomeBtn, SIGNAL(clicked()), this, SLOT(SomeBtnClicked())); // Some other QObject::connect calls return; }

    void MyMainWindowClass::SomeBtnClicked() { std::cout << "Some Btn has been clicked" << std::endl; this->setEnabled(false); MyWidgetClass mwc(this); mwc.show(); return; }

    这将从MyWidgetClass调用Ctor和Dtor,禁用MyMainWindowClassUi,但不显示其他GUI。当我点击按钮时,我错过了什么让窗口显示出来?

    1 回复  |  直到 15 年前
        1
  •  3
  •   Philip Daubmeier    15 年前

    试试这个而不是你的 SomeBtnClicked 方法:

    MyWidgetClass *mwc;
    
    void MyMainWindowClass::SomeBtnClicked()
    {
        std::cout << "Some Btn has been clicked" << std::endl;
        this->setEnabled(false);
    
        if (!mwc)
            mwc = new MyWidgetClass(this);
        mwc->show();
        mwc->raise();
        mwc->setActiveWindow();  // Qt 4: activateWindow()
    
        return;
    }