Python Forum
How a QMainWindow can open a dialog?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How a QMainWindow can open a dialog?
#1
I have the following code which has two classes:
-Ui_dialog_in
-myWindow

What I 'm trying to achieve is:
-when myWindow opens, to open a Ui_dialog_in

What must I change - add?

from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QMainWindow, QHBoxLayout, QWidget
from PySide6.QtWidgets import QApplication

class Ui_dialog_in(object):
    def setupUi(self, dialog_in):
        dialog_in.setObjectName("dialog_in")
        dialog_in.resize(400, 146)
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(dialog_in)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.titleLabel = QtWidgets.QLabel(dialog_in)
        self.titleLabel.setObjectName("titleLabel")
        self.horizontalLayout_2.addWidget(self.titleLabel)
        self.titleEdit = QtWidgets.QLineEdit(dialog_in)
        self.titleEdit.setObjectName("titleEdit")
        self.horizontalLayout_2.addWidget(self.titleEdit)
        self.horizontalLayout_2.setStretch(0, 1)
        self.horizontalLayout_2.setStretch(1, 4)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.authorLabel = QtWidgets.QLabel(dialog_in)
        self.authorLabel.setObjectName("authorLabel")
        self.horizontalLayout_3.addWidget(self.authorLabel)
        self.authorNameCombo = QtWidgets.QComboBox(dialog_in)
        self.authorNameCombo.setObjectName("authorNameCombo")
        self.horizontalLayout_3.addWidget(self.authorNameCombo)
        self.horizontalLayout_3.setStretch(0, 1)
        self.horizontalLayout_3.setStretch(1, 4)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.okButton = QtWidgets.QPushButton(dialog_in)
        self.okButton.setObjectName("okButton")
        self.horizontalLayout.addWidget(self.okButton)
        self.pushButton = QtWidgets.QPushButton(dialog_in)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        self.verticalLayout_3.addLayout(self.verticalLayout_2)

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

    def retranslateUi(self, dialog_in):
        _translate = QtCore.QCoreApplication.translate
        dialog_in.setWindowTitle(_translate("dialog_in", "Dialog"))
        self.titleLabel.setText(_translate("dialog_in", "Title"))
        self.authorLabel.setText(_translate("dialog_in", "Author"))
        self.okButton.setText(_translate("dialog_in", "PushButton"))
        self.pushButton.setText(_translate("dialog_in", "PushButton"))



class myWindow(QMainWindow):
    """A window to show the books available"""
    def __init__(self, parent=None):
        """Initializer."""
        super().__init__(parent)
        self.setWindowTitle("My title")
        self.resize(550, 250)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.layout = QHBoxLayout()
        self.centralWidget.setLayout(self.layout)
        #self.setupUI()
        # open second window, myDialog
        myDialog = Ui_dialog_in()


if __name__ == "__main__":
    app = QApplication([])
    import sys
    window = myWindow()
    window.resize(800, 600)
    window.show()
    sys.exit(app.exec())
Reply
#2
You have mixed PyQt6 and PySide6

A simple Dialog Example

import sys

from PyQt6.QtWidgets import QApplication, QDialog, QMainWindow, QPushButton


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("App")

        button = QPushButton("Click me")
        button.clicked.connect(self.button_clicked)
        self.setCentralWidget(button)

    def button_clicked(self, s):
        print("click", s)

        dlg = QDialog(self)
        dlg.setWindowTitle("Hallo!")
        dlg.exec()


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()
panoss likes this post
Reply
#3
It was so simple, this is embarashing Big Grin .

What if I want the second form to derive from 'object'? 'class Ui_dialog_in(object):'
(because this is what is produced by pyuic6)
Reply
#4
Use a window, and if you want to learn something don't use the designer.

from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import (QMainWindow, QHBoxLayout, 
                             QWidget, QApplication, QPushButton)
 
class Ui_dialog_in(QMainWindow):
    def __init__(self, parent = None):
        super(Ui_dialog_in, self).__init__(parent)
        
    def setupUi(self, parent=None):
        self.setObjectName("self")
        self.resize(400, 146)
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.titleLabel = QtWidgets.QLabel(self)
        self.titleLabel.setObjectName("titleLabel")
        self.horizontalLayout_2.addWidget(self.titleLabel)
        self.titleEdit = QtWidgets.QLineEdit(self)
        self.titleEdit.setObjectName("titleEdit")
        self.horizontalLayout_2.addWidget(self.titleEdit)
        self.horizontalLayout_2.setStretch(0, 1)
        self.horizontalLayout_2.setStretch(1, 4)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.authorLabel = QtWidgets.QLabel(self)
        self.authorLabel.setObjectName("authorLabel")
        self.horizontalLayout_3.addWidget(self.authorLabel)
        self.authorNameCombo = QtWidgets.QComboBox(self)
        self.authorNameCombo.setObjectName("authorNameCombo")
        self.horizontalLayout_3.addWidget(self.authorNameCombo)
        self.horizontalLayout_3.setStretch(0, 1)
        self.horizontalLayout_3.setStretch(1, 4)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.okButton = QtWidgets.QPushButton(self)
        self.okButton.setObjectName("okButton")
        self.horizontalLayout.addWidget(self.okButton)
        self.pushButton = QtWidgets.QPushButton(self)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        self.verticalLayout_3.addLayout(self.verticalLayout_2)
 
        self.retranslateUi(self)
        QtCore.QMetaObject.connectSlotsByName(self)
        central_widget = QWidget()
        central_widget.setLayout(self.verticalLayout_3)
        self.setCentralWidget(central_widget)
        self.show()

 
    def retranslateUi(self, dialog_in):
        _translate = QtCore.QCoreApplication.translate
        dialog_in.setWindowTitle(_translate("dialog_in", "Dialog"))
        self.titleLabel.setText(_translate("dialog_in", "Title"))
        self.authorLabel.setText(_translate("dialog_in", "Author"))
        self.okButton.setText(_translate("dialog_in", "PushButton"))
        self.pushButton.setText(_translate("dialog_in", "PushButton"))
 
 
 
