Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show another window from mainwindow in QT

Platform: QT, Windows XP

I am new to Qt. I want to show another window(what to do to open it as dialog) from mainwindow. I did "add New Item ->Qt Designer Form Class", named it say MyWindow. But how to show this MyWindow from mainwindow ?

like image 895
Samir Avatar asked Oct 05 '09 04:10

Samir


People also ask

How do I change the window name in Qt?

EDIT: If you are using QtDesigner, on the property tab, there is an editable property called windowTitle which can be found under the QWidget section. The property tab can usually be found on the lower right part of the designer window.

What is the difference between QMainWindow and QWidget?

A QDialog is based on QWidget , but designed to be shown as a window. It will always appear in a window, and has functions to make it work well with common buttons on dialogs (accept, reject, etc.). QMainWindow is designed around common needs for a main window to have.


1 Answers

  1. Implement a slot in your QMainWindow where you will open your new Window,
  2. Place a widget on your QMainWindow,
  3. Connect a signal from this widget to a slot from the QMainWindow (for example: if the widget is a QPushButton connect the signal click() to the QMainWindow custom slot you have created).

Code example:

MainWindow.h

// ... include "newwindow.h" // ... public slots:    void openNewWindow(); // ... private:    NewWindow *mMyNewWindow; // ... } 

MainWindow.cpp

// ...    MainWindow::MainWindow()    {       // ...       connect(mMyButton, SIGNAL(click()), this, SLOT(openNewWindow()));       // ...    } // ... void MainWindow::openNewWindow() {    mMyNewWindow = new NewWindow(); // Be sure to destroy your window somewhere    mMyNewWindow->show();    // ... } 

This is an example on how display a custom new window. There are a lot of ways to do this.

like image 69
Patrice Bernassola Avatar answered Sep 21 '22 13:09

Patrice Bernassola