Important: Please read the Qt Code of Conduct -

Return a value from a QWidget

  • Hello everybody,

    I'm a beginner with Qt and I can't find a solution to my problem.

    I want to have a login window, the user would then enter its login and password, and then, the main window will open according to the login information. (it can be a normal user, a super user, or wrong credentials)

    I don't want to use a QDialog for the login window, because I already have a class that implements the design of my software (title bar, logo and so on), so I would like to inherit from my own class, which itself inherit from QWidget, so that this class will open, and then wait for the user to enter its login informations.

    I already implemented the design of this window but I don't know how to block until the user puts his information. Moreover, I really want a return value, something like the exec() function of the QDialog. I looked at its implementation, but I can't understand it, and I guess something easier is possible.

    Thank you!

  • It sounds like you're on the right track. Using QDialog::exec will start a modal dialog, which will block input to other windows in your application until the user enters input. Your QDialog instance could send a signal to a class that will verify credentials and return a value via signal that will allow the QDialog to determine whether it can be be accepted and close. The credential checking class can then signal the rest of your application to allow what features you want user types to have. If you post some code samples, people can help you more with your architecting process.

  • Thanks for your message.
    However, my point is that I don't want to use QDialog, but my own class instead, which inherits from QWidget.
    Therefore I need to implement the exec() function for that class.

    Let's say I have a class MyQWidget that inherits from QWidget. I want to do something like that:

    @MyQWidget qw;
    int m = qw.exec();

    if (m == 0) blablabla...@

  • Why not inheriting from QDialog, or adding the inherited QWidget to a QDialog.

  • My example was wrong, but correct in my first post.
    I have a class that implements the design of my software, let's call it myWindow.
    This class inherits from QWidget.

    To create my login window, I want to inherit from myWindow, therefore I can't inherit from QDialog.

    Adding "myWindow" inside a QDialog seems to be a good solution, however I didn't find a good way to do this, as myWindow redefine everything including the title bar, I end up with two title bar....

  • Sorry to say that, but either you use the QDialog class (using your verification logic etc. of MyWindow) which will prove easier than you might think now or you end up re-inventing QDialog again.

  • But can I have my own design when using QDialog ?
    QDialog already has a title bar with the close button etc(with the Windows design), MyWindow has this bar too with my own design, so I end up with two title bars.
    Is it possible to remove everything in the QDialog?

  • Hi,

    For a QDialog try using this.

    dialog->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

    That should remove the title bar and all the buttons, in theory. If you insist on using your own format, you can use


    before you execute your custom dialog, just remember to use


    when your dialog is done or else your code will not process anymore events. As for returning values you may have to create a signal to send the information from your custom dialog.

Log in to reply