-1

I`m trying to develop nice-looking app in pyqt using WebView and have a little problem. I want to create new Window by pressing button in WebView.

I have main window(short view):

class LoginWindow(QWidget):
    def __init__(self):
        # creating window and different properties...
        # Its not important,i think.

        self.foo = LoginConnector(self)
        view.page().mainFrame().addToJavaScriptWindowObject("foo", self.foo)

... connector to JS

class LoginConnector(QObject):

@pyqtSlot(result=str)
def get_user_list(self):
    return ','.join(core.available_users())

@pyqtSlot()
def new_window(self):
    print 'Hello!'  
    #What can i do here?

@pyqtSlot()
def quit(self):
    QApplication.quit()

When i`m pressing button in window,its call new_window() from LoginConnector,but how create Dialog inside LoginConnector?

Full code

SOLVED! I`m idiot,listen that clever guy below

progerz
  • 27
  • 1
  • 9

1 Answers1

1

Just look at the api docs. http://pyqt.sourceforge.net/Docs/PyQt4/classes.html If you don't like this PySide is basically the same thing with a few differences in syntax. http://pyside.github.io/docs/pyside/

class LoginWindow(QWebView):
    def __init__(self, *args, **kwargs):
        super(QWebView, self).__init__(*args, **kwargs):

        ...
        self.foo = LoginConnector(self)

        self.loadPage("httpdocs/login.hmtl")
    # end Constructor

    def loadPage(self, web_page):
        """Loads a web page in the browser.

        Args:
            webPage (str): The URL of the desired web page with JavaScript Python Communication.
        """
        # Inspector - right click inspect to debug javascript and have access to the console.
        inspector = QtWebKit.QWebInspector()
        inspector.setPage(self.page())

        # Force relative path otherwise you get a does not except file:// error
        if os.path.isabs(web_page):
            web_page = os.path.relpath(web_page)

        url = QtCore.QUrl(web_page)
        self.load(url)
        self.frame = self.page().mainFrame()
        self.frame.addToJavaScriptWindowObject("foo", self.foo)
    # end loadPage
# end class LoginWindow


class LoginConnector(QObject):
    @pyqtSlot()
    def new_window(self):
        self.dialog = QDialog()
        self.dialog.show() # or exec
    # end new_window
# end class LoginConnector

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    window = LoginWindow()
    window.show()

    sys.exit(app.exec_())

This is the main application structure for PySide. PyQt should be similar.

justengel
  • 6,132
  • 4
  • 26
  • 42
  • I tried to do something like that.Windows appear but not initializating. Code here http://pastebin.com/rk9HaN0C .I don`t see "Init!" in console,and window appears without title and WebView. – progerz Jul 15 '14 at 12:16
  • It looks like you didn't set the layout to your dialog. self.setLayout(layout). self.setWindowTitle("Name") sets the title for PySide (I don't use pyqt). The code didn't have the new_window method wrapped around a QObject class, but it looks like you were doing that just didn't post all of your code. – justengel Jul 15 '14 at 12:24
  • Yep,i know,full code is here http://pastebin.com/283R2A39 . I found out that problem somewhere in CreateUser class and i`m trying to find bug now. – progerz Jul 15 '14 at 12:29
  • You might just want your LoginWindow to inherit a QWebView. I updated the code with some stuff you might find useful. – justengel Jul 15 '14 at 12:36
  • Oh yeah the automatic garbage collection. I don't think it should do that if you use exec_ method instead of show because the code waits for a return from that function. I may be wrong. – justengel Jul 15 '14 at 12:39
  • I`m idiot! Sorry that i waste your time! Problem was at the 81 line,i have to use "new_usr_window" instead of "login_window"... Pff,probalby i have to use more clear code) – progerz Jul 15 '14 at 12:41