Displaying popup from action pane on compiled PyQt4 Qt Designer

So, I tried to create an application and am struggling to hook up the action to another dialog. I searched for it but found a window solution that was not built with Qt Designer. I developed it using Qt Designer and compiled the .ui format to .py.

This is my first window:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'home.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from howto import Ui_HowToDialog

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_HomeWindow(object):
    def setupUi(self, HomeWindow):
        HomeWindow.setObjectName(_fromUtf8("HomeWindow"))
        HomeWindow.resize(1106, 259)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Consolas"))
        HomeWindow.setFont(font)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/plottwist.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        HomeWindow.setWindowIcon(icon)
        HomeWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.centralwidget = QtGui.QWidget(HomeWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.homeBrowser = QtGui.QTextBrowser(self.centralwidget)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Consolas"))
        self.homeBrowser.setFont(font)
        self.homeBrowser.setObjectName(_fromUtf8("homeBrowser"))
        self.horizontalLayout.addWidget(self.homeBrowser)
        HomeWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(HomeWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        HomeWindow.setStatusBar(self.statusbar)
        self.homeToolbar = QtGui.QToolBar(HomeWindow)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Consolas"))
        font.setPointSize(10)
        self.homeToolbar.setFont(font)
        self.homeToolbar.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.homeToolbar.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.homeToolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
        self.homeToolbar.setObjectName(_fromUtf8("homeToolbar"))
        HomeWindow.addToolBar(QtCore.Qt.LeftToolBarArea, self.homeToolbar)
        self.actionOpenDataset = QtGui.QAction(HomeWindow)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/dataset.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.actionOpenDataset.setIcon(icon1)
        self.actionOpenDataset.setObjectName(_fromUtf8("actionOpenDataset"))
        self.actionHowTo = QtGui.QAction(HomeWindow)
        self.actionHowTo.triggered.connect(self.showHowto)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/howto.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.actionHowTo.setIcon(icon2)
        self.actionHowTo.setObjectName(_fromUtf8("actionHowTo"))
        self.actionAbout = QtGui.QAction(HomeWindow)
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/about.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.actionAbout.setIcon(icon3)
        self.actionAbout.setObjectName(_fromUtf8("actionAbout"))
        self.homeToolbar.addAction(self.actionOpenDataset)
        self.homeToolbar.addAction(self.actionHowTo)
        self.homeToolbar.addAction(self.actionAbout)

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

    def retranslateUi(self, HomeWindow):
        HomeWindow.setWindowTitle(_translate("HomeWindow", "Plot Twist", None))
        self.homeBrowser.setHtml(_translate("HomeWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Consolas\'; font-size:7.5pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:14pt; font-weight:600;\">Data Visualization is as important as it sounds, you can visualize your data with this app just by inserting any dataset you want. For more detailed instructions, simply go to How To icon.</span></p>\n"
"<p align=\"justify\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:14pt; font-weight:600;\"><br /></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600;\">With Plot Twist, you can:</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600;\">- Look inside your dataset</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600;\">- Visualize it by choosing any possible diagrams of any columns</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt; font-weight:600;\">- Find out specific calculation of any column</span></p></body></html>", None))
        self.homeToolbar.setWindowTitle(_translate("HomeWindow", "toolBar", None))
        self.actionOpenDataset.setText(_translate("HomeWindow", "Open Dataset", None))
        self.actionOpenDataset.setStatusTip(_translate("HomeWindow", "Open dataset to visualize it.", None))
        self.actionOpenDataset.setShortcut(_translate("HomeWindow", "Ctrl+O", None))
        self.actionHowTo.setText(_translate("HomeWindow", "How To", None))
        self.actionHowTo.setToolTip(_translate("HomeWindow", "How To Do This?", None))
        self.actionHowTo.setStatusTip(_translate("HomeWindow", "Instructions on how this app works.", None))
        self.actionHowTo.setShortcut(_translate("HomeWindow", "F1", None))
        self.actionAbout.setText(_translate("HomeWindow", "About", None))
        self.actionAbout.setToolTip(_translate("HomeWindow", "About", None))
        self.actionAbout.setStatusTip(_translate("HomeWindow", "Description and message by creator.", None))
        self.actionAbout.setShortcut(_translate("HomeWindow", "F2", None))

    def showHowto(self):
        HowToDialog = QtGui.QDialog()
        hui = Ui_HowToDialog()
        hui.setupUi(HowToDialog)
        HowToDialog.show()
        sys.exit(app.exec_())

import resources_rc

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

      

And this is the dialogue I was trying to pop up. It should show the HowTo dialog when the toolbar icon is launched.

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'howto.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_HowToDialog(object):
    def setupUi(self, HowToDialog):
        HowToDialog.setObjectName(_fromUtf8("HowToDialog"))
        HowToDialog.resize(585, 242)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Consolas"))
        HowToDialog.setFont(font)
        HowToDialog.setAcceptDrops(False)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/resources/howto.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        HowToDialog.setWindowIcon(icon)
        self.horizontalLayout = QtGui.QHBoxLayout(HowToDialog)
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.howtoBrowser = QtGui.QTextBrowser(HowToDialog)
        self.howtoBrowser.setObjectName(_fromUtf8("howtoBrowser"))
        self.horizontalLayout.addWidget(self.howtoBrowser)

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

    def retranslateUi(self, HowToDialog):
        HowToDialog.setWindowTitle(_translate("HowToDialog", "How To Do This?", None))
        self.howtoBrowser.setHtml(_translate("HowToDialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Consolas\'; font-size:7.5pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">How to insert your dataset:</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">1. Click </span><span style=\" font-size:12pt; font-weight:600;\">Open Dataset </span><span style=\" font-size:12pt;\">icon.</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">2. Click </span><span style=\" font-size:12pt; font-weight:600;\">Browse </span><span style=\" font-size:12pt;\">and select your dataset.</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">3. Note that the dataset has to be a .csv file.</span></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">4. Click </span><span style=\" font-size:12pt; font-weight:600;\">Insert File </span><span style=\" font-size:12pt;\">button.</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">5. Your dataset visualization window will appear, and you can start to visualize it.</span></p>\n"
"<p align=\"justify\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:12pt;\"><br /></p>\n"
"<p align=\"justify\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:12pt;\">Happy visualizing :)</span></p></body></html>", None))


import resources_rc

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    HowToDialog = QtGui.QDialog()
    ui = Ui_HowToDialog()
    ui.setupUi(HowToDialog)
    HowToDialog.show()
    sys.exit(app.exec_())

      

As you can see, this is the function I define to show the second window:

def showHowto(self):
    HowToDialog = QtGui.QDialog()
    hui = Ui_HowToDialog()
    hui.setupUi(HowToDialog)
    HowToDialog.show()
    sys.exit(app.exec_())

      

And I linked it to the action bar with self.actionHowTo.triggered.connect(self.showHowto)

.

When I run it it will display QCoreApplication::exec: The event loop is already running

. Can someone tell me what is wrong and the solution? Thanks before!

+3


source to share


1 answer


I had a similar problem starting with pyqt. You have to create another class for the dialog:

#!python3
'''docs'''
import sys
from PyQt4.QtGui import *
from _apps.PyUi import DialogUi


class DialogApp(QDialog, DialogUi.Ui_DialogApp):
    """Dialog class"""

    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)


def main(self):
    app = QApplication(sys.argv)
    form = DialogApp()
    form.show()
    app.exec_()


if __name__ == '__main__':
    main()

      

I included the main one for easy testing ... The next part is in the main window of only the slot associated with the action:

def openDialog(self):

    self.dialog= DialogApp(self)
    self.dialog.show()

      



The part you have to be careful about is who is the parent of the dialog, if it None

is the parent, as in my example, the dialog will get a separate tab on the taskbar and can be instantiated as many times as you press the button action

, assuming the dialog itself the window is meaningless. If you pass a dialog mainwindow

( self

) in a dialog, which is recommended in most cases in dialogs, the parent will be the parent, by default the dialog will focus on the main when it appears.

Edit:

Also you need and can only have one QApplication

active, all the specified windows and main methods and functions go through this initial QApplication

one as far as I know.

0


source







All Articles