class myWindow(QMainWindow):
    def __init__(self, parent = None):
        super(myWindow, self).__init__(parent)
        self.setWindowTitle("App")
        self.resize(550, 250)
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        self.layout = QHBoxLayout()
        button = QPushButton("Open WIndow")
        button.clicked.connect(self.button_clicked)
        self.layout.addWidget(button)
        self.central_widget.setLayout(self.layout)
 
    def button_clicked(self):
        self.dlg = Ui_dialog_in()
        self.dlg.setupUi()
 
 
if __name__ == "__main__":
    app = QApplication([])
    import sys
    window = myWindow()
    window.resize(200, 100)
    window.show()
    sys.exit(app.exec())
panoss likes this post
Reply
#5
I found it, thanks!

from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6.QtWidgets import QMainWindow, QPushButton, QDialog, QApplication, QVBoxLayout
from PyQt6.QtWidgets import QMainWindow, QHBoxLayout, QWidget
from PyQt6.QtWidgets import QApplication




class myWindow(QMainWindow):
    """A window to show the books available"""
    def __init__(self, parent=None):
        """Initializer."""
        super().__init__(parent)
        self.setWindowTitle("My title")
        self.resize(550, 250)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.layout = QHBoxLayout()
        self.centralWidget.setLayout(self.layout)
        #self.setupUI()
        # open second window, myDialog
        button = QPushButton("Click me")
        button.clicked.connect(self.button_clicked)
        self.setCentralWidget(button)
    def button_clicked(self):
        myDialog = Dialog()
        myDialog.exec()


class Ui_dialog_in(object):
    def setupUi(self, dialog_in):
        dialog_in.setObjectName("dialog_in")
        dialog_in.resize(400, 146)
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(dialog_in)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.titleLabel = QtWidgets.QLabel(dialog_in)
        self.titleLabel.setObjectName("titleLabel")
        self.horizontalLayout_2.addWidget(self.titleLabel)
        self.titleEdit = QtWidgets.QLineEdit(dialog_in)
        self.titleEdit.setObjectName("titleEdit")
        self.horizontalLayout_2.addWidget(self.titleEdit)
        self.horizontalLayout_2.setStretch(0, 1)
        self.horizontalLayout_2.setStretch(1, 4)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.authorLabel = QtWidgets.QLabel(dialog_in)
        self.authorLabel.setObjectName("authorLabel")
        self.horizontalLayout_3.addWidget(self.authorLabel)
        self.authorNameCombo = QtWidgets.QComboBox(dialog_in)
        self.authorNameCombo.setObjectName("authorNameCombo")
        self.horizontalLayout_3.addWidget(self.authorNameCombo)
        self.horizontalLayout_3.setStretch(0, 1)
        self.horizontalLayout_3.setStretch(1, 4)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum,
                                           QtWidgets.QSizePolicy.Policy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding,
                                            QtWidgets.QSizePolicy.Policy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.okButton = QtWidgets.QPushButton(dialog_in)
        self.okButton.setObjectName("okButton")
        self.horizontalLayout.addWidget(self.okButton)
        self.pushButton = QtWidgets.QPushButton(dialog_in)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        self.verticalLayout_3.addLayout(self.verticalLayout_2)

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

    def retranslateUi(self, dialog_in):
        _translate = QtCore.QCoreApplication.translate
        dialog_in.setWindowTitle(_translate("dialog_in", "Dialog"))
        self.titleLabel.setText(_translate("dialog_in", "Title"))
        self.authorLabel.setText(_translate("dialog_in", "Author"))
        self.okButton.setText(_translate("dialog_in", "PushButton"))
        self.pushButton.setText(_translate("dialog_in", "PushButton"))

class Dialog(QDialog, Ui_dialog_in):
    def __init__(self,parent=None):
        QDialog.__init__(self,parent)
        self.setupUi(self)

if __name__ == "__main__":
    app = QApplication([])
    import sys
    window = myWindow()
    window.resize(800, 600)
    window.show()
    sys.exit(app.exec())
Axel_Erfurt likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 2,237 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  [Tkinter] cancelling open dialog gives empty string rwahdan 2 3,384 Jul-17-2021, 09:17 PM
Last Post: steve_shambles
  [WxPython] Return code when closing the dialog ioprst 1 3,186 Aug-13-2019, 11:47 AM
Last Post: jefsummers
  PyQT5 : Unable to Create Another Dialog While One is Open iMuny 3 3,887 Jul-17-2019, 11:40 AM
Last Post: iMuny
  [WxPython] Any dialog that allow user to select file OR folder? buran 3 4,235 Apr-03-2019, 06:33 PM
Last Post: Yoriz
  [WxPython] how to run the dialog from another py file royer14 0 2,697 Jul-02-2018, 05:33 AM
Last Post: royer14
  FileBrowser dialog reverendfuzzy 5 4,647 May-06-2018, 01:56 PM
Last Post: reverendfuzzy
  I'm trying to create a simple yes/no dialog box RedSkeleton007 15 18,563 Apr-25-2018, 05:10 PM
Last Post: nilamo
  About Dialog in PyQt5 on macOS cpuin 0 2,879 Jan-23-2018, 08:50 PM
Last Post: cpuin
  set font from dialog box pyside python Gigux 1 3,601 May-08-2017, 12:25 AM
Last Post: Joseph_f2

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020