Launching QWidget from a QMain Window
I am making my first C ++ application - Qt4 and I am having trouble connecting my different uis. I have a main window with multiple buttons and when I click one I want to open another window.
MyMainWindowClass inherits from QMainWindow and the other inherits from QWidget.
Here's the code I've 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 Ctor and Dtor from MyWidgetClass, disables MyMainWindowClassUi, but doesn't show my other GUI. What am I missing for the window to be displayed when I click the button?
source to share
Try this instead of a method 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;
}
source to share