PDA

View Full Version : How to Close SubWindow in MdiArea



marcos.miranda
28th June 2016, 18:40
Hi everyone.
I need a little help.

I am developing an application using MdiArea and can not close another
form through its close button (PushButton).

Can someone show me where I am going wrong, follows code below.



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "formwidget.h" -----------> Include of SubWindow

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_actionClients_triggered();

private:
Ui::MainWindow *ui;
};




#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setCentralWidget(ui->mdiArea); ---------> Centering MdiArea

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_actionClients_triggered()
{
FormWidget *frmClients = new FormWidget;
ui->mdiArea->addSubWindow(frmClients);
frmClients->show(); -----------------------------> Instantiating the subwindow
}




#include "formwidget.h"
#include "ui_formwidget.h"

FormWidget::FormWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::FormWidget)
{
ui->setupUi(this);
}

FormWidget::~FormWidget()
{
delete ui;
}

void FormWidget::on_pushButton_clicked()
{
close(); --------> Does not work, the widget disappears (LineEdit, Label, Pushbutton) and the form does not close.
}


I thank the help.

João Marcos .

marcos.miranda
28th June 2016, 23:03
Someone who can help.

anda_skoa
29th June 2016, 08:54
You are calling close on the content of the sub window, not on the sub window itself.

Cheers,
_

marcos.miranda
29th June 2016, 12:16
Regards, Anda_Skoa.

Thanks for helping me.
You have an example of how would this code.
For the lock button is in the form and the form within the MdiArea.

PS: Thank you to format my post.

Regards,

João Marcos

anda_skoa
29th June 2016, 14:56
You could emit a signal from the FormWidget and connect that to the sub window's close slot.

Or could check if the sub window is the FormWidget's parent or grand parent and call close directly.

Cheers,
_

marcos.miranda
29th June 2016, 15:51
Hi, Anda_Skoa.

Following its first orientation, I created a button sign for the form (Close slot) and did not work. I used QtCreator for this.

I searched for any subroutine or function and bumped into "this-> parentWidget () -> close ();" it worked !!! :D.

I think that fits in its second orientation.

I appreciate your help enough, and I hope this information will help other developers.

PS: How to check if the Sub Window is the parent or grand FormWidget's?

Cheers,

João Marcos

anda_skoa
30th June 2016, 09:50
Following its first orientation, I created a button sign for the form (Close slot) and did not work.

What did not work?
Did you connect the button's signal to your signal or did you emit your signal from the existing slot?
Did you connect your signal to the mdi sub window's slot?



I searched for any subroutine or function and bumped into "this-> parentWidget () -> close ();" it worked !!! :D.

As I expected.



PS: How to check if the Sub Window is the parent or grand FormWidget's?

By casting the parent to the class you want to check for using a cast that checks if it is possible, e.g. qobject_cast



QMdiSubWindow *window = qobject_cast<QMdiSubWindow*>(parentWidget());
if (window != 0) // -> parent is the sub window


Cheers,
_