2014-11-16 208 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