2023年6月17日 星期六

【Python交易程式開發現場9】使用Qt Designer設計GUI (3) | 順利使用Qt獨特的Model-View架構

終於順利使用Qt獨特的Model-View架構

.
受不了群益API的python範例檔的設計架構,
用Qt的Model-Views架構重新改寫..
把核心和介面切割開來,
介面用UI Designer設計完就沒事了,
不用和程式邏輯混在一起..
.
新版的GUI已經慢慢和群益API接上了,
好像幫家裡接電一樣,
終於電通了..
.
其實寫程式和打電動的感覺很像,
關關難過, 關關過..


Model:


def twLogin(twID, twPW):
    try:
        m_nCode = skC.SKCenterLib_SetLogPath(os.path.split(os.path.realpath(__file__))[0] + "\\CapitalLog_Quote")
        m_nCode = skC.SKCenterLib_Login(twID, twPW)
        if(m_nCode==0):
            #Global_ID["text"] =  twID   # textID: ID帳號
            SetID(twID)  #ctw focus
            print("登入成功")
            #WriteMessage("登入成功",GlobalListInformation)
        else:
            print("登入失敗")
            #WriteMessage(m_nCode,GlobalListInformation)
    except Exception as e:
        messagebox.showerror("error!",e)

def twConnect():
    try:
        m_nCode = skQ.SKQuoteLib_EnterMonitorLONG()
        #SendReturnMessage("Quote", m_nCode, "SKQuoteLib_EnterMonitorLONG",GlobalListInformation)
        return m_nCode
    except Exception as e:
        messagebox.showerror("error!",e)    

def twDisconnect():
    try:
        m_nCode = skQ.SKQuoteLib_LeaveMonitor()
        return m_nCode
        """
        if (m_nCode != 0):
            strMsg = "SKQuoteLib_LeaveMonitor failed!", skC.SKCenterLib_GetReturnCodeMessage(m_nCode)
            WriteMessage(strMsg,GlobalListInformation)
        else:
            SendReturnMessage("Quote", m_nCode, "SKQuoteLib_LeaveMonitor",GlobalListInformation)
        """    
    except Exception as e:
        messagebox.showerror("error!",e)    


ViewController:

class MyMainWindow(QMainWindow, Ui_Form):
    def __init__(self, parent=None):    
        super(MyMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.twTest1) #ctw signal
        self.pushButton_2.clicked.connect(self.twTest2) #ctw signal
        self.pushButton_3.clicked.connect(self.twTest3) #ctw signal

    def twTest1(self):  #ctw slot function
        print("1-Login")    
        self.listWidget.addItem("1-Login")
        twLogin(ID, PW)

    def twTest2(self):  #ctw slot function
        print("2-Connect")    
        self.listWidget.addItem("2-Connect")
        self.listWidget.addItem("m_nCode: " + str(twConnect()))

    def twTest3(self):  #ctw slot function
        print("3-DisConnect")    
        self.listWidget.addItem("3-DisConnect")
        self.listWidget.addItem("m_nCode: " + str(twDisconnect()))




沒有留言:

張貼留言

prog-0318

test test1 Written with StackEdit .