0
votes

I have done simple Qt programming, for that I have two windows MainWindow and NewWindow. MainWindow is having one push button called mMyButton. My mainwindow.h is:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
//added
#include "newwindow.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

public slots:
  void openNewWindow();

//added name of new window
private:
  NewWindow *mMyNewWindow;

private:
  Ui::MainWindow *ui;

private slots:
  void on_mMyButton_clicked();
};

#endif // MAINWINDOW_H

my newwindow.h is:

#ifndef NEWWINDOW_H
#define NEWWINDOW_H

#include <QMainWindow>

namespace Ui {
class NewWindow;
}

class NewWindow : public QMainWindow
{
  Q_OBJECT

public:
  explicit NewWindow(QWidget *parent = 0);
  ~NewWindow();

private:
  Ui::NewWindow *ui;
};

#endif // NEWWINDOW_H

my mainwindow.ccp is:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);
  //added
  connect(ui->mMyButton, SIGNAL(click()), this, SLOT(openNewWindow()));
}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::openNewWindow()
{
  mMyNewWindow = new NewWindow();

  mMyNewWindow->show();

}

void MainWindow::on_mMyButton_clicked()
{
  openNewWindow();

}

my newwindow.ccp is:

#include "newwindow.h"
#include "ui_newwindow.h"

NewWindow::NewWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::NewWindow)
{
  ui->setupUi(this);
}

NewWindow::~NewWindow()
{
  delete ui;
}

And my main.ccp is :

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  MainWindow w;
  w.show();

  return a.exec();
}

Above code will generate the window like:enter image description here

and after clicking on push button the another window will open which is as:enter image description here

The above code of Qt is working for opening new window after clicking push button. And my converted .py code mainwindow.py is:

from PyQt4 import QtCore, QtGui

try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(400, 300)
    self.centralWidget = QtGui.QWidget(MainWindow)
    self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
    self.mMyButton = QtGui.QPushButton(self.centralWidget)
    self.mMyButton.setGeometry(QtCore.QRect(140, 90, 99, 27))
    self.mMyButton.setObjectName(_fromUtf8("mMyButton"))
    MainWindow.setCentralWidget(self.centralWidget)
    self.menuBar = QtGui.QMenuBar(MainWindow)
    self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 25))
    self.menuBar.setObjectName(_fromUtf8("menuBar"))
    MainWindow.setMenuBar(self.menuBar)
    self.mainToolBar = QtGui.QToolBar(MainWindow)
    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
    MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
    self.statusBar = QtGui.QStatusBar(MainWindow)
    self.statusBar.setObjectName(_fromUtf8("statusBar"))
    MainWindow.setStatusBar(self.statusBar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

  def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    self.mMyButton.setText(QtGui.QApplication.translate("MainWindow", "Addition", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
  import sys
  app = QtGui.QApplication(sys.argv)
  MainWindow = QtGui.QMainWindow()
  ui = Ui_MainWindow()
  ui.setupUi(MainWindow)
  MainWindow.show()
  sys.exit(app.exec_())

And newwindow.py is :

# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui

try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

class Ui_NewWindow(object):
  def setupUi(self, NewWindow):
    NewWindow.setObjectName(_fromUtf8("NewWindow"))
    NewWindow.resize(439, 225)
    self.centralwidget = QtGui.QWidget(NewWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
    self.pushButton_2.setGeometry(QtCore.QRect(170, 140, 99, 27))
    self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
    self.widget = QtGui.QWidget(self.centralwidget)
    self.widget.setGeometry(QtCore.QRect(40, 30, 365, 89))
    self.widget.setObjectName(_fromUtf8("widget"))
    self.verticalLayout = QtGui.QVBoxLayout(self.widget)
    self.verticalLayout.setMargin(0)
    self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
    self.horizontalLayout_2 = QtGui.QHBoxLayout()
    self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
    self.label_3 = QtGui.QLabel(self.widget)
    self.label_3.setObjectName(_fromUtf8("label_3"))
    self.horizontalLayout_2.addWidget(self.label_3)
    self.lineEdit = QtGui.QLineEdit(self.widget)
    self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    self.horizontalLayout_2.addWidget(self.lineEdit)
    self.label_4 = QtGui.QLabel(self.widget)
    self.label_4.setObjectName(_fromUtf8("label_4"))
    self.horizontalLayout_2.addWidget(self.label_4)
    self.lineEdit_2 = QtGui.QLineEdit(self.widget)
    self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
    self.horizontalLayout_2.addWidget(self.lineEdit_2)
    self.verticalLayout.addLayout(self.horizontalLayout_2)
    self.pushButton = QtGui.QPushButton(self.widget)
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.verticalLayout.addWidget(self.pushButton)
    self.horizontalLayout = QtGui.QHBoxLayout()
    self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
    self.label = QtGui.QLabel(self.widget)
    self.label.setObjectName(_fromUtf8("label"))
    self.horizontalLayout.addWidget(self.label)
    self.label_2 = QtGui.QLabel(self.widget)
    self.label_2.setObjectName(_fromUtf8("label_2"))
    self.horizontalLayout.addWidget(self.label_2)
    self.verticalLayout.addLayout(self.horizontalLayout)
    NewWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtGui.QMenuBar(NewWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 25))
    self.menubar.setObjectName(_fromUtf8("menubar"))
    NewWindow.setMenuBar(self.menubar)
    self.statusbar = QtGui.QStatusBar(NewWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))
    NewWindow.setStatusBar(self.statusbar)

    self.retranslateUi(NewWindow)
    QtCore.QMetaObject.connectSlotsByName(NewWindow)

  def retranslateUi(self, NewWindow):
    NewWindow.setWindowTitle(QtGui.QApplication.translate("NewWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_2.setText(QtGui.QApplication.translate("NewWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
    self.label_3.setText(QtGui.QApplication.translate("NewWindow", "A = ", None, QtGui.QApplication.UnicodeUTF8))
    self.lineEdit.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
    self.label_4.setText(QtGui.QApplication.translate("NewWindow", "B = ", None, QtGui.QApplication.UnicodeUTF8))
    self.lineEdit_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("NewWindow", "Add", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("NewWindow", "Answer  : ", None, QtGui.QApplication.UnicodeUTF8))
    self.label_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
  import sys
  app = QtGui.QApplication(sys.argv)
  NewWindow = QtGui.QMainWindow()
  ui = Ui_NewWindow()
  ui.setupUi(NewWindow)
  NewWindow.show()
  sys.exit(app.exec_())

here the above code of python is not working well means from main window after clicking push button the new window is not opening, so please suggest what changes in python code is required so i can open new window.

1
Your question is quite unclear. Are you trying to embed python in your C++ application? Or port your application from C++ to python? Or what, exactly?ekhumoro

1 Answers

0
votes

This one will work. However, it's extremely ugly and glued together. In short: You can't have two main windows. What you likely want is a QDialog or an additional QWidget. I strongly suggest you to read a PySide or PyQT beginners tutorial, f.e. http://zetcode.com/gui/pysidetutorial/

from PySide import QtCore, QtGui

try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    self.mainwindow=MainWindow
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(400, 300)
    self.centralWidget = QtGui.QWidget(MainWindow)
    self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
    self.mMyButton = QtGui.QPushButton(self.centralWidget)
    self.mMyButton.setGeometry(QtCore.QRect(140, 90, 99, 27))
    self.mMyButton.setObjectName(_fromUtf8("mMyButton"))
    MainWindow.setCentralWidget(self.centralWidget)
    self.menuBar = QtGui.QMenuBar(MainWindow)
    self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 25))
    self.menuBar.setObjectName(_fromUtf8("menuBar"))
    MainWindow.setMenuBar(self.menuBar)
    self.mainToolBar = QtGui.QToolBar(MainWindow)
    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
    MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
    self.statusBar = QtGui.QStatusBar(MainWindow)
    self.statusBar.setObjectName(_fromUtf8("statusBar"))
    MainWindow.setStatusBar(self.statusBar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
    self.mMyButton.clicked.connect(self.showNewWindow)

  def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    self.mMyButton.setText(QtGui.QApplication.translate("MainWindow", "Addition", None, QtGui.QApplication.UnicodeUTF8))


  def showNewWindow(self):
    ui = Ui_NewWindow()
    ui.setupUi(QtGui.QDialog())
    ui.NewWindow.exec_()


try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s

class Ui_NewWindow(object):
  def setupUi(self, NewWindow):
    self.NewWindow=NewWindow
    self.NewWindow.setObjectName(_fromUtf8("NewWindow"))
    self.NewWindow.resize(439, 225)
    self.centralwidget = QtGui.QWidget(NewWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
    self.pushButton_2.setGeometry(QtCore.QRect(170, 140, 99, 27))
    self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
    self.widget = QtGui.QWidget(self.centralwidget)
    self.widget.setGeometry(QtCore.QRect(40, 30, 365, 89))
    self.widget.setObjectName(_fromUtf8("widget"))
    self.verticalLayout = QtGui.QVBoxLayout(self.widget)
    #self.verticalLayout.setMargin(0)
    self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
    self.horizontalLayout_2 = QtGui.QHBoxLayout()
    self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
    self.label_3 = QtGui.QLabel(self.widget)
    self.label_3.setObjectName(_fromUtf8("label_3"))
    self.horizontalLayout_2.addWidget(self.label_3)
    self.lineEdit = QtGui.QLineEdit(self.widget)
    self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    self.horizontalLayout_2.addWidget(self.lineEdit)
    self.label_4 = QtGui.QLabel(self.widget)
    self.label_4.setObjectName(_fromUtf8("label_4"))
    self.horizontalLayout_2.addWidget(self.label_4)
    self.lineEdit_2 = QtGui.QLineEdit(self.widget)
    self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
    self.horizontalLayout_2.addWidget(self.lineEdit_2)
    self.verticalLayout.addLayout(self.horizontalLayout_2)
    self.pushButton = QtGui.QPushButton(self.widget)
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.verticalLayout.addWidget(self.pushButton)
    self.horizontalLayout = QtGui.QHBoxLayout()
    self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
    self.label = QtGui.QLabel(self.widget)
    self.label.setObjectName(_fromUtf8("label"))
    self.horizontalLayout.addWidget(self.label)
    self.label_2 = QtGui.QLabel(self.widget)
    self.label_2.setObjectName(_fromUtf8("label_2"))
    self.horizontalLayout.addWidget(self.label_2)
    self.verticalLayout.addLayout(self.horizontalLayout)
    self.menubar = QtGui.QMenuBar(NewWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 25))
    self.menubar.setObjectName(_fromUtf8("menubar"))

    self.statusbar = QtGui.QStatusBar(NewWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))

    self.retranslateUi(NewWindow)
    QtCore.QMetaObject.connectSlotsByName(NewWindow)
    self.pushButton.clicked.connect(self.sum)


  def sum(self):
    summed = float(self.lineEdit.text()) + float(self.lineEdit_2.text())
    self.label_2.setText(str(summed))

  def retranslateUi(self, NewWindow):
    self.NewWindow.setWindowTitle(QtGui.QApplication.translate("NewWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton_2.setText(QtGui.QApplication.translate("NewWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
    self.label_3.setText(QtGui.QApplication.translate("NewWindow", "A = ", None, QtGui.QApplication.UnicodeUTF8))
    self.lineEdit.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
    self.label_4.setText(QtGui.QApplication.translate("NewWindow", "B = ", None, QtGui.QApplication.UnicodeUTF8))
    self.lineEdit_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
    self.pushButton.setText(QtGui.QApplication.translate("NewWindow", "Add", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("NewWindow", "Answer  : ", None, QtGui.QApplication.UnicodeUTF8))
    self.label_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
  import sys
  app = QtGui.QApplication(sys.argv)
  MainWindow = QtGui.QMainWindow()
  ui = Ui_MainWindow()
  ui.setupUi(MainWindow)
  MainWindow.show()
  sys.exit(app.exec_())