Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMdiArea not adding subwindow

Tags:

qt

I have an one function which is responsible for initializing custom widget and add it in to the MdiArea.When I call this at first time it is working fine.But if i will call again, it is initializing custom widget but not adding in into the MdiArea. I have mentioned that function here:-

void CArbWaveViewWidget::newFile()
{
     m_ptrWavePresenter = new CArbWavePresenter;

     QMdiSubWindow *subWindow1 = new QMdiSubWindow;
     subWindow1->setWidget(m_ptrWavePresenter->getTableView()); // getting customWidget
     qDebug()<<"Table View ==="<<m_ptrWavePresenter->getTableView();
     subWindow1->setAttribute(Qt::WA_DeleteOnClose);

     QMdiSubWindow *subWindow2 = new QMdiSubWindow;
     subWindow2->setWidget(m_ptrWavePresenter->getGraphView()); // getting customWidget
     qDebug()<<"Graph View ==="<<m_ptrWavePresenter->getGraphView();
     subWindow2->setAttribute(Qt::WA_DeleteOnClose);
     mdiArea->addSubWindow(subWindow1);
     mdiArea->addSubWindow(subWindow2); 
}

How can i solve this problem?

like image 848
Suresh R Avatar asked Dec 30 '25 04:12

Suresh R


2 Answers

1. Declare your QMdiSubWindow

When you declare a QMdiSubWindow, give mdiArea as argument

QMdiSubWindow *subWindow = new QMdiSubWindow(mdiArea);

or you can use setParent ( QWidget * parent )

QMdiSubWindow *subWindow = new QMdiSubWindow();
subWindow->setParent(mdiArea);

2. Create and add your QWidget in your QMdiSubWindow

QWidget *myWidget = new QWidget();
subWindow->setWidget(myWidget);

3. Update QMdiSubWindow content

If you need to update the subwindow content, declare your QMdiSubWindow as class variable, initialize your QMdiArea and QMdiSubWindow and set QWidget

yourClass.h

class yourClass {

public:
    yourClass();
    void newFile();

private:
    QMdiArea *m_area;
    QMdiSubWindow *m_subWindow1, *m_subWindow2;

    void init();
};

yourClass.cpp

yourClass::yourClass()
{
    init();
}

void yourClass::init()
{
    m_area = new QMdiArea();
    m_subWindow1 = new QMdiSubWindow(m_area);
    m_subWindow2 = new QMdiSubWindow(m_area);
    // continue to init your QMdiSubWindow
}

void yourClass::newFile()
{
    // Set your QWidget (yourWidget) into your QMdiSubWindow
    m_subWindow1->setWidget(yourWidget);
    m_subWindow2->setWidget(anotherWidget);
}
like image 140
Rémi Avatar answered Jan 01 '26 20:01

Rémi


You need invoke method "show"

QMdiSubWindow *subWindow = new QMdiSubWindow();
subWindow->setParent(mdiArea);
subWindow->setWidget(yourWidget);
subWindow->show();
like image 31
Vlad Weird Avatar answered Jan 01 '26 19:01

Vlad Weird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!