简体   繁体   中英

How to set QInputDialog as modal

I'm trying to use dialogues from QtGui to get some input from user. For QFileDialog it works as I expected, but when I use QInputDialog, the dialogue just pops up and continue with the code execution, without waiting for the user input. Here is a short example:

from PyQt4 import QtGui
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from numpy import pi

class Canvas(FigureCanvas):
    def __init__(self):        
        self.fig = Figure()
        FigureCanvas.__init__(self, self.fig)

        self.fig.canvas.mpl_connect('key_press_event',self.key_pressed)
        self.fig.canvas.mpl_connect('button_press_event',self.on_left_click)

        self.ax = self.fig.add_axes([0,0,1,1])
        self.figure.canvas.show()

    def key_pressed(self, event):
        if event.key == 'f': 
            fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 
                                            'c:\\',"Image files (*.png *.jpg *.gif)")
            print fname

    def on_left_click(self,event):
        # If the mouse pointer is not on the canvas, ignore buttons
        if not event.inaxes: return

        if event.button==1:
            x=event.xdata
            y=event.ydata
            r, ok = QtGui.QInputDialog.getDouble(self, 'Text Input Dialog', 'Enter radius:', 10)
            if ok:
                self.ax.scatter(x, y, s=pi*r**2,c=0.5)
                self.draw()

cnv = Canvas()

I tried to replicate this with PySide instead of PyQt4. Your code as is only opens the canvas widget and then immediately closes it and exits. Which means that the application event loop is not running properly.

Try to change your last rows:

app = QtGui.QApplication([])
cnv = Canvas()
app.exec_()

With this everything works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM