Subclassing Graphics view and loading it from ui file.

  • Hello,
    I am subclassing QGrahicsView so that I can control mouse input events for drawing multiple rectangles and selecting areas on the image. I am having a couple problems. I generate the view through qt designer, get a hold of the object as below. This is when things start to break. I originally scaled the QGraphicsView with **self.graphicsView_area.scale(2, 2)**but it doesn't work on my sub-class despite not overwriting the function.

    self.graphicsView_area = ImageGraphicsView(self.image_viewer.graphicsView_area)

    I also try overwritting the paintEvent function but even these lines tell me that the QWidget::paintEngine: Should no longer be called

    def paintEvent(self, event):
                qp = QPainter(self)

    Thanks in advance for any tips

  • Okay first - a mini fully functional program of some sort (does not have to be the actual thing just something that shows what you are doing and has the issue you describe) is extremely helpful as it allows anyone considering to help you an easy means (aka copy/paste) of seeing your issue and trying to render you some aid. Also what version of Python are you using and what version/flavor of qt are you using and what OS.

    That being said what you provided is extremely inadequate for rendering you the help you desire -- please augment.

    Next I would personally say -- stop using the designer -- yeah I know in some ways it makes things easier but at a price. I find that with just a little extra effort understanding how the layout system works will put you miles ahead of those that use the designer because not only will understand how it should be done you will not have to work with that extremely ugly code the designer creates nor will you be constrained by the designer anymore. I suggest looking at what the designer makes to give you an idea of how it might be done but then I would take that base and do some clean up rewriting it to be easier to use and understand.

  • Thanks for the reply and constructive feedback. I'm fairly new to qt so still getting the hang of how everything is tied together and how I can use it. Making a lot of beginner mistakes and learning about best practices. As you suggest I'll re-create what I have so far through code instead of the designer and make a better post if I run into issues. I'm building an image viewer with the ability to custom select areas of an image for pixel manipulation, hence trying to figure out how to capture mouse event and draw rectangles over the image. Thanks again!

  • For capturing events you may want to look into something called addEventFilter (or something very similar to that) however I have found that there are sometimes various signals you can easily intercept if you make the object in question a class however these signals are not always that easy to ascertain since they may be buried a few classes deep so pay attention to the object inheritance being fairly new myself to qt I am starting to get a better handle on how the documentation is laid out -- for instance QTreeView may not have the property you are looking for but its parent has it so I have learned to (if I cannot find something in the object that I think ought to be there) open its parent and peruse it and then its parent's parent if I did not find it within its parent. I typically have not had to go back any further than the grandparent for property or method but you never now.

Log in to reply