How to organize a system of parent and child windows that hide their parent. QT

Let's say I have a main window.

Then, when I click the button, I want the child window to open and those main windows to hide. Then when I close that child window, I want the main windows to reappear.

Right now I am opening this child window by doing:

s=new SignUpWindow(NULL,temp);
s->show();

      

where s is the private pointer of my main window.

When I try to do:

s=new SignUpWindow(this,temp);
s->show();

      

The s window is not displayed.

Here is the signature of its constructor in the header:

SignUpWindow (QWidget* parent=NULL, Netflix *n=NULL);

      

Can anyone explain why we are setting the parent to NULL in the title? Sometimes I get problems when I try to play around with the parameters and I get errors like:

candidate expects 1 argument, 2 supplied by qt

Thanks for the help -A tired college student was just learning qt

UPDATE is basically the title for my main window:

class LoginWindow : public QWidget
{

    Q_OBJECT

    public:
        LoginWindow (QWidget* parent=NULL, Netflix *n=NULL);

    public slots:
        void loggedIn();
        void newUser();
        void quitPushed();


    private:
        QPushButton *quitButton, *loginButton, *newUserButton;
        QLineEdit *login;//this is the text area that takes in the loginID    
};

#endif

      

that is, a function triggered by a button click that opens new windows:

void LoginWindow::newUser()
{
        s=new SignUpWindow(NULL,temp);
        s->show();
        //this->hide();
}

      

How to connect s to LogInWindow?

UPDATE 2 SignUpWIndow.h:

class SignUpWindow : public QWidget
{

    Q_OBJECT

    public:
        SignUpWindow (QWidget* parent=NULL, Netflix *n=NULL);


    public slots:

    private:
 };

      

SignUpWindow.cpp:

SignUpWindow::SignUpWindow (QWidget* parent, Netflix *n) : QWidget (parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout;
    //then i add things like buttons and group boxes and etc but no dialogs...
}

      

I have never done a dialog for my SignUpWindow. I just added layouts and buttons, etc.

+3


source to share


2 answers


When created signUpWindow

inside, loginWindow

set this

as parent. In this case, you will be able to call parentWidget()

inside signUpWindow

and call hide()

, when you close signUpWindow

, call parentWidget()

again and call show().

. It will only work if the parent is signUpWindow

loginWindow

.

In the parent code NULL

, but it works when parent this

.

But yours signUpWindow

must be dialog

either:

If signUpWindow

is widget

, when you install your parent signUpWindow

appears in the parent, but QDialog

will appear in a separate window. If you use a subclass QDialog

, you set it as parent and use my solution, but if you use a subclass QWidget

, you set it NULL

as parent and you cannot use relations parent-child

, so you must use signals and slots (catch signal from signUpWindow

and show or hide yours loginWindow

) ... Make your choice, but note which is QDialog

more suitable for the task.

Also I suggest you use closeEvent

to make sure you can close the closure when the user clicks close button

.

I wrote this dialog and I tested it, works great:

Title:



#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QCloseEvent>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);

    ~Dialog();

protected:

    void closeEvent(QCloseEvent *);

private:
    Ui::Dialog *ui;


};

#endif // DIALOG_H

      

Cpp:

//constructor
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    parentWidget()->hide();
}

//closeEvent
void Dialog::closeEvent(QCloseEvent *)
{
    parentWidget()->show();
}

      

Usage (inside mainWindow):

Dialog *mDialog = new Dialog(this);
mDialog->show();

      

As you can see, I am setting the parent, but the dialog appears as a separate window and you can still use the link parent-child

.

Works the way you want it to be and it's very easy, just add a few lines of code.

+1


source


Can anyone explain why we are setting the parent to NULL in the title?

You don't set the parent to NULL, you provide a default value for the parent if not passed. This is to avoid creating an extra constructor for cases where you create an object without a parent, and it works the same, you just omit the parent pass and use the default NULL instead.


Do you need to use QWidget

s? Keep in mind that this module is no longer actively developed in Qt, and it exists for backward compatibility only. Qt GUI now focuses on QtQuick which is much faster and easier to work with.



QtQuick even provides a ready-to-use component StackView

that does just that, puts a new component (QML widget) on top of the parent, which is hidden until a new window appears, after which the parent window reappears, it even has funky animation when showing and hiding components ...

Also QtQuick provides QtQuick Controls , which are implemented to look native to the platform, so they will look the same as the old QWidget

components.

Just to give you an idea of ​​how easy it is to use QtQuick, here's a quick example. It will display a randomly colored dialog with text that says which level it is on and 2 buttons - one to create another dialog on top of it and the other to close that dialog or application if this is the first dialog:

ApplicationWindow {
    visible: true
    width: 200
    height: 200

    function randomColor() { return Qt.lighter(Qt.rgba(Math.random(),Math.random(), Math.random(), 1))}

    StackView {
        id: stack
    }

    Component {
        id: dialog

        Rectangle {
            color: randomColor()
            Column {
                Text {
                    text: "We are on level " + stack.depth
                }
                Row {
                    Button {
                        text: "Snow Another"
                        onClicked: stack.push(dialog)
                    }
                    Button {
                        text: "Close"
                        onClicked: {
                            if (stack.depth != 1) stack.pop()
                            else Qt.quit()
                        }
                    }
                }
            }
        }
    }

    Component.onCompleted: stack.push(dialog)
}

      

+1


source







All Articles