Create a simple constructor with parameters

  • I'm trying to make it so my widget will open a new window when a button is pressed, and there will be things in that window. I'm pretty sure I need a new class to do this, and I need variables from the main window in this new class, so I'm going to need a constructor with parameters. I have the constructor that comes with each class, that has only the QWidget parent as a parameter:

    explicit MyWindow(QWidget *parent = 0);

    but everytime I try to add a new variable to that list it gives me an error (missing default parameter for parameter 2) or a warning (unreferenced formal parameter). I'm also not certain how to pass pointers to when I create the new object, as I don't know what to pass for the parent parameter, would it just be "this"?

    Basically, need to make a new class, and pass it pointers to variables, so that I can make a window launched from another window. Can someone help me?

    EDIT: I think I actually figured out how to add the parameters, the same was as in C++, but the compiler was barking at me because I was still designing it and hadn't implemented any definition for the parameters (giving me the unreferenced formal parameter warning). Still not sure how to create a new object of it, though.

  • If your new class inherits QWidget than you can pass this as parameter otherwise you dont need to pass the parent parameter as it had default value 0.
    The reason behind passing this parameter is that when the parent gets deleted than child will also automatically deleted.

  • Alright thanks, I think that worked.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.