开发者

Launch a QWidget from a QMain window

开发者 https://www.devze.com 2022-12-26 04:16 出处:网络
I\'m doing my first C++ - Qt4 application and I\'m having some trouble \"connecting\" my different uis.

I'm doing my first C++ - Qt4 application and I'm having some trouble "connecting" my different uis. I have a main window with several buttons and when I click on one, I want another window to open.

The MyMainWindowClass inherits from QMainWindow and the other from QWidget.

Here is the code I have written so far :


#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; }

This calls the Ctor and the Dtor from MyWidgetClass, disables the MyMainWindowClassUi, but doesn't开发者_StackOverflow社区 show my other GUI. What am I missing to have the window showed when I click on the button ?


Try this instead of your SomeBtnClicked method:

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号