Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QLabel Image setScaledContents(True) don't allow Qpainter updates
Forum Updated to NodeBB v4.3 + New Features

QLabel Image setScaledContents(True) don't allow Qpainter updates

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 423 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MVPavan
    wrote on last edited by MVPavan
    #1

    Below code paints a point on Image for every click, however it works only if ("self.imglabel.setScaledContents(True)") is commented. Any reason?

    import sys
    from PyQt5.QtCore import Qt, QPoint
    from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel, QSizePolicy, QMessageBox
    from PyQt5.QtGui import QPixmap, QPainter, QPen, QColor, QImage, QPalette
    
    
    class Menu(QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.central_widget = QWidget()               # define central widget
            self.setCentralWidget(self.central_widget)
            self.vbox = QVBoxLayout(self.central_widget)       
            self.vbox.addWidget(self.imgWidget())
            self.vbox.addWidget(QPushButton("test"))
            
        def imgWidget(self):
            self.imglabel = QLabel()
            self.imglabel.setScaledContents(True)
            self.image = QImage("calib.jpeg")
            self.imagepix = QPixmap.fromImage(self.image)
            self.imglabel.setPixmap(self.imagepix)
            self.imglabel.mousePressEvent = self.imgMousePress
            return self.imglabel
    
        def imgMousePress(self, e):
            painter = QPainter(self.imglabel.pixmap())
            pen = QPen()
            pen.setWidth(10)
            pen.setColor(QColor('red'))
            painter.setPen(pen)
            painter.drawPoint(e.x(), e.y())
            painter.end()
            self.imglabel.update()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        mainMenu = Menu()
        mainMenu.show()
        sys.exit(app.exec_())**bolded text**
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I can't answer to the why but one thing that is sure is that the way you use for the painting does not look correct.

      If you really want to customise the painting on your label, you should subclass QLabel and re-implement both paintEvent and mousePressEvent. The former to do the custom painting and the later to store the point you want to paint.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved