How to call a new window on click of a pushbutton in Qt?



  • I am using calculatorform as my main window. But I want total to be displayed in a new window and not the main window. I tried adding a push button through calculatorform.ui and call the window , but I suppose I am not writing the correct code. Could anyone please help me doing this?


  • Moderators

    welcome to devnet

    When adding anything to an existing window and doing the necessary creation/compiling steps the already existing window will change.

    Apparently you want to have another window with a different layout. You need to create a new window object with all the files required (.ui, .cpp, .h) and create an instance of this window in your program.

    Did you start and go through "the step-by-step tutorials? ":https://qt-project.org/doc/qt-4.8/tutorials.html



  • Yes i know that. i want to know what function should i write at main.cpp where button is present?


  • Moderators

    If you like to see another window instead of the one which is used at the moment, you simply have to substitute with the name of the new window prior to compilation.

    However, if you want to change it dynamically, well, it is a bit more complicated than just adding another function to main.cpp. I would recommend to go through some of the related "examples for dialogs":https://qt-project.org/doc/qt-4.8/examples-dialogs.html . May the "extension example":https://qt-project.org/doc/qt-4.8/dialogs-extension.html helps you .



  • Would be better if you gave some more info, like QT-C++ or QML, or both and some code.
    Please don't expect from others to dig up the example you are using (or at least give the link).
    I have an application with several dialogs and subwindows, so I might help you, but it is not clear to me what you have tried until now.



  • i am having main window and subwindow.
    both having different .cpp , .h and .ui file
    i want that when i push a push button then subwindow should open.
    i used show() but it will not work for me . It will show me main window as if as a new window instead of subwindow.
    what should i do please resolve this problem.
    thanks in advance ...



  • ndv472 this thread is two years old.
    Could you start a new thread with your question.



  • ok sir



  • include your form class in your mainwindow .h file
    then declare and object of your class e.g MyClass *class
    create function within your mainwindow.cpp
    {
    class = new MyClass(this)
    MyClass->show();
    }
    then simply call the function on your push button


Log in to reply
 

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