0
def clickable(widget): #click event
    class Filter(QObject):    
        clicked = pyqtSignal()  #pyside2 사용자는 pyqtSignal() -> Signal()로 변경
        def eventFilter(self, obj, event):
            if obj == widget:
                if event.type() == QEvent.MouseButtonRelease:
                    if obj.rect().contains(event.pos()):
                        self.clicked.emit()
                        # The developer can opt for .emit(obj) to get the object within the slot.
                        return True
            return False
    filter = Filter(widget)
    widget.installEventFilter(filter)
    return filter.clicked

This is my click function code

enter image description here

This is my pyqt driving screen.

The name of each label matches the name of the tab. It consists of label_Korea_1 to label_Korea_10. The goal I want is to use the clicked() function to move to the corresponding link from the list of links that I have created before when I click on the label.

enter image description here

There is a list of Google News crawling results with the names of each tab, and the list is a double list, the first is the title of the article on the news, and the second is the link to the news. For example, it consists of a picture above the korea list.

category = ['korea','world','buss','sci','enter','sport','health']

for cate in category:
    ca = globals()[cate] #cate is recognized as a korea list.
    for n in range(10):          
        a = getattr(self, "label_{}_{}".format(cate,n+1))  # for example, a = label_korea_1
        link = ca[n][1]
        clickable(a).connect(lambda: webbrowser.open(link)) #If you click the label, link it with the link.         

This is the code I tried. All lists have 10 values. This code allows the label to click, but the link you connect to is the link stored in the last label. For example, if you click on all labels in the code above, you will go to the link corresponding to 'health [9] [1]'. I have a problem here. I want each label to be linked to the corresponding link.

enter image description here

It's a problem that can be solved if I don't use the for statement as shown in the picture above, but this is not the goal I want. I need your help.

황준석
  • 53
  • 7
  • please provide a [mre] – eyllanesc Sep 08 '21 at 06:55
  • 1
    Use `clickable(a).connect(lambda link=link: webbrowser.open(link))`. – musicamante Sep 08 '21 at 07:26
  • @musicamante Thank you. It's solved. I need to study more about lambda. Thank you for your precious time. – 황준석 Sep 08 '21 at 07:44
  • 1
    @황준석 you're welcome. Please accept the duplicate suggestion by clicking "yes" on your post header. Also consider that a function that creates an event filter for each label doesn't seem optimal, but you could at least improve it by adding the link to the function argument and directly emit it from there, so that you can directly connect to the webbrowser.open signal. A better solution, anyway, would be to just use a subclass of QLabel that already supports the custom signal, then you could just use something like `setLink` and connect the signal as above. – musicamante Sep 08 '21 at 07:58

0 Answers0