2014-11-16 212 views
0

我正在创建一个最初显示登录和注册按钮的应用程序。 点击登录我想显示另一个屏幕(或对话框),这将允许用户输入用户名和密码。单击按钮创建新对话框

,我想隐藏第一个对话框,当第二个对话框已经出现了,但没能做到这一点。(就像我们在Facebook的信使)

我们能否通过连接到点击的信号打开一个新的对话框登录按钮在Qt dsigner本身?

我已经设计了第一个屏幕在Qt Designer中,转换是的.ui文件的.py然后导入它在main.py

main.py

import sys 
from Pyside.QtGui import * 
from Pyside.QtCore import * 
from firstscreen import Ui_Dialog 


class MainDialog(QDialog, Ui_Dialog): 
    def __init__(self, parent=None): 
     super(MainDialog, self).__init__(parent) 
     self.setupUi(self) 
     self.Login.clicked.connect(self.showsecondscreen) 

    def showsecondscreen(self): 
     newScreen = QDialog(self) 
     newScreen.show(self) 


app = QApplication(sys.argv) 
form = MainDialog() 
form.show() 
app.exec_() 

firstscreen.ui

<?xml version="1.0" encoding="UTF-8"?> 
    <ui version="4.0"> 
    <class>Dialog</class> 
    <widget class="QDialog" name="Dialog"> 
     <property name="geometry"> 
     <rect> 
     <x>0</x> 
     <y>0</y> 
     <width>322</width> 
     <height>300</height> 
     </rect> 
     </property> 
     <property name="windowTitle"> 
     <string>My App</string> 
     </property> 
     <widget class="QPushButton" name="Login"> 
     <property name="geometry"> 
     <rect> 
     <x>110</x> 
     <y>110</y> 
     <width>98</width> 
     <height>27</height> 
     </rect> 
     </property> 
     <property name="text"> 
     <string>Login</string> 
     </property> 
     </widget> 
     <widget class="QPushButton" name="Signup"> 
     <property name="geometry"> 
     <rect> 
     <x>110</x> 
     <y>150</y> 
     <width>98</width> 
     <height>27</height> 
     </rect> 
     </property> 
     <property name="text"> 
     <string>Signup</string> 
     </property> 
     </widget> 
    </widget> 
    <resources/> 
    <connections/> 
    </ui> 

回答

0

你试过

def showsecondscreen(self): 
    self.hide(self) 
    newScreen = QDialog(self) 
    newScreen.show(self) 

无论如何,设计并不是很好,因为您尝试在对话框中使用Qt-Application-init-pattern。这将使MainDialog类在主事件循环内运行 - 因此在关闭后,应用程序将关闭。所以只隐藏作品。

通常你会用exec()执行一个QDialog并使用返回的结果进行进一步处理。

如果您打算在登录后显示主窗口,最好将您的“MainDialog”类重命名为“LoginDialog”,然后创建实际应用程序的主窗口(也可以称为“MainWindow”类)。然后更改启动这样的:

app = QApplication(sys.argv) 
login = LoginDialog() 
if(login.exec() == LOGGED_IN_SUCCESSFULLY_RETURN_VALUE) 
    form = MainWindow() 
    form.show() 
    app.exec_() 

更新:如果这将成为对话的一个序列,就像一个向导(或“助理”为号召MacOSX的),那么也许你会想从QWizard派生。

+0

是self.hide()正在工作 – Patrick

+0

那么请标记为答案。 – St0fF