Hello everybody,

I’m experimenting with PyQt, trying to learn it, and I’m a bit stuck with Signals and Slots. I don’t usually like just to repeat the examples you find in tutorials and guides because I think it’s more useful for learning to experiment concepts by yourself. Tutorials usually teach you doing the most basic things which, once learnt, don’t usually apply to your personal situation or usage.
What I want to do is to replicate the KDE effect, you know that when you grab a window and drag it around the window temporary becomes transparent (low alpha).
I found the Qt class which is setWindowOpacity(), I tried to build a signal-slot system which when the left mouse is pressed on the main widget (i.e. for dragging it around) the widget will become transparent.
I can’t obtain this effect. There’s no error traceback it simply does nothing. Of course I missed something. Any suggestion or help to solve my problem.
Thanks a lot.
Here below my code.

import sys
from PyQt4.QtCore import *                        
from PyQt4.QtGui import *

class WizAndChipsCal(QWidget):

        def __init__(self, parent = None):
                QWidget.__init__(self)
                self.setWindowTitle("Wiz and Chips Calendar")
                self.setWindowIcon(QIcon("C:/Python26/PyQt/Icon/date.png"))
                self.setToolTip("Hello to this Wiz and Chips fancy calendar!")
                self.connect(self,
                             SIGNAL("QMouseEvent()"),
                             self.Opacity)
            
                self.title = ("<font color=red size=3><b>"\
                              + "Wiz and Chips Pushable Calendar!"\
                              + "</font></b>")
                self.Label = QLabel(self.title)
                self.Label.setAlignment(Qt.AlignCenter | Qt.AlignJustify)

                self.calendar = QCalendarWidget()
                self.calendar.setGridVisible(1)
                self.calendar.setMinimumHeight(180)
                self.calendar.setMinimumWidth(110)

                self.check1 = QCheckBox("check1")
                self.check2 = QCheckBox("check2")
                self.TextBox = QTextEdit("type something here")
                self.TextBox.setMaximumHeight(50)

                self.dateLabel = QLabel("Date:")
                self.dateLabel.setMaximumWidth(80)
                CurrDate = QDate.currentDate()
                self.date = QDateEdit(CurrDate)
                self.date.setMaximumWidth(80)

                self.CloseButton = QPushButton("&Quit")
                self.CloseButton.setToolTip("<font color=red size=2><b>" + "Press here to Quit" + "</font></b>")
                self.CloseButton.setMaximumSize(50, 25)

                self.infobox = QGroupBox("Info Box")
                self.infobox.setCheckable(1)
                self.infobox.setChecked(0)
                
                dateLayout = QHBoxLayout()
                dateLayout.addWidget(self.dateLabel)
                dateLayout.addWidget(self.date)
                dateLayout.addSpacing(170)
                
                GBoxLayout = QVBoxLayout(self.infobox)
                GBoxLayout.setSpacing(1)
                GBoxLayout.addLayout(dateLayout)
                GBoxLayout.addWidget(self.check1)
                GBoxLayout.addWidget(self.check2)
                GBoxLayout.addWidget(self.TextBox)

                Layout = QGridLayout()
                Layout.addWidget(self.Label, 0, 0)
                Layout.addWidget(self.calendar, 1, 0)
                Layout.addWidget(self.CloseButton, 4, 0)
                Layout.addWidget(self.infobox, 3, 0)
                self.setLayout(Layout)
                self.connect(self.CloseButton, 
                             SIGNAL("pressed()"), 
                             self.close)
                
        def Opacity(self):
                while QMouseEvent(Qt.LeftButton):
                        self.setWindowOpacity(0.5)
                else:
                        pass                                    
app = QApplication(sys.argv)
main_window = WizAndChipsCal()
main_window.show()
app.exec_()

I added print statements to Opacity() and nothing printed, meaning it never gets called. You might have to test for the location of the mouse cursor when the left button is pressed, or attach something to one particular widget. Sorry, I have never done anything like this so can't help much.

def Opacity(self):
     print "Opacity called"     
     while QMouseEvent(Qt.LeftButton):
          print "Opacity while"
          self.setWindowOpacity(0.5)
     else:
          print "Opacity else"
          pass

Thanks for your answer, I wrote the same question to the PyQt mail list and they answered me that my approach was wrong. I should rewrite the code and implement an event handling system.
The solution they suggested me is to forget about signal&slot and write the following function outside the .__init__ function.

def moveEvent(self, event):
            if self.isVisible():
                self.setWindowOpacity(0.5)
            QTimer.singleShot(1000, functools.partial(self.setWindowOpacity,1))

Of course you must also import functools at the beginning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.