简体   繁体   中英

select item and update QLineedit from QlistWidget by Mouse Click?

Update QLineEdit by selected item from QlistWidget, First Box updated as I Wish. But second textbox and third textbox, not updated by the selected item. If we selected item for second textbox first textbox and second textbox update. and For the third one, no response? How to resolve

file: mouseclick.py

import sys
from mouseclick_source import *

textbox1_item = ["Red", "Green", "Blue", "Brown", "White", "Black", "Grey", 
                  "Dark Red", "Dark Green", "Dark Blue"]
textbox2_item = ["Gold", "Silver", "Bronze", "Copper", "Iron", "Aluminium", 
                "Taitanium"]
textbox3_item = ["Indian Ocean", "Arabian Sea", "Bay of Bengal", "Black Sea", 
                 "Pacific Ocean"]

class Mclick_sample_main(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Mouse click Samples")
        self.textbox1 = QLineEdit()
        self.textbox2 = QLineEdit()
        self.textbox3 = QLineEdit()
        self.listbox1 = QListWidget()
        self.listbox2 = QListWidget()

        self.textbox1.setObjectName("textbox1")
        self.textbox2.setObjectName("textbox2")
        self.textbox3.setObjectName("textbox3")
        self.listbox1.setObjectName("listbox1")
        self.listbox2.setObjectName("listbox2")

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.textbox1)
        vbox.addWidget(self.textbox2)
        vbox.addWidget(self.textbox3)
        vbox.addWidget(self.listbox1)

        QApplication.instance().focusChanged.connect(self.on_focusChanged)

    def on_focusChanged(self):
        fwidget = QApplication.focusWidget()
        if fwidget is not None:
            if fwidget.objectName() == "textbox1":
                self.listbox1.clear()
                self.listbox1.addItems(textbox1_item)

                self.getdetails_1 = Sourcefile(self.textbox1, self.listbox1, self.listbox2)
                #self.textbox1.textChanged.connect(self.getdetails_1.func_textbox_textchanged)
                self.listbox1.itemClicked.connect(self.getdetails_1.listbox_clicked)
                return True

            if fwidget.objectName() == "textbox2":
                self.listbox1.clear()
                self.listbox1.addItems(textbox2_item)

                self.getdetails_2 = Sourcefile(self.textbox2, self.listbox1, self.listbox2)
                #self.textbox1.textChanged.connect(self.getdetails_2.func_textbox_textchanged)
                self.listbox1.itemClicked.connect(self.getdetails_2.listbox_clicked)
                return True

            if fwidget.objectName() == "textbox3":
                self.listbox1.clear()
                self.listbox1.addItems(textbox3_item)

                self.getdetails_3 = Sourcefile(self.textbox3, self.listbox1, self.listbox2)
                #self.textbox1.textChanged.connect(self.getdetails_3.func_textbox_textchanged)
                self.listbox1.itemClicked.connect(self.getdetails_3.listbox_clicked)
                return True


def main():
    myapp = QApplication(sys.argv)
    mywin = Mclick_sample_main()
    mywin.show()
    sys.exit(myapp.exec_())


if __name__ == "__main__":
    main()

file: mouseclick_source.py

from PyQt5.QtWidgets import *

class Sourcefile(QWidget):
    def __init__(self, textbox, listbox1, parent=None):
        super().__init__(listbox2)
        self.tbox1 = textbox
        self.lbox1 = listbox1
            
    def listbox_clicked(self, item):
        self.tbox1.setText(item.text())
        self.tbox1.setFocus()
        return True

The issue might be that the line self.tbox1.setFocus() causes the focusChanged signal to be emitted again. The QListWidget is cleared and restocked so there is no current item text, and the text box is set to an empty string giving off the appearance of being "unresponsive". I got segmentation faults trying to run the code anyways so I had to change it.

All the signal and slot connections can be done within the main class. Selecting an item from the QListWidget can be detected with the currentTextChanged signal. On focusChanged, keep a pointer to the newest focused-in QLineEdit. Now when an item is selected from the list widget you can set the text to the correct text box and reapply focus to it.

import sys
from PyQt5.QtWidgets import *

textbox_items = {
    "textbox1": ["Red", "Green", "Blue", "Brown", "White", "Black", "Grey", 
                  "Dark Red", "Dark Green", "Dark Blue"],
    "textbox2": ["Gold", "Silver", "Bronze", "Copper", "Iron", "Aluminium", 
                "Taitanium"],
    "textbox3": ["Indian Ocean", "Arabian Sea", "Bay of Bengal", "Black Sea", 
                 "Pacific Ocean"]
    }

class Mclick_sample_main(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Mouse click Samples")
        self.textbox1 = QLineEdit()
        self.textbox2 = QLineEdit()
        self.textbox3 = QLineEdit()
        self.listbox1 = QListWidget()
        self.listbox1.currentTextChanged[str].connect(self.update_textbox)

        self.textbox1.setObjectName("textbox1")
        self.textbox2.setObjectName("textbox2")
        self.textbox3.setObjectName("textbox3")
        self.listbox1.setObjectName("listbox1")

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.textbox1)
        vbox.addWidget(self.textbox2)
        vbox.addWidget(self.textbox3)
        vbox.addWidget(self.listbox1)

        self.current_textbox = None
        QApplication.instance().focusChanged.connect(self.on_focusChanged)

    def on_focusChanged(self, old, new):
        if isinstance(new, QLineEdit) and new != self.current_textbox:
            self.current_textbox = new
            self.listbox1.clear()
            self.listbox1.addItems(textbox_items[new.objectName()])
                
    def update_textbox(self, text):
        if text:
            self.current_textbox.setText(text)
            self.current_textbox.setFocus()


def main():
    myapp = QApplication(sys.argv)
    mywin = Mclick_sample_main()
    mywin.show()
    sys.exit(myapp.exec_())


if __name__ == "__main__":
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM