如何在应用启动时将焦点设置在小部件上?

时间:2014-04-12 12:25:56

标签: python python-2.7 pyside setfocus qlineedit

我正在尝试在应用启动时将焦点设置在QLineEdit窗口小部件上,但由于某些原因它失败了。调用包含QLineEdit_object.setFocus()并绑定按钮的方法,可以完美地运行。但是在启动时,在创建小部件后设置为初始化时,似乎根本不执行。

在Python中使用PySide。

# coding=utf-8
import sys
import PySide.QtGui as QG
import PySide.QtCore as QC

class GG(QG.QMainWindow):
    def __init__(self):
        super(GG, self).__init__()
        self.move(0,0)
        self.resize(400,300)
        self.setWindowTitle('Demo')

        self.tabw = QG.QTabWidget()
        self.tab1 = Tab1()
        self.tab2 = Tab2()

        self.tabw.addTab(self.tab1, 'Tab1')
        self.tabw.addTab(self.tab2, 'Tab2')

        hbox = QG.QHBoxLayout()
        hbox.addWidget(self.tabw)

        self.setCentralWidget(self.tabw)
        self.setLayout(hbox)
        self.show()

class Tab1(QG.QWidget):
    def __init__(self):
        super(Tab1, self).__init__()

        self.btns()
        self.inputt()
        self.layoutz()
        self.inp.setFocus() # doesn't set the focus on startup ?
        self.show()

    def inputt(self):
        self.inp = QG.QLineEdit('', self)

    def btns(self):
        self.btn1 = QG.QPushButton('Button1', self)
        self.btn1.clicked.connect(self.focusit) # works just fine

    def layoutz(self):
        vbox = QG.QVBoxLayout()   
        vbox.addWidget(self.btn1)
        vbox.addStretch(1)
        vbox.addWidget(self.inp)
        self.setLayout(vbox)

    def focusit(self):
        self.inp.setFocus() # works just fine


class Tab2(Tab1):
    def __init__(self):
        super(Tab2, self).__init__()


def main():
    app = QG.QApplication(sys.argv)
    a = GG()
    sys.exit(app.exec_())

if __name__ == '__main__':
   main()

1 个答案:

答案 0 :(得分:1)

好吧,经过一些游戏,我想出了这个解决方案:

import sys
import PySide.QtGui as QG
import PySide.QtCore as QC

class GG(QG.QMainWindow):
    def __init__(self):
        super(GG, self).__init__()
        self.move(0,0)
        self.resize(400,300)
        self.setWindowTitle('Demo')

        self.tabw = QG.QTabWidget()
        self.tab1 = Tab1()
        self.tab2 = Tab2()

        self.tabw.addTab(self.tab1, 'Tab1')
        self.tabw.addTab(self.tab2, 'Tab2')

        hbox = QG.QHBoxLayout()
        hbox.addWidget(self.tabw)

        self.setCentralWidget(self.tabw)
        self.setLayout(hbox)
        self.tab2.inp.setFocus() # setting focus right here
        self.tab1.inp.setFocus() # and here; notice the order
        self.show()

class Tab1(QG.QWidget):
    def __init__(self):
        super(Tab1, self).__init__()

        self.btns()
        self.inputt()
        self.layoutz()
        self.show()

    def inputt(self):
        self.inp = QG.QLineEdit('', self)

    def btns(self):
        self.btn1 = QG.QPushButton('Button1', self)
        self.btn1.clicked.connect(self.focusit)

    def layoutz(self):
        vbox = QG.QVBoxLayout()   
        vbox.addWidget(self.btn1)
        vbox.addStretch(1)
        vbox.addWidget(self.inp)
        self.setLayout(vbox)

    def focusit(self):
        self.inp.setFocus()


class Tab2(Tab1):
    def __init__(self):
        super(Tab2, self).__init__()


def main():
    app = QG.QApplication(sys.argv)
    a = GG()
    sys.exit(app.exec_())

if __name__ == '__main__':
   main()